LCOV - code coverage report
Current view: top level - lib/vhost - vhost_crypto.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 1 962 0.1 %
Date: 2025-04-03 19:37:06 Functions: 1 20 5.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 1 1549 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 "iotlb.h"
      12                 :            : #include "rte_vhost_crypto.h"
      13                 :            : #include "vhost.h"
      14                 :            : #include "vhost_user.h"
      15                 :            : #include "virtio_crypto.h"
      16                 :            : 
      17                 :            : #define INHDR_LEN               (sizeof(struct virtio_crypto_inhdr))
      18                 :            : #define IV_OFFSET               (sizeof(struct rte_crypto_op) + \
      19                 :            :                                 sizeof(struct rte_crypto_sym_op))
      20                 :            : 
      21         [ -  + ]:        252 : RTE_LOG_REGISTER_SUFFIX(vhost_crypto_logtype, crypto, INFO);
      22                 :            : #define RTE_LOGTYPE_VHOST_CRYPTO        vhost_crypto_logtype
      23                 :            : 
      24                 :            : #define VC_LOG_ERR(...) \
      25                 :            :         RTE_LOG_LINE_PREFIX(ERR, VHOST_CRYPTO, "%s() line %u: ", \
      26                 :            :                 __func__ RTE_LOG_COMMA __LINE__, __VA_ARGS__)
      27                 :            : 
      28                 :            : #define VC_LOG_INFO(...) \
      29                 :            :         RTE_LOG_LINE_PREFIX(INFO, VHOST_CRYPTO, "%s() line %u: ", \
      30                 :            :                 __func__ RTE_LOG_COMMA __LINE__, __VA_ARGS__)
      31                 :            : 
      32                 :            : #ifdef RTE_LIBRTE_VHOST_DEBUG
      33                 :            : #define VC_LOG_DBG(...) \
      34                 :            :         RTE_LOG_LINE_PREFIX(DEBUG, VHOST_CRYPTO, "%s() line %u: ", \
      35                 :            :                 __func__ RTE_LOG_COMMA __LINE__, __VA_ARGS__)
      36                 :            : #else
      37                 :            : #define VC_LOG_DBG(...)
      38                 :            : #endif
      39                 :            : 
      40                 :            : #define VIRTIO_CRYPTO_FEATURES ((1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |      \
      41                 :            :                 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |                   \
      42                 :            :                 (1ULL << VIRTIO_RING_F_EVENT_IDX) |                       \
      43                 :            :                 (1ULL << VIRTIO_NET_F_CTRL_VQ) |                  \
      44                 :            :                 (1ULL << VIRTIO_F_VERSION_1) |                            \
      45                 :            :                 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))
      46                 :            : 
      47                 :            : #define IOVA_TO_VVA(t, dev, vq, a, l, p)                                \
      48                 :            :         ((t)(uintptr_t)vhost_iova_to_vva(dev, vq, a, l, p))
      49                 :            : 
      50                 :            : /*
      51                 :            :  * vhost_crypto_desc is used to copy original vring_desc to the local buffer
      52                 :            :  * before processing (except the next index). The copy result will be an
      53                 :            :  * array of vhost_crypto_desc elements that follows the sequence of original
      54                 :            :  * vring_desc.next is arranged.
      55                 :            :  */
      56                 :            : #define vhost_crypto_desc vring_desc
      57                 :            : 
      58                 :            : struct vhost_crypto_session {
      59                 :            :         union {
      60                 :            :                 struct rte_cryptodev_asym_session *asym;
      61                 :            :                 struct rte_cryptodev_sym_session *sym;
      62                 :            :         };
      63                 :            :         enum rte_crypto_op_type type;
      64                 :            : };
      65                 :            : 
      66                 :            : static int
      67                 :          0 : cipher_algo_transform(uint32_t virtio_cipher_algo,
      68                 :            :                 enum rte_crypto_cipher_algorithm *algo)
      69                 :            : {
      70   [ #  #  #  #  :          0 :         switch (virtio_cipher_algo) {
          #  #  #  #  #  
             #  #  #  # ]
      71                 :          0 :         case VIRTIO_CRYPTO_CIPHER_AES_CBC:
      72                 :          0 :                 *algo = RTE_CRYPTO_CIPHER_AES_CBC;
      73                 :          0 :                 break;
      74                 :          0 :         case VIRTIO_CRYPTO_CIPHER_AES_CTR:
      75                 :          0 :                 *algo = RTE_CRYPTO_CIPHER_AES_CTR;
      76                 :          0 :                 break;
      77                 :          0 :         case VIRTIO_CRYPTO_CIPHER_DES_ECB:
      78                 :          0 :                 *algo = -VIRTIO_CRYPTO_NOTSUPP;
      79                 :          0 :                 break;
      80                 :          0 :         case VIRTIO_CRYPTO_CIPHER_DES_CBC:
      81                 :          0 :                 *algo = RTE_CRYPTO_CIPHER_DES_CBC;
      82                 :          0 :                 break;
      83                 :          0 :         case VIRTIO_CRYPTO_CIPHER_3DES_ECB:
      84                 :          0 :                 *algo = RTE_CRYPTO_CIPHER_3DES_ECB;
      85                 :          0 :                 break;
      86                 :          0 :         case VIRTIO_CRYPTO_CIPHER_3DES_CBC:
      87                 :          0 :                 *algo = RTE_CRYPTO_CIPHER_3DES_CBC;
      88                 :          0 :                 break;
      89                 :          0 :         case VIRTIO_CRYPTO_CIPHER_3DES_CTR:
      90                 :          0 :                 *algo = RTE_CRYPTO_CIPHER_3DES_CTR;
      91                 :          0 :                 break;
      92                 :          0 :         case VIRTIO_CRYPTO_CIPHER_KASUMI_F8:
      93                 :          0 :                 *algo = RTE_CRYPTO_CIPHER_KASUMI_F8;
      94                 :          0 :                 break;
      95                 :          0 :         case VIRTIO_CRYPTO_CIPHER_SNOW3G_UEA2:
      96                 :          0 :                 *algo = RTE_CRYPTO_CIPHER_SNOW3G_UEA2;
      97                 :          0 :                 break;
      98                 :          0 :         case VIRTIO_CRYPTO_CIPHER_AES_F8:
      99                 :          0 :                 *algo = RTE_CRYPTO_CIPHER_AES_F8;
     100                 :          0 :                 break;
     101                 :          0 :         case VIRTIO_CRYPTO_CIPHER_AES_XTS:
     102                 :          0 :                 *algo = RTE_CRYPTO_CIPHER_AES_XTS;
     103                 :          0 :                 break;
     104                 :          0 :         case VIRTIO_CRYPTO_CIPHER_ZUC_EEA3:
     105                 :          0 :                 *algo = RTE_CRYPTO_CIPHER_ZUC_EEA3;
     106                 :          0 :                 break;
     107                 :            :         default:
     108                 :            :                 return -VIRTIO_CRYPTO_BADMSG;
     109                 :            :                 break;
     110                 :            :         }
     111                 :            : 
     112                 :            :         return 0;
     113                 :            : }
     114                 :            : 
     115                 :            : static int
     116                 :          0 : auth_algo_transform(uint32_t virtio_auth_algo,
     117                 :            :                 enum rte_crypto_auth_algorithm *algo)
     118                 :            : {
     119   [ #  #  #  #  :          0 :         switch (virtio_auth_algo) {
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     120                 :          0 :         case VIRTIO_CRYPTO_NO_MAC:
     121                 :          0 :                 *algo = RTE_CRYPTO_AUTH_NULL;
     122                 :          0 :                 break;
     123                 :          0 :         case VIRTIO_CRYPTO_MAC_HMAC_MD5:
     124                 :          0 :                 *algo = RTE_CRYPTO_AUTH_MD5_HMAC;
     125                 :          0 :                 break;
     126                 :          0 :         case VIRTIO_CRYPTO_MAC_HMAC_SHA1:
     127                 :          0 :                 *algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
     128                 :          0 :                 break;
     129                 :          0 :         case VIRTIO_CRYPTO_MAC_HMAC_SHA_224:
     130                 :          0 :                 *algo = RTE_CRYPTO_AUTH_SHA224_HMAC;
     131                 :          0 :                 break;
     132                 :          0 :         case VIRTIO_CRYPTO_MAC_HMAC_SHA_256:
     133                 :          0 :                 *algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
     134                 :          0 :                 break;
     135                 :          0 :         case VIRTIO_CRYPTO_MAC_HMAC_SHA_384:
     136                 :          0 :                 *algo = RTE_CRYPTO_AUTH_SHA384_HMAC;
     137                 :          0 :                 break;
     138                 :          0 :         case VIRTIO_CRYPTO_MAC_HMAC_SHA_512:
     139                 :          0 :                 *algo = RTE_CRYPTO_AUTH_SHA512_HMAC;
     140                 :          0 :                 break;
     141                 :          0 :         case VIRTIO_CRYPTO_MAC_CMAC_AES:
     142                 :          0 :                 *algo = RTE_CRYPTO_AUTH_AES_CMAC;
     143                 :          0 :                 break;
     144                 :          0 :         case VIRTIO_CRYPTO_MAC_KASUMI_F9:
     145                 :          0 :                 *algo = RTE_CRYPTO_AUTH_KASUMI_F9;
     146                 :          0 :                 break;
     147                 :          0 :         case VIRTIO_CRYPTO_MAC_SNOW3G_UIA2:
     148                 :          0 :                 *algo = RTE_CRYPTO_AUTH_SNOW3G_UIA2;
     149                 :          0 :                 break;
     150                 :          0 :         case VIRTIO_CRYPTO_MAC_GMAC_AES:
     151                 :          0 :                 *algo = RTE_CRYPTO_AUTH_AES_GMAC;
     152                 :          0 :                 break;
     153                 :          0 :         case VIRTIO_CRYPTO_MAC_CBCMAC_AES:
     154                 :          0 :                 *algo = RTE_CRYPTO_AUTH_AES_CBC_MAC;
     155                 :          0 :                 break;
     156                 :          0 :         case VIRTIO_CRYPTO_MAC_XCBC_AES:
     157                 :          0 :                 *algo = RTE_CRYPTO_AUTH_AES_XCBC_MAC;
     158                 :          0 :                 break;
     159                 :            :         case VIRTIO_CRYPTO_MAC_CMAC_3DES:
     160                 :            :         case VIRTIO_CRYPTO_MAC_GMAC_TWOFISH:
     161                 :            :         case VIRTIO_CRYPTO_MAC_CBCMAC_KASUMI_F9:
     162                 :            :                 return -VIRTIO_CRYPTO_NOTSUPP;
     163                 :          0 :         default:
     164                 :          0 :                 return -VIRTIO_CRYPTO_BADMSG;
     165                 :            :         }
     166                 :            : 
     167                 :            :         return 0;
     168                 :            : }
     169                 :            : 
     170                 :            : static int get_iv_len(enum rte_crypto_cipher_algorithm algo)
     171                 :            : {
     172                 :            :         int len;
     173                 :            : 
     174                 :          0 :         switch (algo) {
     175                 :            :         case RTE_CRYPTO_CIPHER_3DES_CBC:
     176                 :            :                 len = 8;
     177                 :            :                 break;
     178                 :            :         case RTE_CRYPTO_CIPHER_3DES_CTR:
     179                 :            :                 len = 8;
     180                 :            :                 break;
     181                 :            :         case RTE_CRYPTO_CIPHER_3DES_ECB:
     182                 :            :                 len = 8;
     183                 :            :                 break;
     184                 :          0 :         case RTE_CRYPTO_CIPHER_AES_CBC:
     185                 :            :                 len = 16;
     186                 :          0 :                 break;
     187                 :            : 
     188                 :            :         /* TODO: add common algos */
     189                 :            : 
     190                 :          0 :         default:
     191                 :            :                 len = -1;
     192                 :          0 :                 break;
     193                 :            :         }
     194                 :            : 
     195                 :            :         return len;
     196                 :            : }
     197                 :            : 
     198                 :            : /**
     199                 :            :  * vhost_crypto struct is used to maintain a number of virtio_cryptos and
     200                 :            :  * one DPDK crypto device that deals with all crypto workloads. It is declared
     201                 :            :  * here and defined in vhost_crypto.c
     202                 :            :  */
     203                 :            : struct __rte_cache_aligned vhost_crypto {
     204                 :            :         /** Used to lookup DPDK Cryptodev Session based on VIRTIO crypto
     205                 :            :          *  session ID.
     206                 :            :          */
     207                 :            :         struct rte_hash *session_map;
     208                 :            :         struct rte_mempool *mbuf_pool;
     209                 :            :         struct rte_mempool *sess_pool;
     210                 :            :         struct rte_mempool *wb_pool;
     211                 :            : 
     212                 :            :         /** DPDK cryptodev ID */
     213                 :            :         uint8_t cid;
     214                 :            :         uint16_t nb_qps;
     215                 :            : 
     216                 :            :         uint64_t last_session_id;
     217                 :            : 
     218                 :            :         uint64_t cache_sym_session_id;
     219                 :            :         struct rte_cryptodev_sym_session *cache_sym_session;
     220                 :            :         uint64_t cache_asym_session_id;
     221                 :            :         struct rte_cryptodev_asym_session *cache_asym_session;
     222                 :            :         /** socket id for the device */
     223                 :            :         int socket_id;
     224                 :            : 
     225                 :            :         struct virtio_net *dev;
     226                 :            : 
     227                 :            :         uint8_t option;
     228                 :            : };
     229                 :            : 
     230                 :            : struct vhost_crypto_writeback_data {
     231                 :            :         uint8_t *src;
     232                 :            :         uint8_t *dst;
     233                 :            :         uint64_t len;
     234                 :            :         struct vhost_crypto_writeback_data *next;
     235                 :            : };
     236                 :            : 
     237                 :            : struct vhost_crypto_data_req {
     238                 :            :         struct vring_desc *head;
     239                 :            :         struct virtio_net *dev;
     240                 :            :         struct virtio_crypto_inhdr *inhdr;
     241                 :            :         struct vhost_virtqueue *vq;
     242                 :            :         struct vhost_crypto_writeback_data *wb;
     243                 :            :         struct rte_mempool *wb_pool;
     244                 :            :         uint16_t desc_idx;
     245                 :            :         uint16_t len;
     246                 :            :         uint16_t zero_copy;
     247                 :            : };
     248                 :            : 
     249                 :            : static int
     250                 :          0 : transform_cipher_param(struct rte_crypto_sym_xform *xform,
     251                 :            :                 VhostUserCryptoSymSessionParam *param)
     252                 :            : {
     253                 :            :         int ret;
     254                 :            : 
     255                 :          0 :         ret = cipher_algo_transform(param->cipher_algo, &xform->cipher.algo);
     256         [ #  # ]:          0 :         if (unlikely(ret < 0))
     257                 :            :                 return ret;
     258                 :            : 
     259         [ #  # ]:          0 :         if (param->cipher_key_len > VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH) {
     260                 :            :                 VC_LOG_DBG("Invalid cipher key length");
     261                 :            :                 return -VIRTIO_CRYPTO_BADMSG;
     262                 :            :         }
     263                 :            : 
     264                 :          0 :         xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
     265                 :          0 :         xform->cipher.key.length = param->cipher_key_len;
     266         [ #  # ]:          0 :         if (xform->cipher.key.length > 0)
     267                 :          0 :                 xform->cipher.key.data = param->cipher_key_buf;
     268         [ #  # ]:          0 :         if (param->dir == VIRTIO_CRYPTO_OP_ENCRYPT)
     269                 :          0 :                 xform->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
     270         [ #  # ]:          0 :         else if (param->dir == VIRTIO_CRYPTO_OP_DECRYPT)
     271                 :          0 :                 xform->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
     272                 :            :         else {
     273                 :            :                 VC_LOG_DBG("Bad operation type");
     274                 :            :                 return -VIRTIO_CRYPTO_BADMSG;
     275                 :            :         }
     276                 :            : 
     277      [ #  #  # ]:          0 :         ret = get_iv_len(xform->cipher.algo);
     278         [ #  # ]:          0 :         if (unlikely(ret < 0))
     279                 :            :                 return ret;
     280                 :          0 :         xform->cipher.iv.length = (uint16_t)ret;
     281                 :          0 :         xform->cipher.iv.offset = IV_OFFSET;
     282                 :          0 :         return 0;
     283                 :            : }
     284                 :            : 
     285                 :            : static int
     286                 :          0 : transform_chain_param(struct rte_crypto_sym_xform *xforms,
     287                 :            :                 VhostUserCryptoSymSessionParam *param)
     288                 :            : {
     289                 :            :         struct rte_crypto_sym_xform *xform_cipher, *xform_auth;
     290                 :            :         int ret;
     291                 :            : 
     292      [ #  #  # ]:          0 :         switch (param->chaining_dir) {
     293                 :          0 :         case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER:
     294                 :            :                 xform_auth = xforms;
     295                 :          0 :                 xform_cipher = xforms->next;
     296                 :          0 :                 xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
     297                 :          0 :                 xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
     298                 :          0 :                 break;
     299                 :          0 :         case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH:
     300                 :            :                 xform_cipher = xforms;
     301                 :          0 :                 xform_auth = xforms->next;
     302                 :          0 :                 xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
     303                 :          0 :                 xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
     304                 :          0 :                 break;
     305                 :            :         default:
     306                 :            :                 return -VIRTIO_CRYPTO_BADMSG;
     307                 :            :         }
     308                 :            : 
     309                 :            :         /* cipher */
     310                 :          0 :         ret = cipher_algo_transform(param->cipher_algo,
     311                 :            :                         &xform_cipher->cipher.algo);
     312         [ #  # ]:          0 :         if (unlikely(ret < 0))
     313                 :            :                 return ret;
     314                 :            : 
     315         [ #  # ]:          0 :         if (param->cipher_key_len > VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH) {
     316                 :            :                 VC_LOG_DBG("Invalid cipher key length");
     317                 :            :                 return -VIRTIO_CRYPTO_BADMSG;
     318                 :            :         }
     319                 :            : 
     320                 :          0 :         xform_cipher->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
     321                 :          0 :         xform_cipher->cipher.key.length = param->cipher_key_len;
     322                 :          0 :         xform_cipher->cipher.key.data = param->cipher_key_buf;
     323      [ #  #  # ]:          0 :         ret = get_iv_len(xform_cipher->cipher.algo);
     324         [ #  # ]:          0 :         if (unlikely(ret < 0))
     325                 :            :                 return ret;
     326                 :          0 :         xform_cipher->cipher.iv.length = (uint16_t)ret;
     327                 :          0 :         xform_cipher->cipher.iv.offset = IV_OFFSET;
     328                 :            : 
     329                 :            :         /* auth */
     330                 :          0 :         xform_auth->type = RTE_CRYPTO_SYM_XFORM_AUTH;
     331                 :          0 :         ret = auth_algo_transform(param->hash_algo, &xform_auth->auth.algo);
     332         [ #  # ]:          0 :         if (unlikely(ret < 0))
     333                 :            :                 return ret;
     334                 :            : 
     335         [ #  # ]:          0 :         if (param->auth_key_len > VHOST_USER_CRYPTO_MAX_HMAC_KEY_LENGTH) {
     336                 :            :                 VC_LOG_DBG("Invalid auth key length");
     337                 :            :                 return -VIRTIO_CRYPTO_BADMSG;
     338                 :            :         }
     339                 :            : 
     340                 :          0 :         xform_auth->auth.digest_length = param->digest_len;
     341                 :          0 :         xform_auth->auth.key.length = param->auth_key_len;
     342                 :          0 :         xform_auth->auth.key.data = param->auth_key_buf;
     343                 :            : 
     344                 :          0 :         return 0;
     345                 :            : }
     346                 :            : 
     347                 :            : static void
     348                 :          0 : vhost_crypto_create_sym_sess(struct vhost_crypto *vcrypto,
     349                 :            :                 VhostUserCryptoSessionParam *sess_param)
     350                 :            : {
     351                 :          0 :         struct rte_crypto_sym_xform xform1 = {0}, xform2 = {0};
     352                 :            :         struct vhost_crypto_session *vhost_session;
     353                 :            :         struct rte_cryptodev_sym_session *session;
     354                 :            :         int ret;
     355                 :            : 
     356      [ #  #  # ]:          0 :         switch (sess_param->u.sym_sess.op_type) {
     357                 :          0 :         case VIRTIO_CRYPTO_SYM_OP_NONE:
     358                 :            :         case VIRTIO_CRYPTO_SYM_OP_CIPHER:
     359                 :          0 :                 ret = transform_cipher_param(&xform1, &sess_param->u.sym_sess);
     360         [ #  # ]:          0 :                 if (unlikely(ret)) {
     361                 :          0 :                         VC_LOG_ERR("Error transform session msg (%i)", ret);
     362                 :          0 :                         sess_param->session_id = ret;
     363                 :          0 :                         return;
     364                 :            :                 }
     365                 :            :                 break;
     366                 :          0 :         case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
     367         [ #  # ]:          0 :                 if (unlikely(sess_param->u.sym_sess.hash_mode !=
     368                 :            :                                 VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH)) {
     369                 :          0 :                         sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
     370                 :          0 :                         VC_LOG_ERR("Error transform session message (%i)",
     371                 :            :                                         -VIRTIO_CRYPTO_NOTSUPP);
     372                 :          0 :                         return;
     373                 :            :                 }
     374                 :            : 
     375                 :          0 :                 xform1.next = &xform2;
     376                 :            : 
     377                 :          0 :                 ret = transform_chain_param(&xform1, &sess_param->u.sym_sess);
     378         [ #  # ]:          0 :                 if (unlikely(ret)) {
     379                 :          0 :                         VC_LOG_ERR("Error transform session message (%i)", ret);
     380                 :          0 :                         sess_param->session_id = ret;
     381                 :          0 :                         return;
     382                 :            :                 }
     383                 :            : 
     384                 :            :                 break;
     385                 :          0 :         default:
     386                 :          0 :                 VC_LOG_ERR("Algorithm not yet supported");
     387                 :          0 :                 sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
     388                 :          0 :                 return;
     389                 :            :         }
     390                 :            : 
     391                 :          0 :         session = rte_cryptodev_sym_session_create(vcrypto->cid, &xform1,
     392                 :            :                         vcrypto->sess_pool);
     393         [ #  # ]:          0 :         if (!session) {
     394                 :          0 :                 VC_LOG_ERR("Failed to create session");
     395                 :          0 :                 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
     396                 :          0 :                 return;
     397                 :            :         }
     398                 :            : 
     399                 :          0 :         vhost_session = rte_zmalloc(NULL, sizeof(*vhost_session), 0);
     400         [ #  # ]:          0 :         if (vhost_session == NULL) {
     401                 :          0 :                 VC_LOG_ERR("Failed to alloc session memory");
     402                 :          0 :                 goto error_exit;
     403                 :            :         }
     404                 :            : 
     405                 :          0 :         vhost_session->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
     406                 :          0 :         vhost_session->sym = session;
     407                 :            : 
     408                 :            :         /* insert session to map */
     409         [ #  # ]:          0 :         if ((rte_hash_add_key_data(vcrypto->session_map,
     410                 :          0 :                 &vcrypto->last_session_id, vhost_session) < 0)) {
     411                 :          0 :                 VC_LOG_ERR("Failed to insert session to hash table");
     412                 :          0 :                 goto error_exit;
     413                 :            :         }
     414                 :            : 
     415                 :          0 :         VC_LOG_INFO("Session %"PRIu64" created for vdev %i.",
     416                 :            :                         vcrypto->last_session_id, vcrypto->dev->vid);
     417                 :            : 
     418                 :          0 :         sess_param->session_id = vcrypto->last_session_id;
     419                 :          0 :         vcrypto->last_session_id++;
     420                 :          0 :         return;
     421                 :            : 
     422                 :          0 : error_exit:
     423         [ #  # ]:          0 :         if (rte_cryptodev_sym_session_free(vcrypto->cid, session) < 0)
     424                 :          0 :                 VC_LOG_ERR("Failed to free session");
     425                 :            : 
     426                 :          0 :         sess_param->session_id = -VIRTIO_CRYPTO_ERR;
     427                 :          0 :         rte_free(vhost_session);
     428                 :            : }
     429                 :            : 
     430                 :            : static int
     431                 :            : tlv_decode(uint8_t *tlv, uint8_t type, uint8_t **data, size_t *data_len)
     432                 :            : {
     433                 :            :         size_t tlen = -EINVAL, len;
     434                 :            : 
     435         [ #  # ]:          0 :         if (tlv[0] != type)
     436                 :            :                 return -EINVAL;
     437                 :            : 
     438   [ #  #  #  #  :          0 :         if (tlv[1] == 0x82) {
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     439                 :          0 :                 len = (tlv[2] << 8) | tlv[3];
     440                 :          0 :                 *data = &tlv[4];
     441                 :          0 :                 tlen = len + 4;
     442   [ #  #  #  #  :          0 :         } else if (tlv[1] == 0x81) {
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     443                 :          0 :                 len = tlv[2];
     444                 :          0 :                 *data = &tlv[3];
     445                 :          0 :                 tlen = len + 3;
     446                 :            :         } else {
     447                 :          0 :                 len = tlv[1];
     448                 :          0 :                 *data = &tlv[2];
     449                 :          0 :                 tlen = len + 2;
     450                 :            :         }
     451                 :            : 
     452                 :            :         *data_len = len;
     453                 :          0 :         return tlen;
     454                 :            : }
     455                 :            : 
     456                 :            : static int
     457                 :          0 : virtio_crypto_asym_rsa_der_to_xform(uint8_t *der, size_t der_len,
     458                 :            :                 struct rte_crypto_asym_xform *xform)
     459                 :            : {
     460                 :            :         uint8_t *n = NULL, *e = NULL, *d = NULL, *p = NULL, *q = NULL, *dp = NULL,
     461                 :            :                 *dq = NULL, *qinv = NULL, *v = NULL, *tlv;
     462                 :            :         size_t nlen, elen, dlen, plen, qlen, dplen, dqlen, qinvlen, vlen;
     463                 :            :         int len;
     464                 :            : 
     465                 :            :         RTE_SET_USED(der_len);
     466                 :            : 
     467         [ #  # ]:          0 :         if (der[0] != 0x30)
     468                 :            :                 return -EINVAL;
     469                 :            : 
     470         [ #  # ]:          0 :         if (der[1] == 0x82)
     471                 :          0 :                 tlv = &der[4];
     472         [ #  # ]:          0 :         else if (der[1] == 0x81)
     473                 :          0 :                 tlv = &der[3];
     474                 :            :         else
     475                 :            :                 return -EINVAL;
     476                 :            : 
     477                 :            :         len = tlv_decode(tlv, 0x02, &v, &vlen);
     478   [ #  #  #  #  :          0 :         if (len < 0 || v[0] != 0x0 || vlen != 1)
                   #  # ]
     479                 :            :                 return -EINVAL;
     480                 :            : 
     481         [ #  # ]:          0 :         tlv = tlv + len;
     482                 :            :         len = tlv_decode(tlv, 0x02, &n, &nlen);
     483         [ #  # ]:          0 :         if (len < 0)
     484                 :            :                 return len;
     485                 :            : 
     486         [ #  # ]:          0 :         tlv = tlv + len;
     487                 :            :         len = tlv_decode(tlv, 0x02, &e, &elen);
     488         [ #  # ]:          0 :         if (len < 0)
     489                 :            :                 return len;
     490                 :            : 
     491         [ #  # ]:          0 :         tlv = tlv + len;
     492                 :            :         len = tlv_decode(tlv, 0x02, &d, &dlen);
     493         [ #  # ]:          0 :         if (len < 0)
     494                 :            :                 return len;
     495                 :            : 
     496         [ #  # ]:          0 :         tlv = tlv + len;
     497                 :            :         len = tlv_decode(tlv, 0x02, &p, &plen);
     498         [ #  # ]:          0 :         if (len < 0)
     499                 :            :                 return len;
     500                 :            : 
     501         [ #  # ]:          0 :         tlv = tlv + len;
     502                 :            :         len = tlv_decode(tlv, 0x02, &q, &qlen);
     503         [ #  # ]:          0 :         if (len < 0)
     504                 :            :                 return len;
     505                 :            : 
     506         [ #  # ]:          0 :         tlv = tlv + len;
     507                 :            :         len = tlv_decode(tlv, 0x02, &dp, &dplen);
     508         [ #  # ]:          0 :         if (len < 0)
     509                 :            :                 return len;
     510                 :            : 
     511         [ #  # ]:          0 :         tlv = tlv + len;
     512                 :            :         len = tlv_decode(tlv, 0x02, &dq, &dqlen);
     513         [ #  # ]:          0 :         if (len < 0)
     514                 :            :                 return len;
     515                 :            : 
     516         [ #  # ]:          0 :         tlv = tlv + len;
     517                 :            :         len = tlv_decode(tlv, 0x02, &qinv, &qinvlen);
     518         [ #  # ]:          0 :         if (len < 0)
     519                 :            :                 return len;
     520                 :            : 
     521                 :          0 :         xform->rsa.n.data = n;
     522                 :          0 :         xform->rsa.n.length = nlen;
     523                 :          0 :         xform->rsa.e.data = e;
     524                 :          0 :         xform->rsa.e.length = elen;
     525                 :          0 :         xform->rsa.d.data = d;
     526                 :          0 :         xform->rsa.d.length = dlen;
     527                 :          0 :         xform->rsa.qt.p.data = p;
     528                 :          0 :         xform->rsa.qt.p.length = plen;
     529                 :          0 :         xform->rsa.qt.q.data = q;
     530                 :          0 :         xform->rsa.qt.q.length = qlen;
     531                 :          0 :         xform->rsa.qt.dP.data = dp;
     532                 :          0 :         xform->rsa.qt.dP.length = dplen;
     533                 :          0 :         xform->rsa.qt.dQ.data = dq;
     534                 :          0 :         xform->rsa.qt.dQ.length = dqlen;
     535                 :          0 :         xform->rsa.qt.qInv.data = qinv;
     536                 :          0 :         xform->rsa.qt.qInv.length = qinvlen;
     537                 :            : 
     538                 :            :         RTE_ASSERT(tlv + len == der + der_len);
     539                 :          0 :         return 0;
     540                 :            : }
     541                 :            : 
     542                 :            : static int
     543                 :          0 : rsa_param_transform(struct rte_crypto_asym_xform *xform,
     544                 :            :                 VhostUserCryptoAsymSessionParam *param)
     545                 :            : {
     546                 :            :         int ret;
     547                 :            : 
     548                 :          0 :         ret = virtio_crypto_asym_rsa_der_to_xform(param->key_buf, param->key_len, xform);
     549         [ #  # ]:          0 :         if (ret < 0)
     550                 :            :                 return ret;
     551                 :            : 
     552      [ #  #  # ]:          0 :         switch (param->u.rsa.padding_algo) {
     553                 :          0 :         case VIRTIO_CRYPTO_RSA_RAW_PADDING:
     554                 :          0 :                 xform->rsa.padding.type = RTE_CRYPTO_RSA_PADDING_NONE;
     555                 :          0 :                 break;
     556                 :          0 :         case VIRTIO_CRYPTO_RSA_PKCS1_PADDING:
     557                 :          0 :                 xform->rsa.padding.type = RTE_CRYPTO_RSA_PADDING_PKCS1_5;
     558                 :          0 :                 break;
     559                 :          0 :         default:
     560                 :          0 :                 VC_LOG_ERR("Unknown padding type");
     561                 :          0 :                 return -EINVAL;
     562                 :            :         }
     563                 :            : 
     564                 :          0 :         xform->rsa.key_type = RTE_RSA_KEY_TYPE_QT;
     565                 :          0 :         xform->xform_type = RTE_CRYPTO_ASYM_XFORM_RSA;
     566                 :          0 :         return 0;
     567                 :            : }
     568                 :            : 
     569                 :            : static void
     570                 :          0 : vhost_crypto_create_asym_sess(struct vhost_crypto *vcrypto,
     571                 :            :                 VhostUserCryptoSessionParam *sess_param)
     572                 :            : {
     573                 :          0 :         struct rte_cryptodev_asym_session *session = NULL;
     574                 :            :         struct vhost_crypto_session *vhost_session;
     575                 :          0 :         struct rte_crypto_asym_xform xform = {0};
     576                 :            :         int ret;
     577                 :            : 
     578         [ #  # ]:          0 :         switch (sess_param->u.asym_sess.algo) {
     579                 :          0 :         case VIRTIO_CRYPTO_AKCIPHER_RSA:
     580                 :          0 :                 ret = rsa_param_transform(&xform, &sess_param->u.asym_sess);
     581         [ #  # ]:          0 :                 if (unlikely(ret < 0)) {
     582                 :          0 :                         VC_LOG_ERR("Error transform session msg (%i)", ret);
     583                 :          0 :                         sess_param->session_id = ret;
     584                 :          0 :                         return;
     585                 :            :                 }
     586                 :            :                 break;
     587                 :          0 :         default:
     588                 :          0 :                 VC_LOG_ERR("Invalid op algo");
     589                 :          0 :                 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
     590                 :          0 :                 return;
     591                 :            :         }
     592                 :            : 
     593                 :          0 :         ret = rte_cryptodev_asym_session_create(vcrypto->cid, &xform,
     594                 :            :                 vcrypto->sess_pool, (void *)&session);
     595         [ #  # ]:          0 :         if (session == NULL) {
     596                 :          0 :                 VC_LOG_ERR("Failed to create session");
     597                 :          0 :                 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
     598                 :          0 :                 return;
     599                 :            :         }
     600                 :            : 
     601                 :          0 :         vhost_session = rte_zmalloc(NULL, sizeof(*vhost_session), 0);
     602         [ #  # ]:          0 :         if (vhost_session == NULL) {
     603                 :          0 :                 VC_LOG_ERR("Failed to alloc session memory");
     604                 :          0 :                 goto error_exit;
     605                 :            :         }
     606                 :            : 
     607                 :          0 :         vhost_session->type = RTE_CRYPTO_OP_TYPE_ASYMMETRIC;
     608                 :          0 :         vhost_session->asym = session;
     609                 :            : 
     610                 :            :         /* insert session to map */
     611         [ #  # ]:          0 :         if ((rte_hash_add_key_data(vcrypto->session_map,
     612                 :          0 :                         &vcrypto->last_session_id, vhost_session) < 0)) {
     613                 :          0 :                 VC_LOG_ERR("Failed to insert session to hash table");
     614                 :          0 :                 goto error_exit;
     615                 :            :         }
     616                 :            : 
     617                 :          0 :         VC_LOG_INFO("Session %"PRIu64" created for vdev %i.",
     618                 :            :                         vcrypto->last_session_id, vcrypto->dev->vid);
     619                 :            : 
     620                 :          0 :         sess_param->session_id = vcrypto->last_session_id;
     621                 :          0 :         vcrypto->last_session_id++;
     622                 :          0 :         return;
     623                 :            : 
     624                 :          0 : error_exit:
     625         [ #  # ]:          0 :         if (rte_cryptodev_asym_session_free(vcrypto->cid, session) < 0)
     626                 :          0 :                 VC_LOG_ERR("Failed to free session");
     627                 :          0 :         sess_param->session_id = -VIRTIO_CRYPTO_ERR;
     628                 :          0 :         rte_free(vhost_session);
     629                 :            : }
     630                 :            : 
     631                 :            : static void
     632                 :          0 : vhost_crypto_create_sess(struct vhost_crypto *vcrypto,
     633                 :            :                 VhostUserCryptoSessionParam *sess_param)
     634                 :            : {
     635         [ #  # ]:          0 :         if (sess_param->op_code == VIRTIO_CRYPTO_AKCIPHER_CREATE_SESSION)
     636                 :          0 :                 vhost_crypto_create_asym_sess(vcrypto, sess_param);
     637                 :            :         else
     638                 :          0 :                 vhost_crypto_create_sym_sess(vcrypto, sess_param);
     639                 :          0 : }
     640                 :            : 
     641                 :            : static int
     642                 :          0 : vhost_crypto_close_sess(struct vhost_crypto *vcrypto, uint64_t session_id)
     643                 :            : {
     644                 :          0 :         struct vhost_crypto_session *vhost_session = NULL;
     645                 :          0 :         uint64_t sess_id = session_id;
     646                 :            :         int ret;
     647                 :            : 
     648                 :          0 :         ret = rte_hash_lookup_data(vcrypto->session_map, &sess_id,
     649                 :            :                                 (void **)&vhost_session);
     650         [ #  # ]:          0 :         if (unlikely(ret < 0)) {
     651                 :          0 :                 VC_LOG_ERR("Failed to find session for id %"PRIu64".", session_id);
     652                 :          0 :                 return -VIRTIO_CRYPTO_INVSESS;
     653                 :            :         }
     654                 :            : 
     655         [ #  # ]:          0 :         if (vhost_session->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
     656         [ #  # ]:          0 :                 if (rte_cryptodev_sym_session_free(vcrypto->cid,
     657                 :          0 :                         vhost_session->sym) < 0) {
     658                 :            :                         VC_LOG_DBG("Failed to free session");
     659                 :            :                         return -VIRTIO_CRYPTO_ERR;
     660                 :            :                 }
     661         [ #  # ]:          0 :         } else if (vhost_session->type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC) {
     662         [ #  # ]:          0 :                 if (rte_cryptodev_asym_session_free(vcrypto->cid,
     663                 :          0 :                         vhost_session->asym) < 0) {
     664                 :            :                         VC_LOG_DBG("Failed to free session");
     665                 :            :                         return -VIRTIO_CRYPTO_ERR;
     666                 :            :                         }
     667                 :            :         } else {
     668                 :          0 :                 VC_LOG_ERR("Invalid session for id %"PRIu64".", session_id);
     669                 :          0 :                 return -VIRTIO_CRYPTO_INVSESS;
     670                 :            :         }
     671                 :            : 
     672         [ #  # ]:          0 :         if (rte_hash_del_key(vcrypto->session_map, &sess_id) < 0) {
     673                 :            :                 VC_LOG_DBG("Failed to delete session from hash table.");
     674                 :            :                 return -VIRTIO_CRYPTO_ERR;
     675                 :            :         }
     676                 :            : 
     677                 :          0 :         VC_LOG_INFO("Session %"PRIu64" deleted for vdev %i.", sess_id,
     678                 :            :                         vcrypto->dev->vid);
     679                 :            : 
     680                 :          0 :         rte_free(vhost_session);
     681                 :          0 :         return 0;
     682                 :            : }
     683                 :            : 
     684                 :            : static enum rte_vhost_msg_result
     685         [ #  # ]:          0 : vhost_crypto_msg_post_handler(int vid, void *msg)
     686                 :            : {
     687                 :            :         struct virtio_net *dev = get_device(vid);
     688                 :            :         struct vhost_crypto *vcrypto;
     689                 :            :         struct vhu_msg_context *ctx = msg;
     690                 :            :         enum rte_vhost_msg_result ret = RTE_VHOST_MSG_RESULT_OK;
     691                 :            : 
     692         [ #  # ]:          0 :         if (dev == NULL) {
     693                 :          0 :                 VC_LOG_ERR("Invalid vid %i", vid);
     694                 :          0 :                 return RTE_VHOST_MSG_RESULT_ERR;
     695                 :            :         }
     696                 :            : 
     697                 :          0 :         vcrypto = dev->extern_data;
     698         [ #  # ]:          0 :         if (vcrypto == NULL) {
     699                 :          0 :                 VC_LOG_ERR("Cannot find required data, is it initialized?");
     700                 :          0 :                 return RTE_VHOST_MSG_RESULT_ERR;
     701                 :            :         }
     702                 :            : 
     703      [ #  #  # ]:          0 :         switch (ctx->msg.request.frontend) {
     704                 :          0 :         case VHOST_USER_CRYPTO_CREATE_SESS:
     705                 :          0 :                 vhost_crypto_create_sess(vcrypto,
     706                 :            :                                 &ctx->msg.payload.crypto_session);
     707                 :          0 :                 ctx->fd_num = 0;
     708                 :            :                 ret = RTE_VHOST_MSG_RESULT_REPLY;
     709                 :          0 :                 break;
     710                 :          0 :         case VHOST_USER_CRYPTO_CLOSE_SESS:
     711         [ #  # ]:          0 :                 if (vhost_crypto_close_sess(vcrypto, ctx->msg.payload.u64))
     712                 :            :                         ret = RTE_VHOST_MSG_RESULT_ERR;
     713                 :            :                 break;
     714                 :            :         default:
     715                 :            :                 ret = RTE_VHOST_MSG_RESULT_NOT_HANDLED;
     716                 :            :                 break;
     717                 :            :         }
     718                 :            : 
     719                 :            :         return ret;
     720                 :            : }
     721                 :            : 
     722                 :            : static __rte_always_inline struct vhost_crypto_desc *
     723                 :            : find_write_desc(struct vhost_crypto_desc *head, struct vhost_crypto_desc *desc,
     724                 :            :                 uint32_t max_n_descs)
     725                 :            : {
     726   [ #  #  #  #  :          0 :         if (desc < head)
             #  #  #  # ]
     727                 :            :                 return NULL;
     728                 :            : 
     729   [ #  #  #  #  :          0 :         while (desc - head < (int)max_n_descs) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     730   [ #  #  #  #  :          0 :                 if (desc->flags & VRING_DESC_F_WRITE)
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     731                 :            :                         return desc;
     732                 :          0 :                 desc++;
     733                 :            :         }
     734                 :            : 
     735                 :            :         return NULL;
     736                 :            : }
     737                 :            : 
     738                 :            : static __rte_always_inline struct virtio_crypto_inhdr *
     739                 :            : reach_inhdr(struct virtio_net *dev, struct vhost_virtqueue *vq,
     740                 :            :                 struct vhost_crypto_desc *head,
     741                 :            :                 uint32_t max_n_descs)
     742                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
     743                 :            : {
     744                 :            :         struct virtio_crypto_inhdr *inhdr;
     745                 :          0 :         struct vhost_crypto_desc *last = head + (max_n_descs - 1);
     746                 :          0 :         uint64_t dlen = last->len;
     747                 :            : 
     748                 :          0 :         if (unlikely(dlen != sizeof(*inhdr)))
     749                 :            :                 return NULL;
     750                 :            : 
     751   [ #  #  #  # ]:          0 :         inhdr = IOVA_TO_VVA(struct virtio_crypto_inhdr *, dev, vq,
     752                 :            :                         last->addr, &dlen, VHOST_ACCESS_WO);
     753   [ #  #  #  #  :          0 :         if (unlikely(!inhdr || dlen != last->len))
             #  #  #  # ]
     754                 :          0 :                 return NULL;
     755                 :            : 
     756                 :            :         return inhdr;
     757                 :            : }
     758                 :            : 
     759                 :            : static __rte_always_inline int
     760                 :            : move_desc(struct vhost_crypto_desc *head,
     761                 :            :                 struct vhost_crypto_desc **cur_desc,
     762                 :            :                 uint32_t size, uint32_t max_n_descs)
     763                 :            : {
     764                 :            :         struct vhost_crypto_desc *desc = *cur_desc;
     765                 :          0 :         int left = size - desc->len;
     766                 :            : 
     767                 :          0 :         while (desc->flags & VRING_DESC_F_NEXT && left > 0 &&
     768   [ #  #  #  #  :          0 :                         desc >= head &&
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     769   [ #  #  #  #  :          0 :                         desc - head < (int)max_n_descs) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     770                 :          0 :                 desc++;
     771                 :          0 :                 left -= desc->len;
     772                 :            :         }
     773                 :            : 
     774   [ #  #  #  #  :          0 :         if (unlikely(left > 0))
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     775                 :            :                 return -1;
     776                 :            : 
     777   [ #  #  #  #  :          0 :         if (unlikely(head - desc == (int)max_n_descs))
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     778                 :            :                 *cur_desc = NULL;
     779                 :            :         else
     780                 :          0 :                 *cur_desc = desc + 1;
     781                 :            : 
     782                 :            :         return 0;
     783                 :            : }
     784                 :            : 
     785                 :            : static __rte_always_inline void *
     786                 :            : get_data_ptr(struct vhost_virtqueue *vq, struct vhost_crypto_data_req *vc_req,
     787                 :            :                 struct vhost_crypto_desc *cur_desc,
     788                 :            :                 uint8_t perm)
     789                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
     790                 :            : {
     791                 :            :         void *data;
     792                 :          0 :         uint64_t dlen = cur_desc->len;
     793                 :            : 
     794   [ #  #  #  #  :          0 :         data = IOVA_TO_VVA(void *, vc_req->dev, vq,
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     795                 :            :                         cur_desc->addr, &dlen, perm);
     796   [ #  #  #  #  :          0 :         if (unlikely(!data || dlen != cur_desc->len)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     797                 :          0 :                 VC_LOG_ERR("Failed to map object");
     798                 :          0 :                 return NULL;
     799                 :            :         }
     800                 :            : 
     801                 :            :         return data;
     802                 :            : }
     803                 :            : 
     804                 :            : static __rte_always_inline uint32_t
     805                 :            : copy_data_from_desc(void *dst, struct virtio_net *dev,
     806                 :            :         struct vhost_virtqueue *vq, struct vhost_crypto_desc *desc, uint32_t size)
     807                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
     808                 :            : {
     809                 :            :         uint64_t remain;
     810                 :            :         uint64_t addr;
     811                 :            : 
     812                 :          0 :         remain = RTE_MIN(desc->len, size);
     813                 :          0 :         addr = desc->addr;
     814                 :            :         do {
     815                 :            :                 uint64_t len;
     816                 :            :                 void *src;
     817                 :            : 
     818   [ #  #  #  #  :          0 :                 len = remain;
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     819                 :          0 :                 src = IOVA_TO_VVA(void *, dev, vq,
     820                 :            :                                 addr, &len, VHOST_ACCESS_RO);
     821   [ #  #  #  #  :          0 :                 if (unlikely(src == NULL || len == 0))
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     822                 :          0 :                         return 0;
     823                 :            : 
     824                 :            :                 rte_memcpy(dst, src, len);
     825                 :          0 :                 remain -= len;
     826                 :            :                 /* cast is needed for 32-bit architecture */
     827                 :          0 :                 dst = RTE_PTR_ADD(dst, (size_t)len);
     828                 :          0 :                 addr += len;
     829   [ #  #  #  #  :          0 :         } while (unlikely(remain != 0));
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     830                 :            : 
     831                 :            :         return RTE_MIN(desc->len, size);
     832                 :            : }
     833                 :            : 
     834                 :            : 
     835                 :            : static __rte_always_inline int
     836                 :            : copy_data(void *data, struct virtio_net *dev, struct vhost_virtqueue *vq,
     837                 :            :         struct vhost_crypto_desc *head, struct vhost_crypto_desc **cur_desc,
     838                 :            :         uint32_t size, uint32_t max_n_descs)
     839                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
     840                 :            : {
     841                 :            :         struct vhost_crypto_desc *desc = *cur_desc;
     842                 :            :         uint32_t left = size;
     843                 :            : 
     844                 :            :         do {
     845                 :            :                 uint32_t copied;
     846                 :            : 
     847                 :            :                 copied = copy_data_from_desc(data, dev, vq, desc, left);
     848   [ #  #  #  #  :          0 :                 if (copied == 0)
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     849                 :            :                         return -1;
     850                 :          0 :                 left -= copied;
     851                 :          0 :                 data = RTE_PTR_ADD(data, copied);
     852   [ #  #  #  #  :          0 :         } while (left != 0 && ++desc < head + max_n_descs);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     853                 :            : 
     854   [ #  #  #  #  :          0 :         if (unlikely(left != 0))
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     855                 :            :                 return -1;
     856                 :            : 
     857   [ #  #  #  #  :          0 :         if (unlikely(desc == head + max_n_descs))
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     858                 :            :                 *cur_desc = NULL;
     859                 :            :         else
     860                 :          0 :                 *cur_desc = desc + 1;
     861                 :            : 
     862                 :            :         return 0;
     863                 :            : }
     864                 :            : 
     865                 :            : static void
     866                 :          0 : write_back_data(struct vhost_crypto_data_req *vc_req)
     867                 :            : {
     868                 :          0 :         struct vhost_crypto_writeback_data *wb_data = vc_req->wb, *wb_last;
     869                 :            : 
     870         [ #  # ]:          0 :         while (wb_data) {
     871         [ #  # ]:          0 :                 rte_memcpy(wb_data->dst, wb_data->src, wb_data->len);
     872         [ #  # ]:          0 :                 memset(wb_data->src, 0, wb_data->len);
     873                 :            :                 wb_last = wb_data;
     874                 :          0 :                 wb_data = wb_data->next;
     875         [ #  # ]:          0 :                 rte_mempool_put(vc_req->wb_pool, wb_last);
     876                 :            :         }
     877                 :          0 : }
     878                 :            : 
     879                 :            : static void
     880                 :          0 : free_wb_data(struct vhost_crypto_writeback_data *wb_data,
     881                 :            :                 struct rte_mempool *mp)
     882                 :            : {
     883         [ #  # ]:          0 :         while (wb_data->next != NULL)
     884                 :          0 :                 free_wb_data(wb_data->next, mp);
     885                 :            : 
     886                 :          0 :         rte_mempool_put(mp, wb_data);
     887                 :          0 : }
     888                 :            : 
     889                 :            : /**
     890                 :            :  * The function will allocate a vhost_crypto_writeback_data linked list
     891                 :            :  * containing the source and destination data pointers for the write back
     892                 :            :  * operation after dequeued from Cryptodev PMD queues.
     893                 :            :  *
     894                 :            :  * @param vc_req
     895                 :            :  *   The vhost crypto data request pointer
     896                 :            :  * @param cur_desc
     897                 :            :  *   The pointer of the current in use descriptor pointer. The content of
     898                 :            :  *   cur_desc is expected to be updated after the function execution.
     899                 :            :  * @param end_wb_data
     900                 :            :  *   The last write back data element to be returned. It is used only in cipher
     901                 :            :  *   and hash chain operations.
     902                 :            :  * @param src
     903                 :            :  *   The source data pointer
     904                 :            :  * @param offset
     905                 :            :  *   The offset to both source and destination data. For source data the offset
     906                 :            :  *   is the number of bytes between src and start point of cipher operation. For
     907                 :            :  *   destination data the offset is the number of bytes from *cur_desc->addr
     908                 :            :  *   to the point where the src will be written to.
     909                 :            :  * @param write_back_len
     910                 :            :  *   The size of the write back length.
     911                 :            :  * @return
     912                 :            :  *   The pointer to the start of the write back data linked list.
     913                 :            :  */
     914                 :            : static __rte_always_inline struct vhost_crypto_writeback_data *
     915                 :            : prepare_write_back_data(struct vhost_virtqueue *vq,
     916                 :            :                 struct vhost_crypto_data_req *vc_req,
     917                 :            :                 struct vhost_crypto_desc *head_desc,
     918                 :            :                 struct vhost_crypto_desc **cur_desc,
     919                 :            :                 struct vhost_crypto_writeback_data **end_wb_data,
     920                 :            :                 uint8_t *src,
     921                 :            :                 uint32_t offset,
     922                 :            :                 uint64_t write_back_len,
     923                 :            :                 uint32_t max_n_descs)
     924                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
     925                 :            : {
     926                 :            :         struct vhost_crypto_writeback_data *wb_data, *head;
     927                 :            :         struct vhost_crypto_desc *desc = *cur_desc;
     928                 :            :         uint64_t dlen;
     929                 :            :         uint8_t *dst;
     930                 :            :         int ret;
     931                 :            : 
     932                 :          0 :         ret = rte_mempool_get(vc_req->wb_pool, (void **)&head);
     933   [ #  #  #  #  :          0 :         if (unlikely(ret < 0)) {
          #  #  #  #  #  
                #  #  # ]
     934                 :          0 :                 VC_LOG_ERR("no memory");
     935                 :          0 :                 goto error_exit;
     936                 :            :         }
     937                 :            : 
     938                 :          0 :         wb_data = head;
     939                 :            : 
     940   [ #  #  #  #  :          0 :         if (likely(desc->len > offset)) {
          #  #  #  #  #  
                #  #  # ]
     941                 :          0 :                 wb_data->src = src + offset;
     942                 :          0 :                 dlen = desc->len;
     943   [ #  #  #  #  :          0 :                 dst = IOVA_TO_VVA(uint8_t *, vc_req->dev, vq,
          #  #  #  #  #  
                #  #  # ]
     944                 :            :                         desc->addr, &dlen, VHOST_ACCESS_RW);
     945   [ #  #  #  #  :          0 :                 if (unlikely(!dst || dlen != desc->len)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     946                 :          0 :                         VC_LOG_ERR("Failed to map descriptor");
     947                 :          0 :                         goto error_exit;
     948                 :            :                 }
     949                 :            : 
     950                 :          0 :                 wb_data->dst = dst + offset;
     951                 :          0 :                 wb_data->len = RTE_MIN(dlen - offset, write_back_len);
     952                 :          0 :                 write_back_len -= wb_data->len;
     953                 :          0 :                 src += offset + wb_data->len;
     954                 :            :                 offset = 0;
     955                 :            : 
     956   [ #  #  #  #  :          0 :                 if (unlikely(write_back_len)) {
          #  #  #  #  #  
                #  #  # ]
     957                 :          0 :                         ret = rte_mempool_get(vc_req->wb_pool,
     958   [ #  #  #  #  :          0 :                                         (void **)&(wb_data->next));
          #  #  #  #  #  
                #  #  # ]
     959   [ #  #  #  #  :          0 :                         if (unlikely(ret < 0)) {
          #  #  #  #  #  
                #  #  # ]
     960                 :          0 :                                 VC_LOG_ERR("no memory");
     961                 :          0 :                                 goto error_exit;
     962                 :            :                         }
     963                 :            : 
     964                 :          0 :                         wb_data = wb_data->next;
     965                 :            :                 } else
     966                 :          0 :                         wb_data->next = NULL;
     967                 :            :         } else
     968                 :          0 :                 offset -= desc->len;
     969                 :            : 
     970                 :          0 :         while (write_back_len &&
     971   [ #  #  #  #  :          0 :                         desc >= head_desc &&
          #  #  #  #  #  
                #  #  # ]
     972   [ #  #  #  #  :          0 :                         desc - head_desc < (int)max_n_descs) {
          #  #  #  #  #  
                #  #  # ]
     973                 :          0 :                 desc++;
     974   [ #  #  #  #  :          0 :                 if (unlikely(!(desc->flags & VRING_DESC_F_WRITE))) {
          #  #  #  #  #  
                #  #  # ]
     975                 :          0 :                         VC_LOG_ERR("incorrect descriptor");
     976                 :          0 :                         goto error_exit;
     977                 :            :                 }
     978                 :            : 
     979   [ #  #  #  #  :          0 :                 if (desc->len <= offset) {
          #  #  #  #  #  
                #  #  # ]
     980                 :          0 :                         offset -= desc->len;
     981                 :          0 :                         continue;
     982                 :            :                 }
     983                 :            : 
     984                 :          0 :                 dlen = desc->len;
     985                 :          0 :                 dst = IOVA_TO_VVA(uint8_t *, vc_req->dev, vq,
     986   [ #  #  #  #  :          0 :                                 desc->addr, &dlen, VHOST_ACCESS_RW) + offset;
          #  #  #  #  #  
                #  #  # ]
     987   [ #  #  #  #  :          0 :                 if (unlikely(dst == NULL || dlen != desc->len)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     988                 :          0 :                         VC_LOG_ERR("Failed to map descriptor");
     989                 :          0 :                         goto error_exit;
     990                 :            :                 }
     991                 :            : 
     992                 :          0 :                 wb_data->src = src + offset;
     993                 :          0 :                 wb_data->dst = dst;
     994                 :          0 :                 wb_data->len = RTE_MIN(desc->len - offset, write_back_len);
     995                 :          0 :                 write_back_len -= wb_data->len;
     996                 :          0 :                 src += wb_data->len;
     997                 :            :                 offset = 0;
     998                 :            : 
     999   [ #  #  #  #  :          0 :                 if (write_back_len) {
          #  #  #  #  #  
                #  #  # ]
    1000                 :          0 :                         ret = rte_mempool_get(vc_req->wb_pool,
    1001   [ #  #  #  #  :          0 :                                         (void **)&(wb_data->next));
          #  #  #  #  #  
                #  #  # ]
    1002   [ #  #  #  #  :          0 :                         if (unlikely(ret < 0)) {
          #  #  #  #  #  
                #  #  # ]
    1003                 :          0 :                                 VC_LOG_ERR("no memory");
    1004                 :          0 :                                 goto error_exit;
    1005                 :            :                         }
    1006                 :            : 
    1007                 :          0 :                         wb_data = wb_data->next;
    1008                 :            :                 } else
    1009                 :          0 :                         wb_data->next = NULL;
    1010                 :            :         }
    1011                 :            : 
    1012   [ #  #  #  #  :          0 :         if (unlikely(desc - head_desc == (int)max_n_descs))
          #  #  #  #  #  
                #  #  # ]
    1013                 :            :                 *cur_desc = NULL;
    1014                 :            :         else
    1015                 :          0 :                 *cur_desc = desc + 1;
    1016                 :            : 
    1017                 :            :         *end_wb_data = wb_data;
    1018                 :            : 
    1019                 :          0 :         return head;
    1020                 :            : 
    1021                 :          0 : error_exit:
    1022   [ #  #  #  #  :          0 :         if (head)
          #  #  #  #  #  
                #  #  # ]
    1023                 :          0 :                 free_wb_data(head, vc_req->wb_pool);
    1024                 :            : 
    1025                 :            :         return NULL;
    1026                 :            : }
    1027                 :            : 
    1028                 :            : static __rte_always_inline uint8_t
    1029                 :            : vhost_crypto_check_cipher_request(struct virtio_crypto_cipher_data_req *req)
    1030                 :            : {
    1031   [ #  #  #  #  :          0 :         if (likely((req->para.iv_len <= VHOST_CRYPTO_MAX_IV_LEN) &&
          #  #  #  #  #  
                #  #  # ]
    1032                 :            :                 (req->para.src_data_len <= RTE_MBUF_DEFAULT_BUF_SIZE) &&
    1033                 :            :                 (req->para.dst_data_len >= req->para.src_data_len) &&
    1034                 :            :                 (req->para.dst_data_len <= RTE_MBUF_DEFAULT_BUF_SIZE)))
    1035                 :          0 :                 return VIRTIO_CRYPTO_OK;
    1036                 :            :         return VIRTIO_CRYPTO_BADMSG;
    1037                 :            : }
    1038                 :            : 
    1039                 :            : static __rte_always_inline uint8_t
    1040                 :            : prepare_sym_cipher_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
    1041                 :            :                 struct vhost_virtqueue *vq,
    1042                 :            :                 struct vhost_crypto_data_req *vc_req,
    1043                 :            :                 struct virtio_crypto_cipher_data_req *cipher,
    1044                 :            :                 struct vhost_crypto_desc *head,
    1045                 :            :                 uint32_t max_n_descs)
    1046                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1047                 :            : {
    1048                 :            :         struct vhost_crypto_desc *desc = head;
    1049                 :            :         struct vhost_crypto_writeback_data *ewb = NULL;
    1050                 :          0 :         struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
    1051                 :          0 :         uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
    1052                 :            :         uint8_t ret = vhost_crypto_check_cipher_request(cipher);
    1053                 :            : 
    1054   [ #  #  #  # ]:          0 :         if (unlikely(ret != VIRTIO_CRYPTO_OK))
    1055                 :          0 :                 goto error_exit;
    1056                 :            : 
    1057                 :            :         /* prepare */
    1058                 :            :         /* iv */
    1059   [ #  #  #  # ]:          0 :         if (unlikely(copy_data(iv_data, vcrypto->dev, vq, head, &desc,
    1060                 :            :                         cipher->para.iv_len, max_n_descs))) {
    1061                 :          0 :                 VC_LOG_ERR("Incorrect virtio descriptor");
    1062                 :            :                 ret = VIRTIO_CRYPTO_BADMSG;
    1063                 :          0 :                 goto error_exit;
    1064                 :            :         }
    1065                 :            : 
    1066   [ #  #  #  #  :          0 :         switch (vcrypto->option) {
                   #  # ]
    1067                 :          0 :         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
    1068                 :          0 :                 m_src->data_len = cipher->para.src_data_len;
    1069                 :          0 :                 rte_mbuf_iova_set(m_src,
    1070   [ #  #  #  # ]:          0 :                                   gpa_to_hpa(vcrypto->dev, desc->addr, cipher->para.src_data_len));
    1071                 :          0 :                 m_src->buf_addr = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RO);
    1072   [ #  #  #  #  :          0 :                 if (unlikely(rte_mbuf_iova_get(m_src) == 0 || m_src->buf_addr == NULL)) {
             #  #  #  # ]
    1073                 :          0 :                         VC_LOG_ERR("zero_copy may fail due to cross page data");
    1074                 :            :                         ret = VIRTIO_CRYPTO_ERR;
    1075                 :          0 :                         goto error_exit;
    1076                 :            :                 }
    1077                 :            : 
    1078   [ #  #  #  # ]:          0 :                 if (unlikely(move_desc(head, &desc, cipher->para.src_data_len,
    1079                 :            :                                 max_n_descs) < 0)) {
    1080                 :          0 :                         VC_LOG_ERR("Incorrect descriptor");
    1081                 :            :                         ret = VIRTIO_CRYPTO_ERR;
    1082                 :          0 :                         goto error_exit;
    1083                 :            :                 }
    1084                 :            : 
    1085                 :            :                 break;
    1086                 :          0 :         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
    1087                 :          0 :                 vc_req->wb_pool = vcrypto->wb_pool;
    1088                 :          0 :                 m_src->data_len = cipher->para.src_data_len;
    1089   [ #  #  #  # ]:          0 :                 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
    1090                 :            :                                 vcrypto->dev, vq, head, &desc,
    1091                 :            :                                 cipher->para.src_data_len, max_n_descs) < 0)) {
    1092                 :          0 :                         VC_LOG_ERR("Incorrect virtio descriptor");
    1093                 :            :                         ret = VIRTIO_CRYPTO_BADMSG;
    1094                 :          0 :                         goto error_exit;
    1095                 :            :                 }
    1096                 :            :                 break;
    1097                 :          0 :         default:
    1098                 :            :                 ret = VIRTIO_CRYPTO_BADMSG;
    1099                 :          0 :                 goto error_exit;
    1100                 :            :         }
    1101                 :            : 
    1102                 :            :         /* dst */
    1103                 :            :         desc = find_write_desc(head, desc, max_n_descs);
    1104   [ #  #  #  # ]:          0 :         if (unlikely(!desc)) {
    1105                 :          0 :                 VC_LOG_ERR("Cannot find write location");
    1106                 :            :                 ret = VIRTIO_CRYPTO_BADMSG;
    1107                 :          0 :                 goto error_exit;
    1108                 :            :         }
    1109                 :            : 
    1110   [ #  #  #  #  :          0 :         switch (vcrypto->option) {
                   #  # ]
    1111                 :          0 :         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
    1112                 :          0 :                 rte_mbuf_iova_set(m_dst,
    1113   [ #  #  #  # ]:          0 :                                   gpa_to_hpa(vcrypto->dev, desc->addr, cipher->para.dst_data_len));
    1114                 :          0 :                 m_dst->buf_addr = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RW);
    1115   [ #  #  #  #  :          0 :                 if (unlikely(rte_mbuf_iova_get(m_dst) == 0 || m_dst->buf_addr == NULL)) {
             #  #  #  # ]
    1116                 :          0 :                         VC_LOG_ERR("zero_copy may fail due to cross page data");
    1117                 :            :                         ret = VIRTIO_CRYPTO_ERR;
    1118                 :          0 :                         goto error_exit;
    1119                 :            :                 }
    1120                 :            : 
    1121   [ #  #  #  # ]:          0 :                 if (unlikely(move_desc(head, &desc, cipher->para.dst_data_len,
    1122                 :            :                                 max_n_descs) < 0)) {
    1123                 :          0 :                         VC_LOG_ERR("Incorrect descriptor");
    1124                 :            :                         ret = VIRTIO_CRYPTO_ERR;
    1125                 :          0 :                         goto error_exit;
    1126                 :            :                 }
    1127                 :            : 
    1128                 :          0 :                 m_dst->data_len = cipher->para.dst_data_len;
    1129                 :          0 :                 break;
    1130                 :          0 :         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
    1131                 :          0 :                 vc_req->wb = prepare_write_back_data(vq, vc_req, head, &desc, &ewb,
    1132                 :          0 :                                 rte_pktmbuf_mtod(m_src, uint8_t *), 0,
    1133   [ #  #  #  # ]:          0 :                                 cipher->para.dst_data_len, max_n_descs);
    1134   [ #  #  #  # ]:          0 :                 if (unlikely(vc_req->wb == NULL)) {
    1135                 :            :                         ret = VIRTIO_CRYPTO_ERR;
    1136                 :          0 :                         goto error_exit;
    1137                 :            :                 }
    1138                 :            : 
    1139                 :            :                 break;
    1140                 :          0 :         default:
    1141                 :            :                 ret = VIRTIO_CRYPTO_BADMSG;
    1142                 :          0 :                 goto error_exit;
    1143                 :            :         }
    1144                 :            : 
    1145                 :            :         /* src data */
    1146                 :          0 :         op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
    1147                 :          0 :         op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
    1148                 :            : 
    1149                 :          0 :         op->sym->cipher.data.offset = 0;
    1150   [ #  #  #  # ]:          0 :         op->sym->cipher.data.length = cipher->para.src_data_len;
    1151                 :            : 
    1152                 :          0 :         vc_req->inhdr = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_WO);
    1153   [ #  #  #  # ]:          0 :         if (unlikely(vc_req->inhdr == NULL)) {
    1154                 :            :                 ret = VIRTIO_CRYPTO_BADMSG;
    1155                 :          0 :                 goto error_exit;
    1156                 :            :         }
    1157                 :            : 
    1158                 :          0 :         vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
    1159                 :          0 :         vc_req->len = cipher->para.dst_data_len + INHDR_LEN;
    1160                 :            : 
    1161                 :          0 :         return 0;
    1162                 :            : 
    1163                 :          0 : error_exit:
    1164   [ #  #  #  # ]:          0 :         if (vc_req->wb)
    1165                 :          0 :                 free_wb_data(vc_req->wb, vc_req->wb_pool);
    1166                 :            : 
    1167                 :          0 :         vc_req->len = INHDR_LEN;
    1168                 :          0 :         return ret;
    1169                 :            : }
    1170                 :            : 
    1171                 :            : static __rte_always_inline uint8_t
    1172                 :            : vhost_crypto_check_chain_request(struct virtio_crypto_alg_chain_data_req *req)
    1173                 :            : {
    1174   [ #  #  #  #  :          0 :         if (likely((req->para.iv_len <= VHOST_CRYPTO_MAX_IV_LEN) &&
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
    1175                 :            :                 (req->para.src_data_len <= VHOST_CRYPTO_MAX_DATA_SIZE) &&
    1176                 :            :                 (req->para.dst_data_len >= req->para.src_data_len) &&
    1177                 :            :                 (req->para.dst_data_len <= VHOST_CRYPTO_MAX_DATA_SIZE) &&
    1178                 :            :                 (req->para.cipher_start_src_offset <
    1179                 :            :                         VHOST_CRYPTO_MAX_DATA_SIZE) &&
    1180                 :            :                 (req->para.len_to_cipher <= VHOST_CRYPTO_MAX_DATA_SIZE) &&
    1181                 :            :                 (req->para.hash_start_src_offset <
    1182                 :            :                         VHOST_CRYPTO_MAX_DATA_SIZE) &&
    1183                 :            :                 (req->para.len_to_hash <= VHOST_CRYPTO_MAX_DATA_SIZE) &&
    1184                 :            :                 (req->para.cipher_start_src_offset + req->para.len_to_cipher <=
    1185                 :            :                         req->para.src_data_len) &&
    1186                 :            :                 (req->para.hash_start_src_offset + req->para.len_to_hash <=
    1187                 :            :                         req->para.src_data_len) &&
    1188                 :            :                 (req->para.dst_data_len + req->para.hash_result_len <=
    1189                 :            :                         VHOST_CRYPTO_MAX_DATA_SIZE)))
    1190                 :          0 :                 return VIRTIO_CRYPTO_OK;
    1191                 :            :         return VIRTIO_CRYPTO_BADMSG;
    1192                 :            : }
    1193                 :            : 
    1194                 :            : static __rte_always_inline uint8_t
    1195                 :            : prepare_sym_chain_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
    1196                 :            :                 struct vhost_virtqueue *vq,
    1197                 :            :                 struct vhost_crypto_data_req *vc_req,
    1198                 :            :                 struct virtio_crypto_alg_chain_data_req *chain,
    1199                 :            :                 struct vhost_crypto_desc *head,
    1200                 :            :                 uint32_t max_n_descs)
    1201                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1202                 :            : {
    1203                 :            :         struct vhost_crypto_desc *desc = head, *digest_desc;
    1204                 :            :         struct vhost_crypto_writeback_data *ewb = NULL, *ewb2 = NULL;
    1205                 :          0 :         struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
    1206                 :          0 :         uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
    1207                 :            :         uint32_t digest_offset;
    1208                 :            :         void *digest_addr;
    1209                 :            :         uint8_t ret = vhost_crypto_check_chain_request(chain);
    1210                 :            : 
    1211   [ #  #  #  # ]:          0 :         if (unlikely(ret != VIRTIO_CRYPTO_OK))
    1212                 :          0 :                 goto error_exit;
    1213                 :            : 
    1214                 :            :         /* prepare */
    1215                 :            :         /* iv */
    1216   [ #  #  #  # ]:          0 :         if (unlikely(copy_data(iv_data, vcrypto->dev, vq, head, &desc,
    1217                 :            :                         chain->para.iv_len, max_n_descs) < 0)) {
    1218                 :          0 :                 VC_LOG_ERR("Incorrect virtio descriptor");
    1219                 :            :                 ret = VIRTIO_CRYPTO_BADMSG;
    1220                 :          0 :                 goto error_exit;
    1221                 :            :         }
    1222                 :            : 
    1223   [ #  #  #  #  :          0 :         switch (vcrypto->option) {
                   #  # ]
    1224                 :          0 :         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
    1225                 :          0 :                 m_src->data_len = chain->para.src_data_len;
    1226                 :          0 :                 m_dst->data_len = chain->para.dst_data_len;
    1227                 :            : 
    1228                 :          0 :                 rte_mbuf_iova_set(m_src,
    1229   [ #  #  #  # ]:          0 :                                   gpa_to_hpa(vcrypto->dev, desc->addr, chain->para.src_data_len));
    1230                 :          0 :                 m_src->buf_addr = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RO);
    1231   [ #  #  #  #  :          0 :                 if (unlikely(rte_mbuf_iova_get(m_src) == 0 || m_src->buf_addr == NULL)) {
             #  #  #  # ]
    1232                 :          0 :                         VC_LOG_ERR("zero_copy may fail due to cross page data");
    1233                 :            :                         ret = VIRTIO_CRYPTO_ERR;
    1234                 :          0 :                         goto error_exit;
    1235                 :            :                 }
    1236                 :            : 
    1237   [ #  #  #  # ]:          0 :                 if (unlikely(move_desc(head, &desc, chain->para.src_data_len,
    1238                 :            :                                 max_n_descs) < 0)) {
    1239                 :          0 :                         VC_LOG_ERR("Incorrect descriptor");
    1240                 :            :                         ret = VIRTIO_CRYPTO_ERR;
    1241                 :          0 :                         goto error_exit;
    1242                 :            :                 }
    1243                 :            :                 break;
    1244                 :          0 :         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
    1245                 :          0 :                 vc_req->wb_pool = vcrypto->wb_pool;
    1246                 :          0 :                 m_src->data_len = chain->para.src_data_len;
    1247   [ #  #  #  # ]:          0 :                 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
    1248                 :            :                                 vcrypto->dev, vq, head, &desc,
    1249                 :            :                                 chain->para.src_data_len, max_n_descs) < 0)) {
    1250                 :          0 :                         VC_LOG_ERR("Incorrect virtio descriptor");
    1251                 :            :                         ret = VIRTIO_CRYPTO_BADMSG;
    1252                 :          0 :                         goto error_exit;
    1253                 :            :                 }
    1254                 :            : 
    1255                 :            :                 break;
    1256                 :          0 :         default:
    1257                 :            :                 ret = VIRTIO_CRYPTO_BADMSG;
    1258                 :          0 :                 goto error_exit;
    1259                 :            :         }
    1260                 :            : 
    1261                 :            :         /* dst */
    1262                 :            :         desc = find_write_desc(head, desc, max_n_descs);
    1263   [ #  #  #  # ]:          0 :         if (unlikely(!desc)) {
    1264                 :          0 :                 VC_LOG_ERR("Cannot find write location");
    1265                 :            :                 ret = VIRTIO_CRYPTO_BADMSG;
    1266                 :          0 :                 goto error_exit;
    1267                 :            :         }
    1268                 :            : 
    1269   [ #  #  #  #  :          0 :         switch (vcrypto->option) {
                   #  # ]
    1270                 :          0 :         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
    1271                 :          0 :                 rte_mbuf_iova_set(m_dst,
    1272   [ #  #  #  # ]:          0 :                                   gpa_to_hpa(vcrypto->dev, desc->addr, chain->para.dst_data_len));
    1273                 :          0 :                 m_dst->buf_addr = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RW);
    1274   [ #  #  #  #  :          0 :                 if (unlikely(rte_mbuf_iova_get(m_dst) == 0 || m_dst->buf_addr == NULL)) {
             #  #  #  # ]
    1275                 :          0 :                         VC_LOG_ERR("zero_copy may fail due to cross page data");
    1276                 :            :                         ret = VIRTIO_CRYPTO_ERR;
    1277                 :          0 :                         goto error_exit;
    1278                 :            :                 }
    1279                 :            : 
    1280   [ #  #  #  # ]:          0 :                 if (unlikely(move_desc(vc_req->head, &desc,
    1281                 :            :                                 chain->para.dst_data_len, max_n_descs) < 0)) {
    1282                 :          0 :                         VC_LOG_ERR("Incorrect descriptor");
    1283                 :            :                         ret = VIRTIO_CRYPTO_ERR;
    1284                 :          0 :                         goto error_exit;
    1285                 :            :                 }
    1286                 :            : 
    1287   [ #  #  #  # ]:          0 :                 op->sym->auth.digest.phys_addr = gpa_to_hpa(vcrypto->dev,
    1288   [ #  #  #  # ]:          0 :                                 desc->addr, chain->para.hash_result_len);
    1289                 :          0 :                 op->sym->auth.digest.data = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RW);
    1290   [ #  #  #  # ]:          0 :                 if (unlikely(op->sym->auth.digest.phys_addr == 0)) {
    1291                 :          0 :                         VC_LOG_ERR("zero_copy may fail due to cross page data");
    1292                 :            :                         ret = VIRTIO_CRYPTO_ERR;
    1293                 :          0 :                         goto error_exit;
    1294                 :            :                 }
    1295                 :            : 
    1296   [ #  #  #  # ]:          0 :                 if (unlikely(move_desc(head, &desc,
    1297                 :            :                                 chain->para.hash_result_len,
    1298                 :            :                                 max_n_descs) < 0)) {
    1299                 :          0 :                         VC_LOG_ERR("Incorrect descriptor");
    1300                 :            :                         ret = VIRTIO_CRYPTO_ERR;
    1301                 :          0 :                         goto error_exit;
    1302                 :            :                 }
    1303                 :            : 
    1304                 :            :                 break;
    1305                 :          0 :         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
    1306                 :          0 :                 vc_req->wb = prepare_write_back_data(vq, vc_req, head, &desc, &ewb,
    1307                 :          0 :                                 rte_pktmbuf_mtod(m_src, uint8_t *),
    1308                 :            :                                 chain->para.cipher_start_src_offset,
    1309                 :          0 :                                 chain->para.dst_data_len -
    1310   [ #  #  #  # ]:          0 :                                         chain->para.cipher_start_src_offset,
    1311                 :            :                                 max_n_descs);
    1312   [ #  #  #  # ]:          0 :                 if (unlikely(vc_req->wb == NULL)) {
    1313                 :            :                         ret = VIRTIO_CRYPTO_ERR;
    1314                 :          0 :                         goto error_exit;
    1315                 :            :                 }
    1316                 :            : 
    1317                 :            :                 digest_desc = desc;
    1318                 :          0 :                 digest_offset = m_src->data_len;
    1319                 :          0 :                 digest_addr = rte_pktmbuf_mtod_offset(m_src, void *,
    1320                 :            :                                 digest_offset);
    1321                 :            : 
    1322                 :            :                 /** create a wb_data for digest */
    1323                 :          0 :                 ewb->next = prepare_write_back_data(vq, vc_req, head, &desc,
    1324                 :            :                                 &ewb2, digest_addr, 0,
    1325   [ #  #  #  # ]:          0 :                                 chain->para.hash_result_len, max_n_descs);
    1326   [ #  #  #  # ]:          0 :                 if (unlikely(ewb->next == NULL)) {
    1327                 :            :                         ret = VIRTIO_CRYPTO_ERR;
    1328                 :          0 :                         goto error_exit;
    1329                 :            :                 }
    1330                 :            : 
    1331   [ #  #  #  # ]:          0 :                 if (unlikely(copy_data(digest_addr, vcrypto->dev, vq, head,
    1332                 :            :                                 &digest_desc, chain->para.hash_result_len,
    1333                 :            :                                 max_n_descs) < 0)) {
    1334                 :          0 :                         VC_LOG_ERR("Incorrect virtio descriptor");
    1335                 :            :                         ret = VIRTIO_CRYPTO_BADMSG;
    1336                 :          0 :                         goto error_exit;
    1337                 :            :                 }
    1338                 :            : 
    1339                 :          0 :                 op->sym->auth.digest.data = digest_addr;
    1340                 :          0 :                 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m_src,
    1341                 :            :                                 digest_offset);
    1342                 :          0 :                 break;
    1343                 :          0 :         default:
    1344                 :            :                 ret = VIRTIO_CRYPTO_BADMSG;
    1345                 :          0 :                 goto error_exit;
    1346                 :            :         }
    1347                 :            : 
    1348                 :            :         /* record inhdr */
    1349                 :          0 :         vc_req->inhdr = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_WO);
    1350   [ #  #  #  # ]:          0 :         if (unlikely(vc_req->inhdr == NULL)) {
    1351                 :            :                 ret = VIRTIO_CRYPTO_BADMSG;
    1352                 :          0 :                 goto error_exit;
    1353                 :            :         }
    1354                 :            : 
    1355                 :          0 :         vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
    1356                 :            : 
    1357                 :          0 :         op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
    1358                 :          0 :         op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
    1359                 :            : 
    1360                 :          0 :         op->sym->cipher.data.offset = chain->para.cipher_start_src_offset;
    1361                 :          0 :         op->sym->cipher.data.length = chain->para.src_data_len -
    1362                 :            :                         chain->para.cipher_start_src_offset;
    1363                 :            : 
    1364                 :          0 :         op->sym->auth.data.offset = chain->para.hash_start_src_offset;
    1365                 :          0 :         op->sym->auth.data.length = chain->para.len_to_hash;
    1366                 :            : 
    1367                 :          0 :         vc_req->len = chain->para.dst_data_len + chain->para.hash_result_len +
    1368                 :            :                         INHDR_LEN;
    1369                 :          0 :         return 0;
    1370                 :            : 
    1371                 :          0 : error_exit:
    1372   [ #  #  #  # ]:          0 :         if (vc_req->wb)
    1373                 :          0 :                 free_wb_data(vc_req->wb, vc_req->wb_pool);
    1374                 :          0 :         vc_req->len = INHDR_LEN;
    1375                 :          0 :         return ret;
    1376                 :            : }
    1377                 :            : 
    1378                 :            : static __rte_always_inline uint8_t
    1379                 :            : prepare_asym_rsa_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
    1380                 :            :                 struct vhost_virtqueue *vq,
    1381                 :            :                 struct vhost_crypto_data_req *vc_req,
    1382                 :            :                 struct virtio_crypto_op_data_req *req,
    1383                 :            :                 struct vhost_crypto_desc *head,
    1384                 :            :                 uint32_t max_n_descs)
    1385                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1386                 :            : {
    1387                 :            :         struct rte_crypto_rsa_op_param *rsa = &op->asym->rsa;
    1388                 :            :         struct vhost_crypto_desc *desc = head;
    1389                 :            :         uint8_t ret = VIRTIO_CRYPTO_ERR;
    1390                 :            :         uint16_t wlen = 0;
    1391                 :            : 
    1392                 :            :         /* prepare */
    1393                 :          0 :         switch (vcrypto->option) {
    1394                 :          0 :         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
    1395                 :          0 :                 vc_req->wb_pool = vcrypto->wb_pool;
    1396   [ #  #  #  # ]:          0 :                 if (req->header.opcode == VIRTIO_CRYPTO_AKCIPHER_SIGN) {
    1397   [ #  #  #  # ]:          0 :                         rsa->op_type = RTE_CRYPTO_ASYM_OP_SIGN;
    1398                 :          0 :                         rsa->message.data = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RO);
    1399                 :          0 :                         rsa->message.length = req->u.akcipher_req.para.src_data_len;
    1400                 :          0 :                         rsa->sign.length = req->u.akcipher_req.para.dst_data_len;
    1401                 :          0 :                         wlen = rsa->sign.length;
    1402                 :            :                         desc = find_write_desc(head, desc, max_n_descs);
    1403   [ #  #  #  # ]:          0 :                         if (unlikely(!desc)) {
    1404                 :          0 :                                 VC_LOG_ERR("Cannot find write location");
    1405                 :            :                                 ret = VIRTIO_CRYPTO_BADMSG;
    1406                 :          0 :                                 goto error_exit;
    1407                 :            :                         }
    1408                 :            : 
    1409                 :          0 :                         rsa->sign.data = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RW);
    1410   [ #  #  #  # ]:          0 :                         if (unlikely(rsa->sign.data == NULL)) {
    1411                 :            :                                 ret = VIRTIO_CRYPTO_ERR;
    1412                 :          0 :                                 goto error_exit;
    1413                 :            :                         }
    1414                 :            : 
    1415                 :          0 :                         desc += 1;
    1416   [ #  #  #  # ]:          0 :                 } else if (req->header.opcode == VIRTIO_CRYPTO_AKCIPHER_VERIFY) {
    1417   [ #  #  #  # ]:          0 :                         rsa->op_type = RTE_CRYPTO_ASYM_OP_VERIFY;
    1418                 :          0 :                         rsa->sign.data = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RO);
    1419   [ #  #  #  # ]:          0 :                         rsa->sign.length = req->u.akcipher_req.para.src_data_len;
    1420                 :            :                         desc += 1;
    1421                 :          0 :                         rsa->message.data = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RO);
    1422                 :          0 :                         rsa->message.length = req->u.akcipher_req.para.dst_data_len;
    1423                 :          0 :                         desc += 1;
    1424   [ #  #  #  # ]:          0 :                 } else if (req->header.opcode == VIRTIO_CRYPTO_AKCIPHER_ENCRYPT) {
    1425   [ #  #  #  # ]:          0 :                         rsa->op_type = RTE_CRYPTO_ASYM_OP_ENCRYPT;
    1426                 :          0 :                         rsa->message.data = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RO);
    1427                 :          0 :                         rsa->message.length = req->u.akcipher_req.para.src_data_len;
    1428                 :          0 :                         rsa->cipher.length = req->u.akcipher_req.para.dst_data_len;
    1429                 :          0 :                         wlen = rsa->cipher.length;
    1430                 :            :                         desc = find_write_desc(head, desc, max_n_descs);
    1431   [ #  #  #  # ]:          0 :                         if (unlikely(!desc)) {
    1432                 :          0 :                                 VC_LOG_ERR("Cannot find write location");
    1433                 :            :                                 ret = VIRTIO_CRYPTO_BADMSG;
    1434                 :          0 :                                 goto error_exit;
    1435                 :            :                         }
    1436                 :            : 
    1437                 :          0 :                         rsa->cipher.data = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RW);
    1438   [ #  #  #  # ]:          0 :                         if (unlikely(rsa->cipher.data == NULL)) {
    1439                 :            :                                 ret = VIRTIO_CRYPTO_ERR;
    1440                 :          0 :                                 goto error_exit;
    1441                 :            :                         }
    1442                 :            : 
    1443                 :          0 :                         desc += 1;
    1444                 :            :                 } else if (req->header.opcode == VIRTIO_CRYPTO_AKCIPHER_DECRYPT) {
    1445   [ #  #  #  # ]:          0 :                         rsa->op_type = RTE_CRYPTO_ASYM_OP_DECRYPT;
    1446                 :          0 :                         rsa->cipher.data = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RO);
    1447   [ #  #  #  # ]:          0 :                         rsa->cipher.length = req->u.akcipher_req.para.src_data_len;
    1448                 :            :                         desc += 1;
    1449                 :          0 :                         rsa->message.data = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RO);
    1450                 :          0 :                         rsa->message.length = req->u.akcipher_req.para.dst_data_len;
    1451                 :          0 :                         desc += 1;
    1452                 :            :                 } else {
    1453                 :            :                         goto error_exit;
    1454                 :            :                 }
    1455                 :            :                 break;
    1456                 :          0 :         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
    1457                 :            :         default:
    1458                 :            :                 ret = VIRTIO_CRYPTO_BADMSG;
    1459                 :          0 :                 goto error_exit;
    1460                 :            :         }
    1461                 :            : 
    1462                 :          0 :         op->type = RTE_CRYPTO_OP_TYPE_ASYMMETRIC;
    1463   [ #  #  #  # ]:          0 :         op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
    1464                 :            : 
    1465                 :          0 :         vc_req->inhdr = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_WO);
    1466   [ #  #  #  # ]:          0 :         if (unlikely(vc_req->inhdr == NULL)) {
    1467                 :            :                 ret = VIRTIO_CRYPTO_BADMSG;
    1468                 :          0 :                 goto error_exit;
    1469                 :            :         }
    1470                 :            : 
    1471                 :          0 :         vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
    1472                 :          0 :         vc_req->len = wlen + INHDR_LEN;
    1473                 :          0 :         return 0;
    1474                 :          0 : error_exit:
    1475   [ #  #  #  # ]:          0 :         if (vc_req->wb)
    1476                 :          0 :                 free_wb_data(vc_req->wb, vc_req->wb_pool);
    1477                 :            : 
    1478                 :          0 :         vc_req->len = INHDR_LEN;
    1479                 :          0 :         return ret;
    1480                 :            : }
    1481                 :            : 
    1482                 :            : /**
    1483                 :            :  * Process on descriptor
    1484                 :            :  */
    1485                 :            : static __rte_always_inline int
    1486                 :            : vhost_crypto_process_one_req(struct vhost_crypto *vcrypto,
    1487                 :            :                 struct vhost_virtqueue *vq, struct rte_crypto_op *op,
    1488                 :            :                 struct vring_desc *head, struct vhost_crypto_desc *descs,
    1489                 :            :                 uint16_t desc_idx)
    1490                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1491                 :            : {
    1492                 :            :         struct vhost_crypto_data_req *vc_req, *vc_req_out;
    1493                 :            :         struct rte_cryptodev_asym_session *asym_session;
    1494                 :            :         struct rte_cryptodev_sym_session *sym_session;
    1495                 :            :         struct vhost_crypto_session *vhost_session;
    1496                 :            :         struct vhost_crypto_desc *desc = descs;
    1497                 :            :         uint32_t nb_descs = 0, max_n_descs, i;
    1498                 :            :         struct vhost_crypto_data_req data_req;
    1499                 :            :         struct virtio_crypto_op_data_req req;
    1500                 :            :         struct virtio_crypto_inhdr *inhdr;
    1501                 :            :         struct vring_desc *src_desc;
    1502                 :            :         uint64_t session_id;
    1503                 :            :         uint64_t dlen;
    1504                 :            :         int err;
    1505                 :            : 
    1506                 :            :         vc_req = &data_req;
    1507                 :          0 :         vc_req->desc_idx = desc_idx;
    1508                 :          0 :         vc_req->dev = vcrypto->dev;
    1509                 :          0 :         vc_req->vq = vq;
    1510                 :            : 
    1511                 :          0 :         if (unlikely((head->flags & VRING_DESC_F_INDIRECT) == 0)) {
    1512                 :          0 :                 VC_LOG_ERR("Invalid descriptor");
    1513                 :          0 :                 return -1;
    1514                 :            :         }
    1515                 :            : 
    1516                 :          0 :         dlen = head->len;
    1517   [ #  #  #  # ]:          0 :         src_desc = IOVA_TO_VVA(struct vring_desc *, vc_req->dev, vq,
    1518                 :            :                         head->addr, &dlen, VHOST_ACCESS_RO);
    1519   [ #  #  #  #  :          0 :         if (unlikely(!src_desc || dlen != head->len)) {
             #  #  #  # ]
    1520                 :          0 :                 VC_LOG_ERR("Invalid descriptor");
    1521                 :          0 :                 return -1;
    1522                 :            :         }
    1523                 :            :         head = src_desc;
    1524                 :            : 
    1525                 :          0 :         nb_descs = max_n_descs = dlen / sizeof(struct vring_desc);
    1526   [ #  #  #  # ]:          0 :         if (unlikely(nb_descs > VHOST_CRYPTO_MAX_N_DESC || nb_descs == 0)) {
    1527                 :            :                 err = VIRTIO_CRYPTO_ERR;
    1528                 :          0 :                 VC_LOG_ERR("Cannot process num of descriptors %u", nb_descs);
    1529   [ #  #  #  # ]:          0 :                 if (nb_descs > 0) {
    1530                 :            :                         struct vring_desc *inhdr_desc = head;
    1531   [ #  #  #  # ]:          0 :                         while (inhdr_desc->flags & VRING_DESC_F_NEXT) {
    1532   [ #  #  #  # ]:          0 :                                 if (inhdr_desc->next >= max_n_descs)
    1533                 :            :                                         return -1;
    1534                 :          0 :                                 inhdr_desc = &head[inhdr_desc->next];
    1535                 :            :                         }
    1536   [ #  #  #  # ]:          0 :                         if (inhdr_desc->len != sizeof(*inhdr))
    1537                 :            :                                 return -1;
    1538   [ #  #  #  # ]:          0 :                         inhdr = IOVA_TO_VVA(struct virtio_crypto_inhdr *, vc_req->dev,
    1539                 :            :                                         vq, inhdr_desc->addr, &dlen,
    1540                 :            :                                         VHOST_ACCESS_WO);
    1541   [ #  #  #  #  :          0 :                         if (unlikely(!inhdr || dlen != inhdr_desc->len))
             #  #  #  # ]
    1542                 :            :                                 return -1;
    1543                 :          0 :                         inhdr->status = VIRTIO_CRYPTO_ERR;
    1544                 :          0 :                         return -1;
    1545                 :            :                 }
    1546                 :            :         }
    1547                 :            : 
    1548                 :            :         /* copy descriptors to local variable */
    1549   [ #  #  #  # ]:          0 :         for (i = 0; i < max_n_descs; i++) {
    1550                 :          0 :                 desc->addr = src_desc->addr;
    1551                 :          0 :                 desc->len = src_desc->len;
    1552                 :          0 :                 desc->flags = src_desc->flags;
    1553                 :          0 :                 desc++;
    1554   [ #  #  #  # ]:          0 :                 if (unlikely((src_desc->flags & VRING_DESC_F_NEXT) == 0))
    1555                 :            :                         break;
    1556   [ #  #  #  # ]:          0 :                 if (unlikely(src_desc->next >= max_n_descs)) {
    1557                 :            :                         err = VIRTIO_CRYPTO_BADMSG;
    1558                 :          0 :                         VC_LOG_ERR("Invalid descriptor");
    1559                 :          0 :                         goto error_exit;
    1560                 :            :                 }
    1561                 :          0 :                 src_desc = &head[src_desc->next];
    1562                 :            :         }
    1563                 :            : 
    1564                 :          0 :         vc_req->head = head;
    1565                 :          0 :         vc_req->zero_copy = vcrypto->option;
    1566                 :            : 
    1567                 :            :         nb_descs = desc - descs;
    1568                 :            :         desc = descs;
    1569                 :            : 
    1570   [ #  #  #  # ]:          0 :         if (unlikely(desc->len < sizeof(req))) {
    1571                 :            :                 err = VIRTIO_CRYPTO_BADMSG;
    1572                 :          0 :                 VC_LOG_ERR("Invalid descriptor");
    1573                 :          0 :                 goto error_exit;
    1574                 :            :         }
    1575                 :            : 
    1576   [ #  #  #  # ]:          0 :         if (unlikely(copy_data(&req, vcrypto->dev, vq, descs, &desc, sizeof(req),
    1577                 :            :                         max_n_descs) < 0)) {
    1578                 :            :                 err = VIRTIO_CRYPTO_BADMSG;
    1579                 :          0 :                 VC_LOG_ERR("Invalid descriptor");
    1580                 :          0 :                 goto error_exit;
    1581                 :            :         }
    1582                 :            : 
    1583                 :            :         /* desc is advanced by 1 now */
    1584                 :            :         max_n_descs -= 1;
    1585                 :            : 
    1586   [ #  #  #  #  :          0 :         switch (req.header.opcode) {
                   #  # ]
    1587                 :          0 :         case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
    1588                 :            :         case VIRTIO_CRYPTO_CIPHER_DECRYPT:
    1589   [ #  #  #  # ]:          0 :                 vc_req_out = rte_mbuf_to_priv(op->sym->m_src);
    1590                 :            :                 memcpy(vc_req_out, vc_req, sizeof(struct vhost_crypto_data_req));
    1591                 :          0 :                 session_id = req.header.session_id;
    1592                 :            : 
    1593                 :            :                 /* one branch to avoid unnecessary table lookup */
    1594   [ #  #  #  # ]:          0 :                 if (vcrypto->cache_sym_session_id != session_id) {
    1595                 :          0 :                         err = rte_hash_lookup_data(vcrypto->session_map,
    1596                 :            :                                         &session_id, (void **)&vhost_session);
    1597   [ #  #  #  # ]:          0 :                         if (unlikely(err < 0)) {
    1598                 :            :                                 err = VIRTIO_CRYPTO_ERR;
    1599                 :          0 :                                 VC_LOG_ERR("Failed to find session %"PRIu64,
    1600                 :            :                                                 session_id);
    1601                 :          0 :                                 goto error_exit;
    1602                 :            :                         }
    1603                 :            : 
    1604                 :          0 :                         vcrypto->cache_sym_session = vhost_session->sym;
    1605                 :          0 :                         vcrypto->cache_sym_session_id = session_id;
    1606                 :            :                 }
    1607                 :            : 
    1608                 :          0 :                 sym_session = vcrypto->cache_sym_session;
    1609                 :          0 :                 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
    1610                 :            : 
    1611                 :          0 :                 err = rte_crypto_op_attach_sym_session(op, sym_session);
    1612   [ #  #  #  # ]:          0 :                 if (unlikely(err < 0)) {
    1613                 :            :                         err = VIRTIO_CRYPTO_ERR;
    1614                 :          0 :                         VC_LOG_ERR("Failed to attach session to op");
    1615                 :          0 :                         goto error_exit;
    1616                 :            :                 }
    1617                 :            : 
    1618   [ #  #  #  #  :          0 :                 switch (req.u.sym_req.op_type) {
             #  #  #  # ]
    1619                 :          0 :                 case VIRTIO_CRYPTO_SYM_OP_NONE:
    1620                 :            :                         err = VIRTIO_CRYPTO_NOTSUPP;
    1621                 :          0 :                         break;
    1622   [ #  #  #  # ]:          0 :                 case VIRTIO_CRYPTO_SYM_OP_CIPHER:
    1623                 :          0 :                         err = prepare_sym_cipher_op(vcrypto, op, vq, vc_req_out,
    1624                 :            :                                         &req.u.sym_req.u.cipher, desc,
    1625                 :            :                                         max_n_descs);
    1626                 :          0 :                         break;
    1627   [ #  #  #  # ]:          0 :                 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
    1628                 :          0 :                         err = prepare_sym_chain_op(vcrypto, op, vq, vc_req_out,
    1629                 :            :                                         &req.u.sym_req.u.chain, desc,
    1630                 :            :                                         max_n_descs);
    1631                 :          0 :                         break;
    1632                 :            :                 }
    1633   [ #  #  #  # ]:          0 :                 if (unlikely(err != 0)) {
    1634                 :          0 :                         VC_LOG_ERR("Failed to process sym request");
    1635                 :          0 :                         goto error_exit;
    1636                 :            :                 }
    1637                 :            :                 break;
    1638                 :          0 :         case VIRTIO_CRYPTO_AKCIPHER_SIGN:
    1639                 :            :         case VIRTIO_CRYPTO_AKCIPHER_VERIFY:
    1640                 :            :         case VIRTIO_CRYPTO_AKCIPHER_ENCRYPT:
    1641                 :            :         case VIRTIO_CRYPTO_AKCIPHER_DECRYPT:
    1642                 :          0 :                 session_id = req.header.session_id;
    1643                 :            : 
    1644                 :            :                 /* one branch to avoid unnecessary table lookup */
    1645   [ #  #  #  # ]:          0 :                 if (vcrypto->cache_asym_session_id != session_id) {
    1646                 :          0 :                         err = rte_hash_lookup_data(vcrypto->session_map,
    1647                 :            :                                         &session_id, (void **)&vhost_session);
    1648   [ #  #  #  # ]:          0 :                         if (unlikely(err < 0)) {
    1649                 :            :                                 err = VIRTIO_CRYPTO_ERR;
    1650                 :          0 :                                 VC_LOG_ERR("Failed to find asym session %"PRIu64,
    1651                 :            :                                                    session_id);
    1652                 :          0 :                                 goto error_exit;
    1653                 :            :                         }
    1654                 :            : 
    1655                 :          0 :                         vcrypto->cache_asym_session = vhost_session->asym;
    1656                 :          0 :                         vcrypto->cache_asym_session_id = session_id;
    1657                 :            :                 }
    1658                 :            : 
    1659                 :          0 :                 asym_session = vcrypto->cache_asym_session;
    1660                 :          0 :                 op->type = RTE_CRYPTO_OP_TYPE_ASYMMETRIC;
    1661                 :            : 
    1662                 :          0 :                 err = rte_crypto_op_attach_asym_session(op, asym_session);
    1663   [ #  #  #  # ]:          0 :                 if (unlikely(err < 0)) {
    1664                 :            :                         err = VIRTIO_CRYPTO_ERR;
    1665                 :          0 :                         VC_LOG_ERR("Failed to attach asym session to op");
    1666                 :          0 :                         goto error_exit;
    1667                 :            :                 }
    1668                 :            : 
    1669                 :          0 :                 vc_req_out = rte_cryptodev_asym_session_get_user_data(asym_session);
    1670                 :            :                 rte_memcpy(vc_req_out, vc_req, sizeof(struct vhost_crypto_data_req));
    1671                 :          0 :                 vc_req_out->wb = NULL;
    1672                 :            : 
    1673   [ #  #  #  # ]:          0 :                 switch (req.header.algo) {
    1674   [ #  #  #  # ]:          0 :                 case VIRTIO_CRYPTO_AKCIPHER_RSA:
    1675                 :          0 :                         err = prepare_asym_rsa_op(vcrypto, op, vq, vc_req_out,
    1676                 :            :                                         &req, desc, max_n_descs);
    1677                 :          0 :                         break;
    1678                 :            :                 }
    1679   [ #  #  #  # ]:          0 :                 if (unlikely(err != 0)) {
    1680                 :          0 :                         VC_LOG_ERR("Failed to process asym request");
    1681                 :          0 :                         goto error_exit;
    1682                 :            :                 }
    1683                 :            : 
    1684                 :            :                 break;
    1685                 :          0 :         default:
    1686                 :            :                 err = VIRTIO_CRYPTO_ERR;
    1687                 :          0 :                 VC_LOG_ERR("Unsupported symmetric crypto request type %u",
    1688                 :            :                                 req.header.opcode);
    1689                 :          0 :                 goto error_exit;
    1690                 :            :         }
    1691                 :            : 
    1692                 :            :         return 0;
    1693                 :            : 
    1694   [ #  #  #  # ]:          0 : error_exit:
    1695                 :            : 
    1696                 :            :         inhdr = reach_inhdr(vc_req->dev, vq, descs, max_n_descs);
    1697   [ #  #  #  # ]:          0 :         if (likely(inhdr != NULL))
    1698                 :          0 :                 inhdr->status = (uint8_t)err;
    1699                 :            : 
    1700                 :            :         return -1;
    1701                 :            : }
    1702                 :            : 
    1703                 :            : static __rte_always_inline struct vhost_virtqueue *
    1704                 :            : vhost_crypto_finalize_one_request(struct rte_crypto_op *op,
    1705                 :            :                 struct vhost_virtqueue *old_vq)
    1706                 :            : {
    1707                 :            :         struct rte_mbuf *m_src = NULL, *m_dst = NULL;
    1708                 :            :         struct vhost_crypto_data_req *vc_req;
    1709                 :            :         struct vhost_virtqueue *vq;
    1710                 :            :         uint16_t used_idx, desc_idx;
    1711                 :            : 
    1712                 :          0 :         if (op->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
    1713                 :          0 :                 m_src = op->sym->m_src;
    1714                 :          0 :                 m_dst = op->sym->m_dst;
    1715                 :            :                 vc_req = rte_mbuf_to_priv(m_src);
    1716   [ #  #  #  # ]:          0 :         } else if (op->type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC) {
    1717                 :          0 :                 vc_req = rte_cryptodev_asym_session_get_user_data(op->asym->session);
    1718                 :            :         } else {
    1719                 :          0 :                 VC_LOG_ERR("Invalid crypto op type");
    1720                 :          0 :                 return NULL;
    1721                 :            :         }
    1722                 :            : 
    1723   [ #  #  #  # ]:          0 :         if (unlikely(!vc_req)) {
    1724                 :          0 :                 VC_LOG_ERR("Failed to retrieve vc_req");
    1725                 :          0 :                 return NULL;
    1726                 :            :         }
    1727                 :          0 :         vq = vc_req->vq;
    1728                 :          0 :         used_idx = vc_req->desc_idx;
    1729                 :            : 
    1730         [ #  # ]:          0 :         if (old_vq && (vq != old_vq))
    1731                 :            :                 return vq;
    1732                 :            : 
    1733   [ #  #  #  # ]:          0 :         if (unlikely(op->status != RTE_CRYPTO_OP_STATUS_SUCCESS))
    1734                 :          0 :                 vc_req->inhdr->status = VIRTIO_CRYPTO_ERR;
    1735                 :            :         else {
    1736   [ #  #  #  # ]:          0 :                 if (vc_req->zero_copy == 0)
    1737                 :          0 :                         write_back_data(vc_req);
    1738                 :            :         }
    1739                 :            : 
    1740                 :          0 :         desc_idx = vq->avail->ring[used_idx];
    1741                 :          0 :         vq->used->ring[desc_idx].id = vq->avail->ring[desc_idx];
    1742                 :          0 :         vq->used->ring[desc_idx].len = vc_req->len;
    1743                 :            : 
    1744   [ #  #  #  # ]:          0 :         if (op->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
    1745   [ #  #  #  # ]:          0 :                 rte_mempool_put(m_src->pool, (void *)m_src);
    1746   [ #  #  #  # ]:          0 :                 if (m_dst)
    1747   [ #  #  #  # ]:          0 :                         rte_mempool_put(m_dst->pool, (void *)m_dst);
    1748                 :            :         }
    1749                 :            : 
    1750                 :            :         return vq;
    1751                 :            : }
    1752                 :            : 
    1753                 :            : static __rte_always_inline uint16_t
    1754                 :            : vhost_crypto_complete_one_vm_requests(struct rte_crypto_op **ops,
    1755                 :            :                 uint16_t nb_ops, int *callfd)
    1756                 :            : {
    1757                 :            :         uint16_t processed = 1;
    1758                 :            :         struct vhost_virtqueue *vq, *tmp_vq;
    1759                 :            : 
    1760                 :            :         if (unlikely(nb_ops == 0))
    1761                 :            :                 return 0;
    1762                 :            : 
    1763         [ #  # ]:          0 :         vq = vhost_crypto_finalize_one_request(ops[0], NULL);
    1764         [ #  # ]:          0 :         if (unlikely(vq == NULL))
    1765                 :            :                 return 0;
    1766                 :            :         tmp_vq = vq;
    1767                 :            : 
    1768         [ #  # ]:          0 :         while ((processed < nb_ops)) {
    1769         [ #  # ]:          0 :                 tmp_vq = vhost_crypto_finalize_one_request(ops[processed],
    1770                 :            :                                 tmp_vq);
    1771                 :            : 
    1772         [ #  # ]:          0 :                 if (unlikely(vq != tmp_vq))
    1773                 :            :                         break;
    1774                 :            : 
    1775                 :          0 :                 processed++;
    1776                 :            :         }
    1777                 :            : 
    1778                 :          0 :         *callfd = vq->callfd;
    1779                 :            : 
    1780                 :          0 :         *(volatile uint16_t *)&vq->used->idx += processed;
    1781                 :            : 
    1782                 :          0 :         return processed;
    1783                 :            : }
    1784                 :            : 
    1785                 :            : int
    1786                 :          0 : rte_vhost_crypto_driver_start(const char *path)
    1787                 :            : {
    1788                 :            :         uint64_t protocol_features;
    1789                 :            :         int ret;
    1790                 :            : 
    1791                 :          0 :         ret = rte_vhost_driver_set_features(path, VIRTIO_CRYPTO_FEATURES);
    1792         [ #  # ]:          0 :         if (ret)
    1793                 :            :                 return -1;
    1794                 :            : 
    1795                 :          0 :         ret = rte_vhost_driver_get_protocol_features(path, &protocol_features);
    1796         [ #  # ]:          0 :         if (ret)
    1797                 :            :                 return -1;
    1798                 :          0 :         protocol_features |= (1ULL << VHOST_USER_PROTOCOL_F_CONFIG);
    1799                 :          0 :         ret = rte_vhost_driver_set_protocol_features(path, protocol_features);
    1800         [ #  # ]:          0 :         if (ret)
    1801                 :            :                 return -1;
    1802                 :            : 
    1803                 :          0 :         return rte_vhost_driver_start(path);
    1804                 :            : }
    1805                 :            : 
    1806                 :            : int
    1807         [ #  # ]:          0 : rte_vhost_crypto_create(int vid, uint8_t cryptodev_id,
    1808                 :            :                 struct rte_mempool *sess_pool,
    1809                 :            :                 int socket_id)
    1810                 :            : {
    1811                 :            :         struct virtio_net *dev = get_device(vid);
    1812                 :          0 :         struct rte_hash_parameters params = {0};
    1813                 :            :         struct vhost_crypto *vcrypto;
    1814                 :            :         char name[128];
    1815                 :            :         int ret;
    1816                 :            : 
    1817         [ #  # ]:          0 :         if (!dev) {
    1818                 :          0 :                 VC_LOG_ERR("Invalid vid %i", vid);
    1819                 :          0 :                 return -EINVAL;
    1820                 :            :         }
    1821                 :            : 
    1822                 :          0 :         vcrypto = rte_zmalloc_socket(NULL, sizeof(*vcrypto),
    1823                 :            :                         RTE_CACHE_LINE_SIZE, socket_id);
    1824         [ #  # ]:          0 :         if (!vcrypto) {
    1825                 :          0 :                 VC_LOG_ERR("Insufficient memory");
    1826                 :          0 :                 return -ENOMEM;
    1827                 :            :         }
    1828                 :            : 
    1829                 :          0 :         vcrypto->sess_pool = sess_pool;
    1830                 :          0 :         vcrypto->cid = cryptodev_id;
    1831                 :          0 :         vcrypto->cache_sym_session_id = UINT64_MAX;
    1832                 :          0 :         vcrypto->cache_asym_session_id = UINT64_MAX;
    1833                 :          0 :         vcrypto->last_session_id = 1;
    1834                 :          0 :         vcrypto->dev = dev;
    1835                 :          0 :         vcrypto->option = RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE;
    1836                 :            : 
    1837                 :            :         snprintf(name, 127, "HASH_VHOST_CRYPT_%u", (uint32_t)vid);
    1838                 :          0 :         params.name = name;
    1839                 :          0 :         params.entries = VHOST_CRYPTO_SESSION_MAP_ENTRIES;
    1840                 :          0 :         params.hash_func = rte_jhash;
    1841                 :          0 :         params.key_len = sizeof(uint64_t);
    1842                 :          0 :         params.socket_id = socket_id;
    1843                 :          0 :         vcrypto->session_map = rte_hash_create(&params);
    1844         [ #  # ]:          0 :         if (!vcrypto->session_map) {
    1845                 :          0 :                 VC_LOG_ERR("Failed to creath session map");
    1846                 :            :                 ret = -ENOMEM;
    1847                 :          0 :                 goto error_exit;
    1848                 :            :         }
    1849                 :            : 
    1850                 :            :         snprintf(name, 127, "MBUF_POOL_VM_%u", (uint32_t)vid);
    1851                 :          0 :         vcrypto->mbuf_pool = rte_pktmbuf_pool_create(name,
    1852                 :            :                         VHOST_CRYPTO_MBUF_POOL_SIZE, 512,
    1853                 :            :                         sizeof(struct vhost_crypto_data_req),
    1854                 :            :                         VHOST_CRYPTO_MAX_DATA_SIZE + RTE_PKTMBUF_HEADROOM,
    1855                 :          0 :                         rte_socket_id());
    1856         [ #  # ]:          0 :         if (!vcrypto->mbuf_pool) {
    1857                 :          0 :                 VC_LOG_ERR("Failed to creath mbuf pool");
    1858                 :            :                 ret = -ENOMEM;
    1859                 :          0 :                 goto error_exit;
    1860                 :            :         }
    1861                 :            : 
    1862                 :            :         snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
    1863                 :          0 :         vcrypto->wb_pool = rte_mempool_create(name,
    1864                 :            :                         VHOST_CRYPTO_MBUF_POOL_SIZE,
    1865                 :            :                         sizeof(struct vhost_crypto_writeback_data),
    1866                 :            :                         128, 0, NULL, NULL, NULL, NULL,
    1867                 :          0 :                         rte_socket_id(), 0);
    1868         [ #  # ]:          0 :         if (!vcrypto->wb_pool) {
    1869                 :          0 :                 VC_LOG_ERR("Failed to creath mempool");
    1870                 :            :                 ret = -ENOMEM;
    1871                 :          0 :                 goto error_exit;
    1872                 :            :         }
    1873                 :            : 
    1874                 :          0 :         dev->extern_data = vcrypto;
    1875                 :          0 :         dev->extern_ops.pre_msg_handle = NULL;
    1876                 :          0 :         dev->extern_ops.post_msg_handle = vhost_crypto_msg_post_handler;
    1877                 :            : 
    1878                 :          0 :         return 0;
    1879                 :            : 
    1880                 :          0 : error_exit:
    1881                 :          0 :         rte_hash_free(vcrypto->session_map);
    1882                 :          0 :         rte_mempool_free(vcrypto->mbuf_pool);
    1883                 :            : 
    1884                 :          0 :         rte_free(vcrypto);
    1885                 :            : 
    1886                 :          0 :         return ret;
    1887                 :            : }
    1888                 :            : 
    1889                 :            : int
    1890         [ #  # ]:          0 : rte_vhost_crypto_free(int vid)
    1891                 :            : {
    1892                 :            :         struct virtio_net *dev = get_device(vid);
    1893                 :            :         struct vhost_crypto *vcrypto;
    1894                 :            : 
    1895         [ #  # ]:          0 :         if (unlikely(dev == NULL)) {
    1896                 :          0 :                 VC_LOG_ERR("Invalid vid %i", vid);
    1897                 :          0 :                 return -EINVAL;
    1898                 :            :         }
    1899                 :            : 
    1900                 :          0 :         vcrypto = dev->extern_data;
    1901         [ #  # ]:          0 :         if (unlikely(vcrypto == NULL)) {
    1902                 :          0 :                 VC_LOG_ERR("Cannot find required data, is it initialized?");
    1903                 :          0 :                 return -ENOENT;
    1904                 :            :         }
    1905                 :            : 
    1906                 :          0 :         rte_hash_free(vcrypto->session_map);
    1907                 :          0 :         rte_mempool_free(vcrypto->mbuf_pool);
    1908                 :          0 :         rte_mempool_free(vcrypto->wb_pool);
    1909                 :          0 :         rte_free(vcrypto);
    1910                 :            : 
    1911                 :          0 :         dev->extern_data = NULL;
    1912                 :          0 :         dev->extern_ops.pre_msg_handle = NULL;
    1913                 :          0 :         dev->extern_ops.post_msg_handle = NULL;
    1914                 :            : 
    1915                 :          0 :         return 0;
    1916                 :            : }
    1917                 :            : 
    1918                 :            : int
    1919         [ #  # ]:          0 : rte_vhost_crypto_set_zero_copy(int vid, enum rte_vhost_crypto_zero_copy option)
    1920                 :            : {
    1921                 :            :         struct virtio_net *dev = get_device(vid);
    1922                 :            :         struct vhost_crypto *vcrypto;
    1923                 :            : 
    1924         [ #  # ]:          0 :         if (unlikely(dev == NULL)) {
    1925                 :          0 :                 VC_LOG_ERR("Invalid vid %i", vid);
    1926                 :          0 :                 return -EINVAL;
    1927                 :            :         }
    1928                 :            : 
    1929         [ #  # ]:          0 :         if (unlikely((uint32_t)option >=
    1930                 :            :                                 RTE_VHOST_CRYPTO_MAX_ZERO_COPY_OPTIONS)) {
    1931                 :          0 :                 VC_LOG_ERR("Invalid option %i", option);
    1932                 :          0 :                 return -EINVAL;
    1933                 :            :         }
    1934                 :            : 
    1935                 :          0 :         vcrypto = (struct vhost_crypto *)dev->extern_data;
    1936         [ #  # ]:          0 :         if (unlikely(vcrypto == NULL)) {
    1937                 :          0 :                 VC_LOG_ERR("Cannot find required data, is it initialized?");
    1938                 :          0 :                 return -ENOENT;
    1939                 :            :         }
    1940                 :            : 
    1941         [ #  # ]:          0 :         if (vcrypto->option == (uint8_t)option)
    1942                 :            :                 return 0;
    1943                 :            : 
    1944   [ #  #  #  # ]:          0 :         if (!(rte_mempool_full(vcrypto->mbuf_pool)) ||
    1945                 :          0 :                         !(rte_mempool_full(vcrypto->wb_pool))) {
    1946                 :          0 :                 VC_LOG_ERR("Cannot update zero copy as mempool is not full");
    1947                 :          0 :                 return -EINVAL;
    1948                 :            :         }
    1949                 :            : 
    1950         [ #  # ]:          0 :         if (option == RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE) {
    1951                 :            :                 char name[128];
    1952                 :            : 
    1953                 :            :                 snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
    1954                 :          0 :                 vcrypto->wb_pool = rte_mempool_create(name,
    1955                 :            :                                 VHOST_CRYPTO_MBUF_POOL_SIZE,
    1956                 :            :                                 sizeof(struct vhost_crypto_writeback_data),
    1957                 :            :                                 128, 0, NULL, NULL, NULL, NULL,
    1958                 :          0 :                                 rte_socket_id(), 0);
    1959         [ #  # ]:          0 :                 if (!vcrypto->wb_pool) {
    1960                 :          0 :                         VC_LOG_ERR("Failed to creath mbuf pool");
    1961                 :          0 :                         return -ENOMEM;
    1962                 :            :                 }
    1963                 :            :         } else {
    1964                 :          0 :                 rte_mempool_free(vcrypto->wb_pool);
    1965                 :          0 :                 vcrypto->wb_pool = NULL;
    1966                 :            :         }
    1967                 :            : 
    1968                 :          0 :         vcrypto->option = (uint8_t)option;
    1969                 :            : 
    1970                 :          0 :         return 0;
    1971                 :            : }
    1972                 :            : 
    1973                 :            : uint16_t
    1974         [ #  # ]:          0 : rte_vhost_crypto_fetch_requests(int vid, uint32_t qid,
    1975                 :            :                 struct rte_crypto_op **ops, uint16_t nb_ops)
    1976                 :            : {
    1977                 :            :         struct rte_mbuf *mbufs[VHOST_CRYPTO_MAX_BURST_SIZE * 2];
    1978                 :            :         struct vhost_crypto_desc descs[VHOST_CRYPTO_MAX_N_DESC];
    1979                 :            :         struct virtio_net *dev = get_device(vid);
    1980                 :            :         struct vhost_crypto *vcrypto;
    1981                 :            :         struct vhost_virtqueue *vq;
    1982                 :            :         uint16_t avail_idx;
    1983                 :            :         uint16_t start_idx;
    1984                 :            :         uint16_t count;
    1985                 :            :         uint16_t i = 0;
    1986                 :            : 
    1987         [ #  # ]:          0 :         if (unlikely(dev == NULL)) {
    1988                 :          0 :                 VC_LOG_ERR("Invalid vid %i", vid);
    1989                 :          0 :                 return 0;
    1990                 :            :         }
    1991                 :            : 
    1992         [ #  # ]:          0 :         if (unlikely(qid >= VHOST_MAX_QUEUE_PAIRS)) {
    1993                 :          0 :                 VC_LOG_ERR("Invalid qid %u", qid);
    1994                 :          0 :                 return 0;
    1995                 :            :         }
    1996                 :            : 
    1997                 :          0 :         vcrypto = (struct vhost_crypto *)dev->extern_data;
    1998         [ #  # ]:          0 :         if (unlikely(vcrypto == NULL)) {
    1999                 :          0 :                 VC_LOG_ERR("Cannot find required data, is it initialized?");
    2000                 :          0 :                 return 0;
    2001                 :            :         }
    2002                 :            : 
    2003                 :          0 :         vq = dev->virtqueue[qid];
    2004                 :            : 
    2005         [ #  # ]:          0 :         if (unlikely(vq == NULL)) {
    2006                 :          0 :                 VC_LOG_ERR("Invalid virtqueue %u", qid);
    2007                 :          0 :                 return 0;
    2008                 :            :         }
    2009                 :            : 
    2010         [ #  # ]:          0 :         if (unlikely(rte_rwlock_read_trylock(&vq->access_lock) != 0))
    2011                 :            :                 return 0;
    2012                 :            : 
    2013                 :            :         vhost_user_iotlb_rd_lock(vq);
    2014         [ #  # ]:          0 :         if (unlikely(!vq->access_ok)) {
    2015                 :            :                 VC_LOG_DBG("Virtqueue %u vrings not yet initialized", qid);
    2016                 :          0 :                 goto out_unlock;
    2017                 :            :         }
    2018                 :            : 
    2019                 :          0 :         avail_idx = *((volatile uint16_t *)&vq->avail->idx);
    2020                 :          0 :         start_idx = vq->last_used_idx;
    2021                 :          0 :         count = avail_idx - start_idx;
    2022                 :          0 :         count = RTE_MIN(count, VHOST_CRYPTO_MAX_BURST_SIZE);
    2023                 :          0 :         count = RTE_MIN(count, nb_ops);
    2024                 :            : 
    2025         [ #  # ]:          0 :         if (unlikely(count == 0))
    2026                 :          0 :                 goto out_unlock;
    2027                 :            : 
    2028                 :            :         /* for zero copy, we need 2 empty mbufs for src and dst, otherwise
    2029                 :            :          * we need only 1 mbuf as src and dst
    2030                 :            :          */
    2031      [ #  #  # ]:          0 :         switch (vcrypto->option) {
    2032                 :          0 :         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
    2033   [ #  #  #  # ]:          0 :                 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
    2034                 :            :                                 (void **)mbufs, count * 2) < 0)) {
    2035                 :          0 :                         VC_LOG_ERR("Insufficient memory");
    2036                 :          0 :                         goto out_unlock;
    2037                 :            :                 }
    2038                 :            : 
    2039         [ #  # ]:          0 :                 for (i = 0; i < count; i++) {
    2040                 :          0 :                         uint16_t used_idx = (start_idx + i) & (vq->size - 1);
    2041                 :          0 :                         uint16_t desc_idx = vq->avail->ring[used_idx];
    2042                 :          0 :                         struct vring_desc *head = &vq->desc[desc_idx];
    2043                 :          0 :                         struct rte_crypto_op *op = ops[i];
    2044                 :            : 
    2045                 :          0 :                         op->sym->m_src = mbufs[i * 2];
    2046                 :          0 :                         op->sym->m_dst = mbufs[i * 2 + 1];
    2047                 :          0 :                         op->sym->m_src->data_off = 0;
    2048         [ #  # ]:          0 :                         op->sym->m_dst->data_off = 0;
    2049                 :            : 
    2050         [ #  # ]:          0 :                         if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
    2051                 :            :                                         op, head, descs, used_idx) < 0))
    2052                 :            :                                 break;
    2053                 :            :                 }
    2054                 :            : 
    2055         [ #  # ]:          0 :                 if (unlikely(i < count))
    2056                 :          0 :                         rte_mempool_put_bulk(vcrypto->mbuf_pool,
    2057                 :          0 :                                         (void **)&mbufs[i * 2],
    2058         [ #  # ]:          0 :                                         (count - i) * 2);
    2059                 :            : 
    2060                 :            :                 break;
    2061                 :            : 
    2062                 :          0 :         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
    2063   [ #  #  #  # ]:          0 :                 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
    2064                 :            :                                 (void **)mbufs, count) < 0)) {
    2065                 :          0 :                         VC_LOG_ERR("Insufficient memory");
    2066                 :          0 :                         goto out_unlock;
    2067                 :            :                 }
    2068                 :            : 
    2069         [ #  # ]:          0 :                 for (i = 0; i < count; i++) {
    2070                 :          0 :                         uint16_t used_idx = (start_idx + i) & (vq->size - 1);
    2071                 :          0 :                         uint16_t desc_idx = vq->avail->ring[used_idx];
    2072                 :          0 :                         struct vring_desc *head = &vq->desc[desc_idx];
    2073                 :          0 :                         struct rte_crypto_op *op = ops[i];
    2074                 :            : 
    2075                 :          0 :                         op->sym->m_src = mbufs[i];
    2076                 :          0 :                         op->sym->m_dst = NULL;
    2077         [ #  # ]:          0 :                         op->sym->m_src->data_off = 0;
    2078                 :            : 
    2079         [ #  # ]:          0 :                         if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
    2080                 :            :                                         op, head, descs, desc_idx) < 0))
    2081                 :            :                                 break;
    2082                 :            :                 }
    2083                 :            : 
    2084         [ #  # ]:          0 :                 if (unlikely(i < count))
    2085                 :          0 :                         rte_mempool_put_bulk(vcrypto->mbuf_pool,
    2086                 :          0 :                                         (void **)&mbufs[i],
    2087         [ #  # ]:          0 :                                         count - i);
    2088                 :            : 
    2089                 :            :                 break;
    2090                 :            : 
    2091                 :            :         }
    2092                 :            : 
    2093                 :          0 :         vq->last_used_idx += i;
    2094                 :            : 
    2095                 :          0 : out_unlock:
    2096                 :            :         vhost_user_iotlb_rd_unlock(vq);
    2097                 :          0 :         rte_rwlock_read_unlock(&vq->access_lock);
    2098                 :            : 
    2099                 :          0 :         return i;
    2100                 :            : }
    2101                 :            : 
    2102                 :            : uint16_t
    2103                 :          0 : rte_vhost_crypto_finalize_requests(struct rte_crypto_op **ops,
    2104                 :            :                 uint16_t nb_ops, int *callfds, uint16_t *nb_callfds)
    2105                 :            : {
    2106                 :            :         struct rte_crypto_op **tmp_ops = ops;
    2107                 :            :         uint16_t count = 0, left = nb_ops;
    2108                 :            :         int callfd;
    2109                 :            :         uint16_t idx = 0;
    2110                 :            : 
    2111         [ #  # ]:          0 :         while (left) {
    2112                 :            :                 count = vhost_crypto_complete_one_vm_requests(tmp_ops, left,
    2113                 :            :                                 &callfd);
    2114         [ #  # ]:          0 :                 if (unlikely(count == 0))
    2115                 :            :                         break;
    2116                 :            : 
    2117                 :          0 :                 tmp_ops = &tmp_ops[count];
    2118                 :          0 :                 left -= count;
    2119                 :            : 
    2120                 :          0 :                 callfds[idx++] = callfd;
    2121                 :            : 
    2122         [ #  # ]:          0 :                 if (unlikely(idx >= VIRTIO_CRYPTO_MAX_NUM_BURST_VQS)) {
    2123                 :          0 :                         VC_LOG_ERR("Too many vqs");
    2124                 :          0 :                         break;
    2125                 :            :                 }
    2126                 :            :         }
    2127                 :            : 
    2128                 :          0 :         *nb_callfds = idx;
    2129                 :            : 
    2130                 :          0 :         return nb_ops - left;
    2131                 :            : }

Generated by: LCOV version 1.14