LCOV - code coverage report
Current view: top level - lib/vhost - vhost_user.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 1399 0.0 %
Date: 2025-05-01 17:49:45 Functions: 0 73 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 886 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2018 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : /* Security model
       6                 :            :  * --------------
       7                 :            :  * The vhost-user protocol connection is an external interface, so it must be
       8                 :            :  * robust against invalid inputs.
       9                 :            :  *
      10                 :            :  * This is important because the vhost-user frontend is only one step removed
      11                 :            :  * from the guest.  Malicious guests that have escaped will then launch further
      12                 :            :  * attacks from the vhost-user frontend.
      13                 :            :  *
      14                 :            :  * Even in deployments where guests are trusted, a bug in the vhost-user frontend
      15                 :            :  * can still cause invalid messages to be sent.  Such messages must not
      16                 :            :  * compromise the stability of the DPDK application by causing crashes, memory
      17                 :            :  * corruption, or other problematic behavior.
      18                 :            :  *
      19                 :            :  * Do not assume received VhostUserMsg fields contain sensible values!
      20                 :            :  */
      21                 :            : 
      22                 :            : #include <assert.h>
      23                 :            : #include <stdint.h>
      24                 :            : #include <stdio.h>
      25                 :            : #include <stdlib.h>
      26                 :            : #include <string.h>
      27                 :            : #include <unistd.h>
      28                 :            : #include <fcntl.h>
      29                 :            : #include <sys/ioctl.h>
      30                 :            : #include <sys/mman.h>
      31                 :            : #include <sys/stat.h>
      32                 :            : #include <sys/syscall.h>
      33                 :            : #ifdef RTE_LIBRTE_VHOST_NUMA
      34                 :            : #include <numaif.h>
      35                 :            : #endif
      36                 :            : #ifdef RTE_LIBRTE_VHOST_POSTCOPY
      37                 :            : #include <linux/userfaultfd.h>
      38                 :            : #endif
      39                 :            : #ifdef F_ADD_SEALS /* if file sealing is supported, so is memfd */
      40                 :            : #include <linux/memfd.h>
      41                 :            : #define MEMFD_SUPPORTED
      42                 :            : #endif
      43                 :            : 
      44                 :            : #include <eal_export.h>
      45                 :            : #include <rte_common.h>
      46                 :            : #include <rte_malloc.h>
      47                 :            : #include <rte_log.h>
      48                 :            : #include <rte_vfio.h>
      49                 :            : #include <rte_errno.h>
      50                 :            : 
      51                 :            : #include "iotlb.h"
      52                 :            : #include "vhost.h"
      53                 :            : #include "vhost_user.h"
      54                 :            : 
      55                 :            : #define VIRTIO_MIN_MTU 68
      56                 :            : #define VIRTIO_MAX_MTU 65535
      57                 :            : 
      58                 :            : #define INFLIGHT_ALIGNMENT      64
      59                 :            : #define INFLIGHT_VERSION        0x1
      60                 :            : 
      61                 :            : typedef const struct vhost_message_handler {
      62                 :            :         const char *description;
      63                 :            :         int (*callback)(struct virtio_net **pdev, struct vhu_msg_context *ctx,
      64                 :            :                 int main_fd);
      65                 :            :         bool accepts_fd;
      66                 :            :         bool lock_all_qps;
      67                 :            : } vhost_message_handler_t;
      68                 :            : static vhost_message_handler_t vhost_message_handlers[];
      69                 :            : 
      70                 :            : #define VHOST_MESSAGE_HANDLERS \
      71                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_NONE, NULL, false, false) \
      72                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_GET_FEATURES, vhost_user_get_features, false, false) \
      73                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_FEATURES, vhost_user_set_features, false, true) \
      74                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_OWNER, vhost_user_set_owner, false, true) \
      75                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_RESET_OWNER, vhost_user_reset_owner, false, false) \
      76                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_MEM_TABLE, vhost_user_set_mem_table, true, true) \
      77                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_LOG_BASE, vhost_user_set_log_base, true, true) \
      78                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_LOG_FD, vhost_user_set_log_fd, true, true) \
      79                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_NUM, vhost_user_set_vring_num, false, true) \
      80                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_ADDR, vhost_user_set_vring_addr, false, true) \
      81                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_BASE, vhost_user_set_vring_base, false, true) \
      82                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_GET_VRING_BASE, vhost_user_get_vring_base, false, false) \
      83                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_KICK, vhost_user_set_vring_kick, true, true) \
      84                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_CALL, vhost_user_set_vring_call, true, true) \
      85                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_ERR, vhost_user_set_vring_err, true, true) \
      86                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_GET_PROTOCOL_FEATURES, vhost_user_get_protocol_features, \
      87                 :            :         false, false) \
      88                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_PROTOCOL_FEATURES, vhost_user_set_protocol_features, \
      89                 :            :         false, true) \
      90                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_GET_QUEUE_NUM, vhost_user_get_queue_num, false, false) \
      91                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_ENABLE, vhost_user_set_vring_enable, false, true) \
      92                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_SEND_RARP, vhost_user_send_rarp, false, true) \
      93                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_NET_SET_MTU, vhost_user_net_set_mtu, false, true) \
      94                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_BACKEND_REQ_FD, vhost_user_set_req_fd, true, true) \
      95                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_IOTLB_MSG, vhost_user_iotlb_msg, false, false) \
      96                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_GET_CONFIG, vhost_user_get_config, false, false) \
      97                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_CONFIG, vhost_user_set_config, false, false) \
      98                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_POSTCOPY_ADVISE, vhost_user_set_postcopy_advise, false, false) \
      99                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_POSTCOPY_LISTEN, vhost_user_set_postcopy_listen, false, false) \
     100                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_POSTCOPY_END, vhost_user_postcopy_end, false, false) \
     101                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_GET_INFLIGHT_FD, vhost_user_get_inflight_fd, false, false) \
     102                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_INFLIGHT_FD, vhost_user_set_inflight_fd, true, false) \
     103                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_STATUS, vhost_user_set_status, false, false) \
     104                 :            : VHOST_MESSAGE_HANDLER(VHOST_USER_GET_STATUS, vhost_user_get_status, false, false)
     105                 :            : 
     106                 :            : #define VHOST_MESSAGE_HANDLER(id, handler, accepts_fd, lock_all_qps) \
     107                 :            :         id ## _LOCK_ALL_QPS = lock_all_qps,
     108                 :            : enum {
     109                 :            :         VHOST_MESSAGE_HANDLERS
     110                 :            : };
     111                 :            : #undef VHOST_MESSAGE_HANDLER
     112                 :            : 
     113                 :            : /* vhost_user_msg_handler() locks all qps based on a handler's lock_all_qps.
     114                 :            :  * Later, a handler may need to ensure the vq has been locked (for example,
     115                 :            :  * when calling lock annotated helpers).
     116                 :            :  *
     117                 :            :  * Note: unfortunately, static_assert() does not see an array content as a
     118                 :            :  * constant expression. Because of this, we can't simply check for
     119                 :            :  * vhost_user_msg_handler[].lock_all_qps.
     120                 :            :  * Instead, define an enum for each handler.
     121                 :            :  */
     122                 :            : #define VHOST_USER_ASSERT_LOCK(dev, vq, id) do { \
     123                 :            :         static_assert(id ## _LOCK_ALL_QPS == true, \
     124                 :            :                 #id " handler is not declared as locking all queue pairs"); \
     125                 :            :         vq_assert_lock(dev, vq); \
     126                 :            : } while (0)
     127                 :            : 
     128                 :            : static int send_vhost_reply(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx);
     129                 :            : static int read_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx);
     130                 :            : 
     131                 :            : static void
     132                 :          0 : close_msg_fds(struct vhu_msg_context *ctx)
     133                 :            : {
     134                 :            :         int i;
     135                 :            : 
     136         [ #  # ]:          0 :         for (i = 0; i < ctx->fd_num; i++) {
     137                 :          0 :                 int fd = ctx->fds[i];
     138                 :            : 
     139         [ #  # ]:          0 :                 if (fd == -1)
     140                 :          0 :                         continue;
     141                 :            : 
     142                 :          0 :                 ctx->fds[i] = -1;
     143                 :          0 :                 close(fd);
     144                 :            :         }
     145                 :          0 : }
     146                 :            : 
     147                 :            : /*
     148                 :            :  * Ensure the expected number of FDs is received,
     149                 :            :  * close all FDs and return an error if this is not the case.
     150                 :            :  */
     151                 :            : static int
     152                 :          0 : validate_msg_fds(struct virtio_net *dev, struct vhu_msg_context *ctx, int expected_fds)
     153                 :            : {
     154         [ #  # ]:          0 :         if (ctx->fd_num == expected_fds)
     155                 :            :                 return 0;
     156                 :            : 
     157                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, ERR,
     158                 :            :                 "expect %d FDs for request %s, received %d",
     159                 :            :                 expected_fds, vhost_message_handlers[ctx->msg.request.frontend].description,
     160                 :            :                 ctx->fd_num);
     161                 :            : 
     162                 :          0 :         close_msg_fds(ctx);
     163                 :            : 
     164                 :          0 :         return -1;
     165                 :            : }
     166                 :            : 
     167                 :            : static uint64_t
     168                 :            : get_blk_size(int fd)
     169                 :            : {
     170                 :            :         struct stat stat;
     171                 :            :         int ret;
     172                 :            : 
     173                 :          0 :         ret = fstat(fd, &stat);
     174   [ #  #  #  #  :          0 :         return ret == -1 ? (uint64_t)-1 : (uint64_t)stat.st_blksize;
          #  #  #  #  #  
                      # ]
     175                 :            : }
     176                 :            : 
     177                 :            : static void
     178                 :          0 : async_dma_map(struct virtio_net *dev, bool do_map)
     179                 :            : {
     180                 :            :         int ret = 0;
     181                 :            :         uint32_t i;
     182                 :            :         struct guest_page *page;
     183                 :            : 
     184         [ #  # ]:          0 :         if (do_map) {
     185         [ #  # ]:          0 :                 for (i = 0; i < dev->nr_guest_pages; i++) {
     186                 :          0 :                         page = &dev->guest_pages[i];
     187                 :          0 :                         ret = rte_vfio_container_dma_map(RTE_VFIO_DEFAULT_CONTAINER_FD,
     188                 :            :                                                          page->host_user_addr,
     189                 :            :                                                          page->host_iova,
     190                 :            :                                                          page->size);
     191         [ #  # ]:          0 :                         if (ret) {
     192                 :            :                                 /*
     193                 :            :                                  * DMA device may bind with kernel driver, in this case,
     194                 :            :                                  * we don't need to program IOMMU manually. However, if no
     195                 :            :                                  * device is bound with vfio/uio in DPDK, and vfio kernel
     196                 :            :                                  * module is loaded, the API will still be called and return
     197                 :            :                                  * with ENODEV.
     198                 :            :                                  *
     199                 :            :                                  * DPDK vfio only returns ENODEV in very similar situations
     200                 :            :                                  * (vfio either unsupported, or supported but no devices found).
     201                 :            :                                  * Either way, no mappings could be performed. We treat it as
     202                 :            :                                  * normal case in async path. This is a workaround.
     203                 :            :                                  */
     204         [ #  # ]:          0 :                                 if (rte_errno == ENODEV)
     205                 :            :                                         return;
     206                 :            : 
     207                 :            :                                 /* DMA mapping errors won't stop VHOST_USER_SET_MEM_TABLE. */
     208                 :          0 :                                 VHOST_CONFIG_LOG(dev->ifname, ERR, "DMA engine map failed");
     209                 :            :                         }
     210                 :            :                 }
     211                 :            : 
     212                 :            :         } else {
     213         [ #  # ]:          0 :                 for (i = 0; i < dev->nr_guest_pages; i++) {
     214                 :          0 :                         page = &dev->guest_pages[i];
     215                 :          0 :                         ret = rte_vfio_container_dma_unmap(RTE_VFIO_DEFAULT_CONTAINER_FD,
     216                 :            :                                                            page->host_user_addr,
     217                 :            :                                                            page->host_iova,
     218                 :            :                                                            page->size);
     219         [ #  # ]:          0 :                         if (ret) {
     220                 :            :                                 /* like DMA map, ignore the kernel driver case when unmap. */
     221         [ #  # ]:          0 :                                 if (rte_errno == EINVAL)
     222                 :            :                                         return;
     223                 :            : 
     224                 :          0 :                                 VHOST_CONFIG_LOG(dev->ifname, ERR, "DMA engine unmap failed");
     225                 :            :                         }
     226                 :            :                 }
     227                 :            :         }
     228                 :            : }
     229                 :            : 
     230                 :            : static void
     231                 :          0 : free_mem_region(struct virtio_net *dev)
     232                 :            : {
     233                 :            :         uint32_t i;
     234                 :            :         struct rte_vhost_mem_region *reg;
     235                 :            : 
     236   [ #  #  #  # ]:          0 :         if (!dev || !dev->mem)
     237                 :            :                 return;
     238                 :            : 
     239   [ #  #  #  # ]:          0 :         if (dev->async_copy && rte_vfio_is_enabled("vfio"))
     240                 :          0 :                 async_dma_map(dev, false);
     241                 :            : 
     242         [ #  # ]:          0 :         for (i = 0; i < dev->mem->nregions; i++) {
     243                 :            :                 reg = &dev->mem->regions[i];
     244         [ #  # ]:          0 :                 if (reg->host_user_addr) {
     245                 :          0 :                         munmap(reg->mmap_addr, reg->mmap_size);
     246                 :          0 :                         close(reg->fd);
     247                 :            :                 }
     248                 :            :         }
     249                 :            : }
     250                 :            : 
     251                 :            : void
     252                 :          0 : vhost_backend_cleanup(struct virtio_net *dev)
     253                 :            : {
     254                 :            :         struct rte_vdpa_device *vdpa_dev;
     255                 :            : 
     256                 :          0 :         vdpa_dev = dev->vdpa_dev;
     257   [ #  #  #  # ]:          0 :         if (vdpa_dev && vdpa_dev->ops->dev_cleanup != NULL)
     258                 :          0 :                 vdpa_dev->ops->dev_cleanup(dev->vid);
     259                 :            : 
     260         [ #  # ]:          0 :         if (dev->mem) {
     261                 :          0 :                 free_mem_region(dev);
     262                 :          0 :                 rte_free(dev->mem);
     263                 :          0 :                 dev->mem = NULL;
     264                 :            :         }
     265                 :            : 
     266                 :          0 :         rte_free(dev->guest_pages);
     267                 :          0 :         dev->guest_pages = NULL;
     268                 :            : 
     269         [ #  # ]:          0 :         if (dev->log_addr) {
     270                 :          0 :                 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
     271                 :          0 :                 dev->log_addr = 0;
     272                 :            :         }
     273                 :            : 
     274         [ #  # ]:          0 :         if (dev->inflight_info) {
     275         [ #  # ]:          0 :                 if (dev->inflight_info->addr) {
     276                 :          0 :                         munmap(dev->inflight_info->addr,
     277                 :            :                                dev->inflight_info->size);
     278                 :          0 :                         dev->inflight_info->addr = NULL;
     279                 :            :                 }
     280                 :            : 
     281         [ #  # ]:          0 :                 if (dev->inflight_info->fd >= 0) {
     282                 :          0 :                         close(dev->inflight_info->fd);
     283                 :          0 :                         dev->inflight_info->fd = -1;
     284                 :            :                 }
     285                 :            : 
     286                 :          0 :                 rte_free(dev->inflight_info);
     287                 :          0 :                 dev->inflight_info = NULL;
     288                 :            :         }
     289                 :            : 
     290         [ #  # ]:          0 :         if (dev->backend_req_fd >= 0) {
     291                 :          0 :                 close(dev->backend_req_fd);
     292                 :          0 :                 dev->backend_req_fd = -1;
     293                 :            :         }
     294                 :            : 
     295         [ #  # ]:          0 :         if (dev->postcopy_ufd >= 0) {
     296                 :          0 :                 close(dev->postcopy_ufd);
     297                 :          0 :                 dev->postcopy_ufd = -1;
     298                 :            :         }
     299                 :            : 
     300                 :          0 :         dev->postcopy_listening = 0;
     301                 :            : 
     302                 :          0 :         vhost_user_iotlb_destroy(dev);
     303                 :          0 : }
     304                 :            : 
     305                 :            : static void
     306                 :          0 : vhost_user_notify_queue_state(struct virtio_net *dev, struct vhost_virtqueue *vq,
     307                 :            :         int enable)
     308                 :            : {
     309                 :          0 :         struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
     310                 :            : 
     311                 :            :         /* Configure guest notifications on enable */
     312   [ #  #  #  # ]:          0 :         if (enable && vq->notif_enable != VIRTIO_UNINITIALIZED_NOTIF)
     313                 :          0 :                 vhost_enable_guest_notification(dev, vq, vq->notif_enable);
     314                 :            : 
     315   [ #  #  #  # ]:          0 :         if (vdpa_dev && vdpa_dev->ops->set_vring_state)
     316                 :          0 :                 vdpa_dev->ops->set_vring_state(dev->vid, vq->index, enable);
     317                 :            : 
     318         [ #  # ]:          0 :         if (dev->notify_ops->vring_state_changed)
     319                 :          0 :                 dev->notify_ops->vring_state_changed(dev->vid, vq->index, enable);
     320                 :          0 : }
     321                 :            : 
     322                 :            : /*
     323                 :            :  * This function just returns success at the moment unless
     324                 :            :  * the device hasn't been initialised.
     325                 :            :  */
     326                 :            : static int
     327                 :          0 : vhost_user_set_owner(struct virtio_net **pdev __rte_unused,
     328                 :            :                         struct vhu_msg_context *ctx __rte_unused,
     329                 :            :                         int main_fd __rte_unused)
     330                 :            : {
     331                 :          0 :         return RTE_VHOST_MSG_RESULT_OK;
     332                 :            : }
     333                 :            : 
     334                 :            : static int
     335                 :          0 : vhost_user_reset_owner(struct virtio_net **pdev,
     336                 :            :                         struct vhu_msg_context *ctx __rte_unused,
     337                 :            :                         int main_fd __rte_unused)
     338                 :            : {
     339                 :          0 :         struct virtio_net *dev = *pdev;
     340                 :            : 
     341                 :          0 :         vhost_destroy_device_notify(dev);
     342                 :            : 
     343                 :          0 :         cleanup_device(dev, 0);
     344                 :          0 :         reset_device(dev);
     345                 :          0 :         return RTE_VHOST_MSG_RESULT_OK;
     346                 :            : }
     347                 :            : 
     348                 :            : /*
     349                 :            :  * The features that we support are requested.
     350                 :            :  */
     351                 :            : static int
     352                 :          0 : vhost_user_get_features(struct virtio_net **pdev,
     353                 :            :                         struct vhu_msg_context *ctx,
     354                 :            :                         int main_fd __rte_unused)
     355                 :            : {
     356                 :          0 :         struct virtio_net *dev = *pdev;
     357                 :          0 :         uint64_t features = 0;
     358                 :            : 
     359                 :          0 :         rte_vhost_driver_get_features(dev->ifname, &features);
     360                 :            : 
     361                 :          0 :         ctx->msg.payload.u64 = features;
     362                 :          0 :         ctx->msg.size = sizeof(ctx->msg.payload.u64);
     363                 :          0 :         ctx->fd_num = 0;
     364                 :            : 
     365                 :          0 :         return RTE_VHOST_MSG_RESULT_REPLY;
     366                 :            : }
     367                 :            : 
     368                 :            : /*
     369                 :            :  * The queue number that we support are requested.
     370                 :            :  */
     371                 :            : static int
     372                 :          0 : vhost_user_get_queue_num(struct virtio_net **pdev,
     373                 :            :                         struct vhu_msg_context *ctx,
     374                 :            :                         int main_fd __rte_unused)
     375                 :            : {
     376                 :          0 :         struct virtio_net *dev = *pdev;
     377                 :          0 :         uint32_t queue_num = 0;
     378                 :            : 
     379                 :          0 :         rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
     380                 :            : 
     381                 :          0 :         ctx->msg.payload.u64 = (uint64_t)queue_num;
     382                 :          0 :         ctx->msg.size = sizeof(ctx->msg.payload.u64);
     383                 :          0 :         ctx->fd_num = 0;
     384                 :            : 
     385                 :          0 :         return RTE_VHOST_MSG_RESULT_REPLY;
     386                 :            : }
     387                 :            : 
     388                 :            : /*
     389                 :            :  * We receive the negotiated features supported by us and the virtio device.
     390                 :            :  */
     391                 :            : static int
     392                 :          0 : vhost_user_set_features(struct virtio_net **pdev,
     393                 :            :                         struct vhu_msg_context *ctx,
     394                 :            :                         int main_fd __rte_unused)
     395                 :            : {
     396                 :          0 :         struct virtio_net *dev = *pdev;
     397                 :          0 :         uint64_t features = ctx->msg.payload.u64;
     398                 :          0 :         uint64_t vhost_features = 0;
     399                 :            :         struct rte_vdpa_device *vdpa_dev;
     400                 :            : 
     401                 :          0 :         rte_vhost_driver_get_features(dev->ifname, &vhost_features);
     402         [ #  # ]:          0 :         if (features & ~vhost_features) {
     403                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "received invalid negotiated features.");
     404                 :          0 :                 dev->flags |= VIRTIO_DEV_FEATURES_FAILED;
     405                 :          0 :                 dev->status &= ~VIRTIO_DEVICE_STATUS_FEATURES_OK;
     406                 :            : 
     407                 :          0 :                 return RTE_VHOST_MSG_RESULT_ERR;
     408                 :            :         }
     409                 :            : 
     410         [ #  # ]:          0 :         if (dev->flags & VIRTIO_DEV_RUNNING) {
     411         [ #  # ]:          0 :                 if (dev->features == features)
     412                 :            :                         return RTE_VHOST_MSG_RESULT_OK;
     413                 :            : 
     414                 :            :                 /*
     415                 :            :                  * Error out if frontend tries to change features while device is
     416                 :            :                  * in running state. The exception being VHOST_F_LOG_ALL, which
     417                 :            :                  * is enabled when the live-migration starts.
     418                 :            :                  */
     419         [ #  # ]:          0 :                 if ((dev->features ^ features) & ~(1ULL << VHOST_F_LOG_ALL)) {
     420                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
     421                 :            :                                 "features changed while device is running.");
     422                 :          0 :                         return RTE_VHOST_MSG_RESULT_ERR;
     423                 :            :                 }
     424                 :            : 
     425         [ #  # ]:          0 :                 if (dev->notify_ops->features_changed)
     426                 :          0 :                         dev->notify_ops->features_changed(dev->vid, features);
     427                 :            :         }
     428                 :            : 
     429                 :          0 :         dev->features = features;
     430         [ #  # ]:          0 :         if (dev->features &
     431                 :            :                 ((1ULL << VIRTIO_NET_F_MRG_RXBUF) |
     432                 :            :                  (1ULL << VIRTIO_F_VERSION_1) |
     433                 :            :                  (1ULL << VIRTIO_F_RING_PACKED))) {
     434                 :          0 :                 dev->vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
     435                 :            :         } else {
     436                 :          0 :                 dev->vhost_hlen = sizeof(struct virtio_net_hdr);
     437                 :            :         }
     438                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
     439                 :            :                 "negotiated Virtio features: 0x%" PRIx64,
     440                 :            :                 dev->features);
     441   [ #  #  #  # ]:          0 :         VHOST_CONFIG_LOG(dev->ifname, DEBUG,
     442                 :            :                 "mergeable RX buffers %s, virtio 1 %s",
     443                 :            :                 (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
     444                 :            :                 (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off");
     445                 :            : 
     446         [ #  # ]:          0 :         if ((dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET) &&
     447         [ #  # ]:          0 :             !(dev->features & (1ULL << VIRTIO_NET_F_MQ))) {
     448                 :            :                 /*
     449                 :            :                  * Remove all but first queue pair if MQ hasn't been
     450                 :            :                  * negotiated. This is safe because the device is not
     451                 :            :                  * running at this stage.
     452                 :            :                  */
     453         [ #  # ]:          0 :                 while (dev->nr_vring > 2) {
     454                 :            :                         struct vhost_virtqueue *vq;
     455                 :            : 
     456                 :          0 :                         vq = dev->virtqueue[--dev->nr_vring];
     457         [ #  # ]:          0 :                         if (!vq)
     458                 :          0 :                                 continue;
     459                 :            : 
     460                 :          0 :                         dev->virtqueue[dev->nr_vring] = NULL;
     461                 :          0 :                         cleanup_vq(vq, 1);
     462                 :          0 :                         cleanup_vq_inflight(dev, vq);
     463                 :            :                         /* vhost_user_lock_all_queue_pairs locked all qps */
     464                 :          0 :                         VHOST_USER_ASSERT_LOCK(dev, vq, VHOST_USER_SET_FEATURES);
     465                 :            :                         rte_rwlock_write_unlock(&vq->access_lock);
     466                 :          0 :                         free_vq(dev, vq);
     467                 :            :                 }
     468                 :            :         }
     469                 :            : 
     470                 :          0 :         vdpa_dev = dev->vdpa_dev;
     471         [ #  # ]:          0 :         if (vdpa_dev)
     472                 :          0 :                 vdpa_dev->ops->set_features(dev->vid);
     473                 :            : 
     474                 :          0 :         dev->flags &= ~VIRTIO_DEV_FEATURES_FAILED;
     475                 :          0 :         return RTE_VHOST_MSG_RESULT_OK;
     476                 :            : }
     477                 :            : 
     478                 :            : /*
     479                 :            :  * The virtio device sends us the size of the descriptor ring.
     480                 :            :  */
     481                 :            : static int
     482                 :          0 : vhost_user_set_vring_num(struct virtio_net **pdev,
     483                 :            :                         struct vhu_msg_context *ctx,
     484                 :            :                         int main_fd __rte_unused)
     485                 :            : {
     486                 :          0 :         struct virtio_net *dev = *pdev;
     487                 :          0 :         struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index];
     488                 :            : 
     489         [ #  # ]:          0 :         if (ctx->msg.payload.state.num > 32768) {
     490                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
     491                 :            :                         "invalid virtqueue size %u",
     492                 :            :                         ctx->msg.payload.state.num);
     493                 :          0 :                 return RTE_VHOST_MSG_RESULT_ERR;
     494                 :            :         }
     495                 :            : 
     496         [ #  # ]:          0 :         vq->size = ctx->msg.payload.state.num;
     497                 :            : 
     498                 :            :         /* VIRTIO 1.0, 2.4 Virtqueues says:
     499                 :            :          *
     500                 :            :          *   Queue Size value is always a power of 2. The maximum Queue Size
     501                 :            :          *   value is 32768.
     502                 :            :          *
     503                 :            :          * VIRTIO 1.1 2.7 Virtqueues says:
     504                 :            :          *
     505                 :            :          *   Packed virtqueues support up to 2^15 entries each.
     506                 :            :          */
     507         [ #  # ]:          0 :         if (!vq_is_packed(dev)) {
     508         [ #  # ]:          0 :                 if (vq->size & (vq->size - 1)) {
     509                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
     510                 :            :                                 "invalid virtqueue size %u",
     511                 :            :                                 vq->size);
     512                 :          0 :                         return RTE_VHOST_MSG_RESULT_ERR;
     513                 :            :                 }
     514                 :            :         }
     515                 :            : 
     516         [ #  # ]:          0 :         if (vq_is_packed(dev)) {
     517                 :          0 :                 rte_free(vq->shadow_used_packed);
     518                 :          0 :                 vq->shadow_used_packed = rte_malloc_socket(NULL,
     519                 :          0 :                                 vq->size *
     520                 :            :                                 sizeof(struct vring_used_elem_packed),
     521                 :            :                                 RTE_CACHE_LINE_SIZE, vq->numa_node);
     522         [ #  # ]:          0 :                 if (!vq->shadow_used_packed) {
     523                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
     524                 :            :                                 "failed to allocate memory for shadow used ring.");
     525                 :          0 :                         return RTE_VHOST_MSG_RESULT_ERR;
     526                 :            :                 }
     527                 :            : 
     528                 :            :         } else {
     529                 :          0 :                 rte_free(vq->shadow_used_split);
     530                 :            : 
     531                 :          0 :                 vq->shadow_used_split = rte_malloc_socket(NULL,
     532                 :          0 :                                 vq->size * sizeof(struct vring_used_elem),
     533                 :            :                                 RTE_CACHE_LINE_SIZE, vq->numa_node);
     534                 :            : 
     535         [ #  # ]:          0 :                 if (!vq->shadow_used_split) {
     536                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
     537                 :            :                                 "failed to allocate memory for vq internal data.");
     538                 :          0 :                         return RTE_VHOST_MSG_RESULT_ERR;
     539                 :            :                 }
     540                 :            :         }
     541                 :            : 
     542                 :          0 :         rte_free(vq->batch_copy_elems);
     543                 :          0 :         vq->batch_copy_elems = rte_malloc_socket(NULL,
     544                 :          0 :                                 vq->size * sizeof(struct batch_copy_elem),
     545                 :            :                                 RTE_CACHE_LINE_SIZE, vq->numa_node);
     546         [ #  # ]:          0 :         if (!vq->batch_copy_elems) {
     547                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
     548                 :            :                         "failed to allocate memory for batching copy.");
     549                 :          0 :                 return RTE_VHOST_MSG_RESULT_ERR;
     550                 :            :         }
     551                 :            : 
     552                 :            :         return RTE_VHOST_MSG_RESULT_OK;
     553                 :            : }
     554                 :            : 
     555                 :            : /*
     556                 :            :  * Reallocate virtio_dev, vhost_virtqueue and related data structures to
     557                 :            :  * make them on the same numa node as the memory of vring descriptor.
     558                 :            :  */
     559                 :            : #ifdef RTE_LIBRTE_VHOST_NUMA
     560                 :            : static void
     561                 :          0 : numa_realloc(struct virtio_net **pdev, struct vhost_virtqueue **pvq)
     562                 :            : {
     563                 :            :         int node, dev_node;
     564                 :            :         struct virtio_net *dev;
     565                 :            :         struct vhost_virtqueue *vq;
     566                 :            :         struct batch_copy_elem *bce;
     567                 :            :         struct guest_page *gp;
     568                 :            :         struct rte_vhost_memory *mem;
     569                 :            :         size_t mem_size;
     570                 :            :         int ret;
     571                 :            : 
     572                 :          0 :         dev = *pdev;
     573                 :          0 :         vq = *pvq;
     574                 :            : 
     575                 :            :         /*
     576                 :            :          * If VQ is ready, it is too late to reallocate, it certainly already
     577                 :            :          * happened anyway on VHOST_USER_SET_VRING_ADRR.
     578                 :            :          */
     579         [ #  # ]:          0 :         if (vq->ready)
     580                 :          0 :                 return;
     581                 :            : 
     582                 :          0 :         ret = get_mempolicy(&node, NULL, 0, vq->desc, MPOL_F_NODE | MPOL_F_ADDR);
     583         [ #  # ]:          0 :         if (ret) {
     584                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
     585                 :            :                         "unable to get virtqueue %d numa information.",
     586                 :            :                         vq->index);
     587                 :          0 :                 return;
     588                 :            :         }
     589                 :            : 
     590         [ #  # ]:          0 :         if (node == vq->numa_node)
     591                 :          0 :                 goto out_dev_realloc;
     592                 :            : 
     593                 :          0 :         vq = rte_realloc_socket(*pvq, sizeof(**pvq), 0, node);
     594         [ #  # ]:          0 :         if (!vq) {
     595                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
     596                 :            :                         "failed to realloc virtqueue %d on node %d",
     597                 :            :                         (*pvq)->index, node);
     598                 :          0 :                 return;
     599                 :            :         }
     600                 :          0 :         *pvq = vq;
     601                 :            : 
     602         [ #  # ]:          0 :         if (vq != dev->virtqueue[vq->index]) {
     603                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, INFO, "reallocated virtqueue on node %d", node);
     604                 :          0 :                 dev->virtqueue[vq->index] = vq;
     605                 :            :         }
     606                 :            : 
     607         [ #  # ]:          0 :         if (vq_is_packed(dev)) {
     608                 :            :                 struct vring_used_elem_packed *sup;
     609                 :            : 
     610                 :          0 :                 sup = rte_realloc_socket(vq->shadow_used_packed, vq->size * sizeof(*sup),
     611                 :            :                                 RTE_CACHE_LINE_SIZE, node);
     612         [ #  # ]:          0 :                 if (!sup) {
     613                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
     614                 :            :                                 "failed to realloc shadow packed on node %d",
     615                 :            :                                 node);
     616                 :          0 :                         return;
     617                 :            :                 }
     618                 :          0 :                 vq->shadow_used_packed = sup;
     619                 :            :         } else {
     620                 :            :                 struct vring_used_elem *sus;
     621                 :            : 
     622                 :          0 :                 sus = rte_realloc_socket(vq->shadow_used_split, vq->size * sizeof(*sus),
     623                 :            :                                 RTE_CACHE_LINE_SIZE, node);
     624         [ #  # ]:          0 :                 if (!sus) {
     625                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
     626                 :            :                                 "failed to realloc shadow split on node %d",
     627                 :            :                                 node);
     628                 :          0 :                         return;
     629                 :            :                 }
     630                 :          0 :                 vq->shadow_used_split = sus;
     631                 :            :         }
     632                 :            : 
     633                 :          0 :         bce = rte_realloc_socket(vq->batch_copy_elems, vq->size * sizeof(*bce),
     634                 :            :                         RTE_CACHE_LINE_SIZE, node);
     635         [ #  # ]:          0 :         if (!bce) {
     636                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
     637                 :            :                         "failed to realloc batch copy elem on node %d",
     638                 :            :                         node);
     639                 :          0 :                 return;
     640                 :            :         }
     641                 :          0 :         vq->batch_copy_elems = bce;
     642                 :            : 
     643         [ #  # ]:          0 :         if (vq->log_cache) {
     644                 :            :                 struct log_cache_entry *lc;
     645                 :            : 
     646                 :          0 :                 lc = rte_realloc_socket(vq->log_cache, sizeof(*lc) * VHOST_LOG_CACHE_NR, 0, node);
     647         [ #  # ]:          0 :                 if (!lc) {
     648                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
     649                 :            :                                 "failed to realloc log cache on node %d",
     650                 :            :                                 node);
     651                 :          0 :                         return;
     652                 :            :                 }
     653                 :          0 :                 vq->log_cache = lc;
     654                 :            :         }
     655                 :            : 
     656         [ #  # ]:          0 :         if (vq->resubmit_inflight) {
     657                 :            :                 struct rte_vhost_resubmit_info *ri;
     658                 :            : 
     659                 :          0 :                 ri = rte_realloc_socket(vq->resubmit_inflight, sizeof(*ri), 0, node);
     660         [ #  # ]:          0 :                 if (!ri) {
     661                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
     662                 :            :                                 "failed to realloc resubmit inflight on node %d",
     663                 :            :                                 node);
     664                 :          0 :                         return;
     665                 :            :                 }
     666                 :          0 :                 vq->resubmit_inflight = ri;
     667                 :            : 
     668         [ #  # ]:          0 :                 if (ri->resubmit_list) {
     669                 :            :                         struct rte_vhost_resubmit_desc *rd;
     670                 :            : 
     671                 :          0 :                         rd = rte_realloc_socket(ri->resubmit_list, sizeof(*rd) * ri->resubmit_num,
     672                 :            :                                         0, node);
     673         [ #  # ]:          0 :                         if (!rd) {
     674                 :          0 :                                 VHOST_CONFIG_LOG(dev->ifname, ERR,
     675                 :            :                                         "failed to realloc resubmit list on node %d",
     676                 :            :                                         node);
     677                 :          0 :                                 return;
     678                 :            :                         }
     679                 :          0 :                         ri->resubmit_list = rd;
     680                 :            :                 }
     681                 :            :         }
     682                 :            : 
     683                 :          0 :         vq->numa_node = node;
     684                 :            : 
     685                 :          0 : out_dev_realloc:
     686                 :            : 
     687         [ #  # ]:          0 :         if (dev->flags & VIRTIO_DEV_RUNNING)
     688                 :            :                 return;
     689                 :            : 
     690                 :          0 :         ret = get_mempolicy(&dev_node, NULL, 0, dev, MPOL_F_NODE | MPOL_F_ADDR);
     691         [ #  # ]:          0 :         if (ret) {
     692                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "unable to get numa information.");
     693                 :          0 :                 return;
     694                 :            :         }
     695                 :            : 
     696         [ #  # ]:          0 :         if (dev_node == node)
     697                 :            :                 return;
     698                 :            : 
     699                 :          0 :         dev = rte_realloc_socket(*pdev, sizeof(**pdev), 0, node);
     700         [ #  # ]:          0 :         if (!dev) {
     701                 :          0 :                 VHOST_CONFIG_LOG((*pdev)->ifname, ERR, "failed to realloc dev on node %d", node);
     702                 :          0 :                 return;
     703                 :            :         }
     704                 :          0 :         *pdev = dev;
     705                 :            : 
     706                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO, "reallocated device on node %d", node);
     707                 :          0 :         vhost_devices[dev->vid] = dev;
     708                 :            : 
     709                 :          0 :         mem_size = sizeof(struct rte_vhost_memory) +
     710                 :          0 :                 sizeof(struct rte_vhost_mem_region) * dev->mem->nregions;
     711                 :          0 :         mem = rte_realloc_socket(dev->mem, mem_size, 0, node);
     712         [ #  # ]:          0 :         if (!mem) {
     713                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
     714                 :            :                         "failed to realloc mem table on node %d",
     715                 :            :                         node);
     716                 :          0 :                 return;
     717                 :            :         }
     718                 :          0 :         dev->mem = mem;
     719                 :            : 
     720                 :          0 :         gp = rte_realloc_socket(dev->guest_pages, dev->max_guest_pages * sizeof(*gp),
     721                 :            :                         RTE_CACHE_LINE_SIZE, node);
     722         [ #  # ]:          0 :         if (!gp) {
     723                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
     724                 :            :                         "failed to realloc guest pages on node %d",
     725                 :            :                         node);
     726                 :          0 :                 return;
     727                 :            :         }
     728                 :          0 :         dev->guest_pages = gp;
     729                 :            : 
     730                 :          0 :         vhost_user_iotlb_init(dev);
     731                 :            : }
     732                 :            : #else
     733                 :            : static void
     734                 :            : numa_realloc(struct virtio_net **pdev, struct vhost_virtqueue **pvq)
     735                 :            : {
     736                 :            :         RTE_SET_USED(pdev);
     737                 :            :         RTE_SET_USED(pvq);
     738                 :            : }
     739                 :            : #endif
     740                 :            : 
     741                 :            : /* Converts QEMU virtual address to Vhost virtual address. */
     742                 :            : static uint64_t
     743                 :          0 : qva_to_vva(struct virtio_net *dev, uint64_t qva, uint64_t *len)
     744                 :            : {
     745                 :            :         struct rte_vhost_mem_region *r;
     746                 :            :         uint32_t i;
     747                 :            : 
     748   [ #  #  #  # ]:          0 :         if (unlikely(!dev || !dev->mem))
     749                 :          0 :                 goto out_error;
     750                 :            : 
     751                 :            :         /* Find the region where the address lives. */
     752         [ #  # ]:          0 :         for (i = 0; i < dev->mem->nregions; i++) {
     753                 :            :                 r = &dev->mem->regions[i];
     754                 :            : 
     755         [ #  # ]:          0 :                 if (qva >= r->guest_user_addr &&
     756         [ #  # ]:          0 :                     qva <  r->guest_user_addr + r->size) {
     757                 :            : 
     758         [ #  # ]:          0 :                         if (unlikely(*len > r->guest_user_addr + r->size - qva))
     759                 :          0 :                                 *len = r->guest_user_addr + r->size - qva;
     760                 :            : 
     761                 :          0 :                         return qva - r->guest_user_addr +
     762                 :          0 :                                r->host_user_addr;
     763                 :            :                 }
     764                 :            :         }
     765                 :          0 : out_error:
     766                 :          0 :         *len = 0;
     767                 :            : 
     768                 :          0 :         return 0;
     769                 :            : }
     770                 :            : 
     771                 :            : 
     772                 :            : /*
     773                 :            :  * Converts ring address to Vhost virtual address.
     774                 :            :  * If IOMMU is enabled, the ring address is a guest IO virtual address,
     775                 :            :  * else it is a QEMU virtual address.
     776                 :            :  */
     777                 :            : static uint64_t
     778                 :          0 : ring_addr_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
     779                 :            :                 uint64_t ra, uint64_t *size)
     780                 :            : {
     781         [ #  # ]:          0 :         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
     782                 :            :                 uint64_t vva;
     783                 :            : 
     784                 :            :                 vhost_user_iotlb_rd_lock(vq);
     785                 :            :                 vva = vhost_iova_to_vva(dev, vq, ra,
     786                 :            :                                         size, VHOST_ACCESS_RW);
     787                 :            :                 vhost_user_iotlb_rd_unlock(vq);
     788                 :            : 
     789                 :          0 :                 return vva;
     790                 :            :         }
     791                 :            : 
     792                 :          0 :         return qva_to_vva(dev, ra, size);
     793                 :            : }
     794                 :            : 
     795                 :            : static uint64_t
     796                 :          0 : log_addr_to_gpa(struct virtio_net *dev, struct vhost_virtqueue *vq)
     797                 :            : {
     798                 :            :         uint64_t log_gpa;
     799                 :            : 
     800                 :            :         vhost_user_iotlb_rd_lock(vq);
     801                 :          0 :         log_gpa = translate_log_addr(dev, vq, vq->ring_addrs.log_guest_addr);
     802                 :            :         vhost_user_iotlb_rd_unlock(vq);
     803                 :            : 
     804                 :          0 :         return log_gpa;
     805                 :            : }
     806                 :            : 
     807                 :            : static uint64_t
     808                 :          0 : hua_to_alignment(struct rte_vhost_memory *mem, void *ptr)
     809                 :            : {
     810                 :            :         struct rte_vhost_mem_region *r;
     811                 :            :         uint32_t i;
     812                 :          0 :         uintptr_t hua = (uintptr_t)ptr;
     813                 :            : 
     814         [ #  # ]:          0 :         for (i = 0; i < mem->nregions; i++) {
     815                 :            :                 r = &mem->regions[i];
     816         [ #  # ]:          0 :                 if (hua >= r->host_user_addr &&
     817         [ #  # ]:          0 :                         hua < r->host_user_addr + r->size) {
     818                 :          0 :                         return get_blk_size(r->fd);
     819                 :            :                 }
     820                 :            :         }
     821                 :            : 
     822                 :            :         /* If region isn't found, don't align at all */
     823                 :            :         return 1;
     824                 :            : }
     825                 :            : 
     826                 :            : void
     827                 :          0 : mem_set_dump(struct virtio_net *dev, void *ptr, size_t size, bool enable, uint64_t pagesz)
     828                 :            : {
     829                 :            : #ifdef MADV_DONTDUMP
     830                 :          0 :         void *start = RTE_PTR_ALIGN_FLOOR(ptr, pagesz);
     831                 :          0 :         uintptr_t end = RTE_ALIGN_CEIL((uintptr_t)ptr + size, pagesz);
     832                 :          0 :         size_t len = end - (uintptr_t)start;
     833                 :            : 
     834   [ #  #  #  # ]:          0 :         if (madvise(start, len, enable ? MADV_DODUMP : MADV_DONTDUMP) == -1) {
     835                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, INFO,
     836                 :            :                         "could not set coredump preference (%s).", strerror(errno));
     837                 :            :         }
     838                 :            : #endif
     839                 :          0 : }
     840                 :            : 
     841                 :            : static void
     842                 :          0 : translate_ring_addresses(struct virtio_net **pdev, struct vhost_virtqueue **pvq)
     843                 :            : {
     844                 :            :         struct vhost_virtqueue *vq;
     845                 :            :         struct virtio_net *dev;
     846                 :            :         uint64_t len, expected_len;
     847                 :            : 
     848                 :          0 :         dev = *pdev;
     849                 :          0 :         vq = *pvq;
     850                 :            : 
     851                 :          0 :         vq_assert_lock(dev, vq);
     852                 :            : 
     853         [ #  # ]:          0 :         if (vq->ring_addrs.flags & (1 << VHOST_VRING_F_LOG)) {
     854                 :          0 :                 vq->log_guest_addr =
     855                 :          0 :                         log_addr_to_gpa(dev, vq);
     856         [ #  # ]:          0 :                 if (vq->log_guest_addr == 0) {
     857                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, DEBUG, "failed to map log_guest_addr.");
     858                 :          0 :                         return;
     859                 :            :                 }
     860                 :            :         }
     861                 :            : 
     862         [ #  # ]:          0 :         if (vq_is_packed(dev)) {
     863                 :          0 :                 len = sizeof(struct vring_packed_desc) * vq->size;
     864                 :          0 :                 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
     865                 :          0 :                         ring_addr_to_vva(dev, vq, vq->ring_addrs.desc_user_addr, &len);
     866         [ #  # ]:          0 :                 if (vq->desc_packed == NULL ||
     867                 :          0 :                                 len != sizeof(struct vring_packed_desc) *
     868         [ #  # ]:          0 :                                 vq->size) {
     869                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, DEBUG, "failed to map desc_packed ring.");
     870                 :          0 :                         return;
     871                 :            :                 }
     872                 :            : 
     873                 :          0 :                 mem_set_dump(dev, vq->desc_packed, len, true,
     874                 :            :                         hua_to_alignment(dev->mem, vq->desc_packed));
     875                 :          0 :                 numa_realloc(&dev, &vq);
     876                 :          0 :                 *pdev = dev;
     877                 :          0 :                 *pvq = vq;
     878                 :            : 
     879                 :          0 :                 len = sizeof(struct vring_packed_desc_event);
     880                 :          0 :                 vq->driver_event = (struct vring_packed_desc_event *)
     881                 :          0 :                                         (uintptr_t)ring_addr_to_vva(dev,
     882                 :          0 :                                         vq, vq->ring_addrs.avail_user_addr, &len);
     883         [ #  # ]:          0 :                 if (vq->driver_event == NULL ||
     884         [ #  # ]:          0 :                                 len != sizeof(struct vring_packed_desc_event)) {
     885                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, DEBUG,
     886                 :            :                                 "failed to find driver area address.");
     887                 :          0 :                         return;
     888                 :            :                 }
     889                 :            : 
     890                 :          0 :                 mem_set_dump(dev, vq->driver_event, len, true,
     891                 :            :                         hua_to_alignment(dev->mem, vq->driver_event));
     892                 :          0 :                 len = sizeof(struct vring_packed_desc_event);
     893                 :          0 :                 vq->device_event = (struct vring_packed_desc_event *)
     894                 :          0 :                                         (uintptr_t)ring_addr_to_vva(dev,
     895                 :          0 :                                         vq, vq->ring_addrs.used_user_addr, &len);
     896         [ #  # ]:          0 :                 if (vq->device_event == NULL ||
     897         [ #  # ]:          0 :                                 len != sizeof(struct vring_packed_desc_event)) {
     898                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, DEBUG,
     899                 :            :                                 "failed to find device area address.");
     900                 :          0 :                         return;
     901                 :            :                 }
     902                 :            : 
     903                 :          0 :                 mem_set_dump(dev, vq->device_event, len, true,
     904                 :            :                         hua_to_alignment(dev->mem, vq->device_event));
     905                 :          0 :                 vq->access_ok = true;
     906                 :          0 :                 return;
     907                 :            :         }
     908                 :            : 
     909                 :            :         /* The addresses are converted from QEMU virtual to Vhost virtual. */
     910   [ #  #  #  #  :          0 :         if (vq->desc && vq->avail && vq->used)
                   #  # ]
     911                 :            :                 return;
     912                 :            : 
     913                 :          0 :         len = sizeof(struct vring_desc) * vq->size;
     914                 :          0 :         vq->desc = (struct vring_desc *)(uintptr_t)ring_addr_to_vva(dev,
     915                 :          0 :                         vq, vq->ring_addrs.desc_user_addr, &len);
     916   [ #  #  #  # ]:          0 :         if (vq->desc == 0 || len != sizeof(struct vring_desc) * vq->size) {
     917                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, DEBUG, "failed to map desc ring.");
     918                 :          0 :                 return;
     919                 :            :         }
     920                 :            : 
     921                 :          0 :         mem_set_dump(dev, vq->desc, len, true, hua_to_alignment(dev->mem, vq->desc));
     922                 :          0 :         numa_realloc(&dev, &vq);
     923                 :          0 :         *pdev = dev;
     924                 :          0 :         *pvq = vq;
     925                 :            : 
     926                 :          0 :         len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
     927         [ #  # ]:          0 :         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
     928                 :          0 :                 len += sizeof(uint16_t);
     929                 :          0 :         expected_len = len;
     930                 :          0 :         vq->avail = (struct vring_avail *)(uintptr_t)ring_addr_to_vva(dev,
     931                 :          0 :                         vq, vq->ring_addrs.avail_user_addr, &len);
     932   [ #  #  #  # ]:          0 :         if (vq->avail == 0 || len != expected_len) {
     933                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, DEBUG, "failed to map avail ring.");
     934                 :          0 :                 return;
     935                 :            :         }
     936                 :            : 
     937                 :          0 :         mem_set_dump(dev, vq->avail, len, true, hua_to_alignment(dev->mem, vq->avail));
     938                 :          0 :         len = sizeof(struct vring_used) +
     939                 :          0 :                 sizeof(struct vring_used_elem) * vq->size;
     940         [ #  # ]:          0 :         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
     941                 :          0 :                 len += sizeof(uint16_t);
     942                 :          0 :         expected_len = len;
     943                 :          0 :         vq->used = (struct vring_used *)(uintptr_t)ring_addr_to_vva(dev,
     944                 :          0 :                         vq, vq->ring_addrs.used_user_addr, &len);
     945   [ #  #  #  # ]:          0 :         if (vq->used == 0 || len != expected_len) {
     946                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, DEBUG, "failed to map used ring.");
     947                 :          0 :                 return;
     948                 :            :         }
     949                 :            : 
     950                 :          0 :         mem_set_dump(dev, vq->used, len, true, hua_to_alignment(dev->mem, vq->used));
     951                 :            : 
     952         [ #  # ]:          0 :         if (vq->last_used_idx != vq->used->idx) {
     953                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, WARNING,
     954                 :            :                         "last_used_idx (%u) and vq->used->idx (%u) mismatches;",
     955                 :            :                         vq->last_used_idx, vq->used->idx);
     956                 :          0 :                 vq->last_used_idx  = vq->used->idx;
     957         [ #  # ]:          0 :                 vq->last_avail_idx = vq->used->idx;
     958                 :            :                 vhost_virtqueue_reconnect_log_split(vq);
     959                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, WARNING,
     960                 :            :                         "some packets maybe resent for Tx and dropped for Rx");
     961                 :            :         }
     962                 :            : 
     963                 :          0 :         vq->access_ok = true;
     964                 :            : 
     965                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, DEBUG, "mapped address desc: %p", vq->desc);
     966                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, DEBUG, "mapped address avail: %p", vq->avail);
     967                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, DEBUG, "mapped address used: %p", vq->used);
     968                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, DEBUG, "log_guest_addr: %" PRIx64, vq->log_guest_addr);
     969                 :            : }
     970                 :            : 
     971                 :            : /*
     972                 :            :  * The virtio device sends us the desc, used and avail ring addresses.
     973                 :            :  * This function then converts these to our address space.
     974                 :            :  */
     975                 :            : static int
     976                 :          0 : vhost_user_set_vring_addr(struct virtio_net **pdev,
     977                 :            :                         struct vhu_msg_context *ctx,
     978                 :            :                         int main_fd __rte_unused)
     979                 :            : {
     980                 :          0 :         struct virtio_net *dev = *pdev;
     981                 :            :         struct vhost_virtqueue *vq;
     982                 :          0 :         struct vhost_vring_addr *addr = &ctx->msg.payload.addr;
     983                 :            :         bool access_ok;
     984                 :            : 
     985         [ #  # ]:          0 :         if (dev->mem == NULL)
     986                 :            :                 return RTE_VHOST_MSG_RESULT_ERR;
     987                 :            : 
     988                 :            :         /* addr->index refers to the queue index. The txq 1, rxq is 0. */
     989                 :          0 :         vq = dev->virtqueue[ctx->msg.payload.addr.index];
     990                 :            : 
     991                 :            :         /*
     992                 :            :          * Rings addresses should not be interpreted as long as the ring is not
     993                 :            :          * started and enabled
     994                 :            :          */
     995         [ #  # ]:          0 :         memcpy(&vq->ring_addrs, addr, sizeof(*addr));
     996                 :            : 
     997         [ #  # ]:          0 :         if (dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)
     998                 :          0 :                 goto out;
     999                 :            : 
    1000                 :            :         /* vhost_user_lock_all_queue_pairs locked all qps */
    1001                 :          0 :         VHOST_USER_ASSERT_LOCK(dev, vq, VHOST_USER_SET_VRING_ADDR);
    1002                 :            : 
    1003                 :          0 :         access_ok = vq->access_ok;
    1004                 :            : 
    1005                 :          0 :         vring_invalidate(dev, vq);
    1006                 :            : 
    1007   [ #  #  #  # ]:          0 :         if ((vq->enabled && (dev->features &
    1008         [ #  # ]:          0 :                                 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) ||
    1009                 :            :                         access_ok) {
    1010                 :          0 :                 translate_ring_addresses(&dev, &vq);
    1011                 :          0 :                 *pdev = dev;
    1012                 :            :         }
    1013                 :            : 
    1014                 :          0 : out:
    1015                 :            :         return RTE_VHOST_MSG_RESULT_OK;
    1016                 :            : }
    1017                 :            : 
    1018                 :            : /*
    1019                 :            :  * The virtio device sends us the available ring last used index.
    1020                 :            :  */
    1021                 :            : static int
    1022                 :          0 : vhost_user_set_vring_base(struct virtio_net **pdev,
    1023                 :            :                         struct vhu_msg_context *ctx,
    1024                 :            :                         int main_fd __rte_unused)
    1025                 :            : {
    1026                 :          0 :         struct virtio_net *dev = *pdev;
    1027                 :          0 :         struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index];
    1028         [ #  # ]:          0 :         uint64_t val = ctx->msg.payload.state.num;
    1029                 :            : 
    1030         [ #  # ]:          0 :         if (vq_is_packed(dev)) {
    1031                 :            :                 /*
    1032                 :            :                  * Bit[0:14]: avail index
    1033                 :            :                  * Bit[15]: avail wrap counter
    1034                 :            :                  */
    1035                 :          0 :                 vq->last_avail_idx = val & 0x7fff;
    1036                 :          0 :                 vq->avail_wrap_counter = !!(val & (0x1 << 15));
    1037                 :            :                 /*
    1038                 :            :                  * Set used index to same value as available one, as
    1039                 :            :                  * their values should be the same since ring processing
    1040                 :            :                  * was stopped at get time.
    1041                 :            :                  */
    1042                 :          0 :                 vq->last_used_idx = vq->last_avail_idx;
    1043         [ #  # ]:          0 :                 vq->used_wrap_counter = vq->avail_wrap_counter;
    1044                 :            :                 vhost_virtqueue_reconnect_log_packed(vq);
    1045                 :            :         } else {
    1046                 :          0 :                 vq->last_used_idx = ctx->msg.payload.state.num;
    1047         [ #  # ]:          0 :                 vq->last_avail_idx = ctx->msg.payload.state.num;
    1048                 :            :                 vhost_virtqueue_reconnect_log_split(vq);
    1049                 :            :         }
    1050                 :            : 
    1051                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    1052                 :            :                 "vring base idx:%u last_used_idx:%u last_avail_idx:%u.",
    1053                 :            :                 ctx->msg.payload.state.index, vq->last_used_idx, vq->last_avail_idx);
    1054                 :            : 
    1055                 :          0 :         return RTE_VHOST_MSG_RESULT_OK;
    1056                 :            : }
    1057                 :            : 
    1058                 :            : static int
    1059                 :          0 : add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
    1060                 :            :                    uint64_t host_iova, uint64_t host_user_addr, uint64_t size)
    1061                 :            : {
    1062                 :            :         struct guest_page *page, *last_page;
    1063                 :            :         struct guest_page *old_pages;
    1064                 :            : 
    1065         [ #  # ]:          0 :         if (dev->nr_guest_pages == dev->max_guest_pages) {
    1066                 :          0 :                 dev->max_guest_pages *= 2;
    1067                 :          0 :                 old_pages = dev->guest_pages;
    1068                 :          0 :                 dev->guest_pages = rte_realloc(dev->guest_pages,
    1069                 :          0 :                                         dev->max_guest_pages * sizeof(*page),
    1070                 :            :                                         RTE_CACHE_LINE_SIZE);
    1071         [ #  # ]:          0 :                 if (dev->guest_pages == NULL) {
    1072                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR, "cannot realloc guest_pages");
    1073                 :          0 :                         rte_free(old_pages);
    1074                 :          0 :                         return -1;
    1075                 :            :                 }
    1076                 :            :         }
    1077                 :            : 
    1078         [ #  # ]:          0 :         if (dev->nr_guest_pages > 0) {
    1079                 :          0 :                 last_page = &dev->guest_pages[dev->nr_guest_pages - 1];
    1080                 :            :                 /* merge if the two pages are continuous */
    1081         [ #  # ]:          0 :                 if (host_iova == last_page->host_iova + last_page->size &&
    1082         [ #  # ]:          0 :                     guest_phys_addr == last_page->guest_phys_addr + last_page->size &&
    1083         [ #  # ]:          0 :                     host_user_addr == last_page->host_user_addr + last_page->size) {
    1084                 :          0 :                         last_page->size += size;
    1085                 :          0 :                         return 0;
    1086                 :            :                 }
    1087                 :            :         }
    1088                 :            : 
    1089                 :          0 :         page = &dev->guest_pages[dev->nr_guest_pages++];
    1090                 :          0 :         page->guest_phys_addr = guest_phys_addr;
    1091                 :          0 :         page->host_iova  = host_iova;
    1092                 :          0 :         page->host_user_addr = host_user_addr;
    1093                 :          0 :         page->size = size;
    1094                 :            : 
    1095                 :          0 :         return 0;
    1096                 :            : }
    1097                 :            : 
    1098                 :            : static int
    1099                 :          0 : add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
    1100                 :            :                 uint64_t page_size)
    1101                 :            : {
    1102                 :          0 :         uint64_t reg_size = reg->size;
    1103                 :          0 :         uint64_t host_user_addr  = reg->host_user_addr;
    1104                 :          0 :         uint64_t guest_phys_addr = reg->guest_phys_addr;
    1105                 :            :         uint64_t host_iova;
    1106                 :            :         uint64_t size;
    1107                 :            : 
    1108                 :          0 :         host_iova = rte_mem_virt2iova((void *)(uintptr_t)host_user_addr);
    1109                 :          0 :         size = page_size - (guest_phys_addr & (page_size - 1));
    1110                 :          0 :         size = RTE_MIN(size, reg_size);
    1111                 :            : 
    1112         [ #  # ]:          0 :         if (add_one_guest_page(dev, guest_phys_addr, host_iova,
    1113                 :            :                                host_user_addr, size) < 0)
    1114                 :            :                 return -1;
    1115                 :            : 
    1116                 :          0 :         host_user_addr  += size;
    1117                 :          0 :         guest_phys_addr += size;
    1118                 :          0 :         reg_size -= size;
    1119                 :            : 
    1120         [ #  # ]:          0 :         while (reg_size > 0) {
    1121                 :          0 :                 size = RTE_MIN(reg_size, page_size);
    1122                 :          0 :                 host_iova = rte_mem_virt2iova((void *)(uintptr_t)
    1123                 :            :                                                   host_user_addr);
    1124         [ #  # ]:          0 :                 if (add_one_guest_page(dev, guest_phys_addr, host_iova,
    1125                 :            :                                        host_user_addr, size) < 0)
    1126                 :            :                         return -1;
    1127                 :            : 
    1128                 :          0 :                 host_user_addr  += size;
    1129                 :          0 :                 guest_phys_addr += size;
    1130                 :          0 :                 reg_size -= size;
    1131                 :            :         }
    1132                 :            : 
    1133                 :            :         /* sort guest page array if over binary search threshold */
    1134         [ #  # ]:          0 :         if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
    1135                 :          0 :                 qsort((void *)dev->guest_pages, dev->nr_guest_pages,
    1136                 :            :                         sizeof(struct guest_page), guest_page_addrcmp);
    1137                 :            :         }
    1138                 :            : 
    1139                 :            :         return 0;
    1140                 :            : }
    1141                 :            : 
    1142                 :            : #ifdef RTE_LIBRTE_VHOST_DEBUG
    1143                 :            : /* TODO: enable it only in debug mode? */
    1144                 :            : static void
    1145                 :            : dump_guest_pages(struct virtio_net *dev)
    1146                 :            : {
    1147                 :            :         uint32_t i;
    1148                 :            :         struct guest_page *page;
    1149                 :            : 
    1150                 :            :         for (i = 0; i < dev->nr_guest_pages; i++) {
    1151                 :            :                 page = &dev->guest_pages[i];
    1152                 :            : 
    1153                 :            :                 VHOST_CONFIG_LOG(dev->ifname, INFO, "guest physical page region %u", i);
    1154                 :            :                 VHOST_CONFIG_LOG(dev->ifname, INFO, "\tguest_phys_addr: %" PRIx64,
    1155                 :            :                         page->guest_phys_addr);
    1156                 :            :                 VHOST_CONFIG_LOG(dev->ifname, INFO, "\thost_iova : %" PRIx64,
    1157                 :            :                         page->host_iova);
    1158                 :            :                 VHOST_CONFIG_LOG(dev->ifname, INFO, "\tsize           : %" PRIx64,
    1159                 :            :                         page->size);
    1160                 :            :         }
    1161                 :            : }
    1162                 :            : #else
    1163                 :            : #define dump_guest_pages(dev)
    1164                 :            : #endif
    1165                 :            : 
    1166                 :            : static bool
    1167                 :          0 : vhost_memory_changed(struct VhostUserMemory *new,
    1168                 :            :                      struct rte_vhost_memory *old)
    1169                 :            : {
    1170                 :            :         uint32_t i;
    1171                 :            : 
    1172         [ #  # ]:          0 :         if (new->nregions != old->nregions)
    1173                 :            :                 return true;
    1174                 :            : 
    1175         [ #  # ]:          0 :         for (i = 0; i < new->nregions; ++i) {
    1176                 :            :                 VhostUserMemoryRegion *new_r = &new->regions[i];
    1177                 :            :                 struct rte_vhost_mem_region *old_r = &old->regions[i];
    1178                 :            : 
    1179         [ #  # ]:          0 :                 if (new_r->guest_phys_addr != old_r->guest_phys_addr)
    1180                 :            :                         return true;
    1181         [ #  # ]:          0 :                 if (new_r->memory_size != old_r->size)
    1182                 :            :                         return true;
    1183         [ #  # ]:          0 :                 if (new_r->userspace_addr != old_r->guest_user_addr)
    1184                 :            :                         return true;
    1185                 :            :         }
    1186                 :            : 
    1187                 :            :         return false;
    1188                 :            : }
    1189                 :            : 
    1190                 :            : #ifdef RTE_LIBRTE_VHOST_POSTCOPY
    1191                 :            : static int
    1192                 :          0 : vhost_user_postcopy_region_register(struct virtio_net *dev,
    1193                 :            :                 struct rte_vhost_mem_region *reg)
    1194                 :            : {
    1195                 :            :         struct uffdio_register reg_struct;
    1196                 :            : 
    1197                 :            :         /*
    1198                 :            :          * Let's register all the mmapped area to ensure
    1199                 :            :          * alignment on page boundary.
    1200                 :            :          */
    1201                 :          0 :         reg_struct.range.start = (uint64_t)(uintptr_t)reg->mmap_addr;
    1202                 :          0 :         reg_struct.range.len = reg->mmap_size;
    1203                 :          0 :         reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
    1204                 :            : 
    1205         [ #  # ]:          0 :         if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER,
    1206                 :            :                                 &reg_struct)) {
    1207                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
    1208                 :            :                         "failed to register ufd for region "
    1209                 :            :                         "%" PRIx64 " - %" PRIx64 " (ufd = %d) %s",
    1210                 :            :                         (uint64_t)reg_struct.range.start,
    1211                 :            :                         (uint64_t)reg_struct.range.start +
    1212                 :            :                         (uint64_t)reg_struct.range.len - 1,
    1213                 :            :                         dev->postcopy_ufd,
    1214                 :            :                         strerror(errno));
    1215                 :          0 :                 return -1;
    1216                 :            :         }
    1217                 :            : 
    1218                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    1219                 :            :                 "\t userfaultfd registered for range : %" PRIx64 " - %" PRIx64,
    1220                 :            :                 (uint64_t)reg_struct.range.start,
    1221                 :            :                 (uint64_t)reg_struct.range.start +
    1222                 :            :                 (uint64_t)reg_struct.range.len - 1);
    1223                 :            : 
    1224                 :          0 :         return 0;
    1225                 :            : }
    1226                 :            : #else
    1227                 :            : static int
    1228                 :            : vhost_user_postcopy_region_register(struct virtio_net *dev __rte_unused,
    1229                 :            :                 struct rte_vhost_mem_region *reg __rte_unused)
    1230                 :            : {
    1231                 :            :         return -1;
    1232                 :            : }
    1233                 :            : #endif
    1234                 :            : 
    1235                 :            : static int
    1236                 :          0 : vhost_user_postcopy_register(struct virtio_net *dev, int main_fd,
    1237                 :            :                 struct vhu_msg_context *ctx)
    1238                 :            : {
    1239                 :            :         struct VhostUserMemory *memory;
    1240                 :            :         struct rte_vhost_mem_region *reg;
    1241                 :            :         struct vhu_msg_context ack_ctx;
    1242                 :            :         uint32_t i;
    1243                 :            : 
    1244         [ #  # ]:          0 :         if (!dev->postcopy_listening)
    1245                 :            :                 return 0;
    1246                 :            : 
    1247                 :            :         /*
    1248                 :            :          * We haven't a better way right now than sharing
    1249                 :            :          * DPDK's virtual address with Qemu, so that Qemu can
    1250                 :            :          * retrieve the region offset when handling userfaults.
    1251                 :            :          */
    1252                 :            :         memory = &ctx->msg.payload.memory;
    1253         [ #  # ]:          0 :         for (i = 0; i < memory->nregions; i++) {
    1254                 :          0 :                 reg = &dev->mem->regions[i];
    1255                 :          0 :                 memory->regions[i].userspace_addr = reg->host_user_addr;
    1256                 :            :         }
    1257                 :            : 
    1258                 :            :         /* Send the addresses back to qemu */
    1259                 :          0 :         ctx->fd_num = 0;
    1260                 :          0 :         send_vhost_reply(dev, main_fd, ctx);
    1261                 :            : 
    1262                 :            :         /* Wait for qemu to acknowledge it got the addresses
    1263                 :            :          * we've got to wait before we're allowed to generate faults.
    1264                 :            :          */
    1265         [ #  # ]:          0 :         if (read_vhost_message(dev, main_fd, &ack_ctx) <= 0) {
    1266                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
    1267                 :            :                         "failed to read qemu ack on postcopy set-mem-table");
    1268                 :          0 :                 return -1;
    1269                 :            :         }
    1270                 :            : 
    1271         [ #  # ]:          0 :         if (validate_msg_fds(dev, &ack_ctx, 0) != 0)
    1272                 :            :                 return -1;
    1273                 :            : 
    1274         [ #  # ]:          0 :         if (ack_ctx.msg.request.frontend != VHOST_USER_SET_MEM_TABLE) {
    1275                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
    1276                 :            :                         "bad qemu ack on postcopy set-mem-table (%d)",
    1277                 :            :                         ack_ctx.msg.request.frontend);
    1278                 :          0 :                 return -1;
    1279                 :            :         }
    1280                 :            : 
    1281                 :            :         /* Now userfault register and we can use the memory */
    1282         [ #  # ]:          0 :         for (i = 0; i < memory->nregions; i++) {
    1283                 :          0 :                 reg = &dev->mem->regions[i];
    1284         [ #  # ]:          0 :                 if (vhost_user_postcopy_region_register(dev, reg) < 0)
    1285                 :            :                         return -1;
    1286                 :            :         }
    1287                 :            : 
    1288                 :            :         return 0;
    1289                 :            : }
    1290                 :            : 
    1291                 :            : static int
    1292                 :          0 : vhost_user_mmap_region(struct virtio_net *dev,
    1293                 :            :                 struct rte_vhost_mem_region *region,
    1294                 :            :                 uint64_t mmap_offset)
    1295                 :            : {
    1296                 :            :         void *mmap_addr;
    1297                 :            :         uint64_t mmap_size;
    1298                 :            :         uint64_t alignment;
    1299                 :            :         int populate;
    1300                 :            : 
    1301                 :            :         /* Check for memory_size + mmap_offset overflow */
    1302         [ #  # ]:          0 :         if (mmap_offset >= -region->size) {
    1303                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
    1304                 :            :                         "mmap_offset (%#"PRIx64") and memory_size (%#"PRIx64") overflow",
    1305                 :            :                         mmap_offset, region->size);
    1306                 :          0 :                 return -1;
    1307                 :            :         }
    1308                 :            : 
    1309                 :          0 :         mmap_size = region->size + mmap_offset;
    1310                 :            : 
    1311                 :            :         /* mmap() without flag of MAP_ANONYMOUS, should be called with length
    1312                 :            :          * argument aligned with hugepagesz at older longterm version Linux,
    1313                 :            :          * like 2.6.32 and 3.2.72, or mmap() will fail with EINVAL.
    1314                 :            :          *
    1315                 :            :          * To avoid failure, make sure in caller to keep length aligned.
    1316                 :            :          */
    1317                 :          0 :         alignment = get_blk_size(region->fd);
    1318         [ #  # ]:          0 :         if (alignment == (uint64_t)-1) {
    1319                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "couldn't get hugepage size through fstat");
    1320                 :          0 :                 return -1;
    1321                 :            :         }
    1322                 :          0 :         mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
    1323         [ #  # ]:          0 :         if (mmap_size == 0) {
    1324                 :            :                 /*
    1325                 :            :                  * It could happen if initial mmap_size + alignment overflows
    1326                 :            :                  * the sizeof uint64, which could happen if either mmap_size or
    1327                 :            :                  * alignment value is wrong.
    1328                 :            :                  *
    1329                 :            :                  * mmap() kernel implementation would return an error, but
    1330                 :            :                  * better catch it before and provide useful info in the logs.
    1331                 :            :                  */
    1332                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
    1333                 :            :                         "mmap size (0x%" PRIx64 ") or alignment (0x%" PRIx64 ") is invalid",
    1334                 :            :                         region->size + mmap_offset, alignment);
    1335                 :          0 :                 return -1;
    1336                 :            :         }
    1337                 :            : 
    1338         [ #  # ]:          0 :         populate = dev->async_copy ? MAP_POPULATE : 0;
    1339                 :          0 :         mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
    1340                 :            :                         MAP_SHARED | populate, region->fd, 0);
    1341                 :            : 
    1342         [ #  # ]:          0 :         if (mmap_addr == MAP_FAILED) {
    1343                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "mmap failed (%s).", strerror(errno));
    1344                 :          0 :                 return -1;
    1345                 :            :         }
    1346                 :            : 
    1347                 :          0 :         region->mmap_addr = mmap_addr;
    1348                 :          0 :         region->mmap_size = mmap_size;
    1349                 :          0 :         region->host_user_addr = (uint64_t)(uintptr_t)mmap_addr + mmap_offset;
    1350                 :          0 :         mem_set_dump(dev, mmap_addr, mmap_size, false, alignment);
    1351                 :            : 
    1352         [ #  # ]:          0 :         if (dev->async_copy) {
    1353         [ #  # ]:          0 :                 if (add_guest_pages(dev, region, alignment) < 0) {
    1354                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
    1355                 :            :                                 "adding guest pages to region failed.");
    1356                 :          0 :                         return -1;
    1357                 :            :                 }
    1358                 :            :         }
    1359                 :            : 
    1360                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    1361                 :            :                 "guest memory region size: 0x%" PRIx64,
    1362                 :            :                 region->size);
    1363                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    1364                 :            :                 "\t guest physical addr: 0x%" PRIx64,
    1365                 :            :                 region->guest_phys_addr);
    1366                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    1367                 :            :                 "\t guest virtual  addr: 0x%" PRIx64,
    1368                 :            :                 region->guest_user_addr);
    1369                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    1370                 :            :                 "\t host  virtual  addr: 0x%" PRIx64,
    1371                 :            :                 region->host_user_addr);
    1372                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    1373                 :            :                 "\t mmap addr : 0x%" PRIx64,
    1374                 :            :                 (uint64_t)(uintptr_t)mmap_addr);
    1375                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    1376                 :            :                 "\t mmap size : 0x%" PRIx64,
    1377                 :            :                 mmap_size);
    1378                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    1379                 :            :                 "\t mmap align: 0x%" PRIx64,
    1380                 :            :                 alignment);
    1381                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    1382                 :            :                 "\t mmap off  : 0x%" PRIx64,
    1383                 :            :                 mmap_offset);
    1384                 :            : 
    1385                 :          0 :         return 0;
    1386                 :            : }
    1387                 :            : 
    1388                 :            : static int
    1389                 :          0 : vhost_user_set_mem_table(struct virtio_net **pdev,
    1390                 :            :                         struct vhu_msg_context *ctx,
    1391                 :            :                         int main_fd)
    1392                 :            : {
    1393                 :          0 :         struct virtio_net *dev = *pdev;
    1394                 :          0 :         struct VhostUserMemory *memory = &ctx->msg.payload.memory;
    1395                 :            :         struct rte_vhost_mem_region *reg;
    1396                 :            :         int numa_node = SOCKET_ID_ANY;
    1397                 :            :         uint64_t mmap_offset;
    1398                 :            :         uint32_t i;
    1399                 :            :         bool async_notify = false;
    1400                 :            : 
    1401         [ #  # ]:          0 :         if (validate_msg_fds(dev, ctx, memory->nregions) != 0)
    1402                 :            :                 return RTE_VHOST_MSG_RESULT_ERR;
    1403                 :            : 
    1404         [ #  # ]:          0 :         if (memory->nregions > VHOST_MEMORY_MAX_NREGIONS) {
    1405                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
    1406                 :            :                         "too many memory regions (%u)",
    1407                 :            :                         memory->nregions);
    1408                 :          0 :                 goto close_msg_fds;
    1409                 :            :         }
    1410                 :            : 
    1411   [ #  #  #  # ]:          0 :         if (dev->mem && !vhost_memory_changed(memory, dev->mem)) {
    1412                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, INFO, "memory regions not changed");
    1413                 :            : 
    1414                 :          0 :                 close_msg_fds(ctx);
    1415                 :            : 
    1416                 :          0 :                 return RTE_VHOST_MSG_RESULT_OK;
    1417                 :            :         }
    1418                 :            : 
    1419         [ #  # ]:          0 :         if (dev->mem) {
    1420         [ #  # ]:          0 :                 if (dev->flags & VIRTIO_DEV_VDPA_CONFIGURED) {
    1421                 :          0 :                         struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
    1422                 :            : 
    1423   [ #  #  #  # ]:          0 :                         if (vdpa_dev && vdpa_dev->ops->dev_close)
    1424                 :          0 :                                 vdpa_dev->ops->dev_close(dev->vid);
    1425                 :          0 :                         dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
    1426                 :            :                 }
    1427                 :            : 
    1428                 :            :                 /* notify the vhost application to stop DMA transfers */
    1429   [ #  #  #  # ]:          0 :                 if (dev->async_copy && dev->notify_ops->vring_state_changed) {
    1430         [ #  # ]:          0 :                         for (i = 0; i < dev->nr_vring; i++) {
    1431                 :          0 :                                 dev->notify_ops->vring_state_changed(dev->vid,
    1432                 :            :                                                 i, 0);
    1433                 :            :                         }
    1434                 :            :                         async_notify = true;
    1435                 :            :                 }
    1436                 :            : 
    1437                 :            :                 /* Flush IOTLB cache as previous HVAs are now invalid */
    1438         [ #  # ]:          0 :                 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
    1439                 :          0 :                         vhost_user_iotlb_flush_all(dev);
    1440                 :            : 
    1441                 :          0 :                 free_mem_region(dev);
    1442                 :          0 :                 rte_free(dev->mem);
    1443                 :          0 :                 dev->mem = NULL;
    1444                 :            :         }
    1445                 :            : 
    1446                 :            :         /*
    1447                 :            :          * If VQ 0 has already been allocated, try to allocate on the same
    1448                 :            :          * NUMA node. It can be reallocated later in numa_realloc().
    1449                 :            :          */
    1450         [ #  # ]:          0 :         if (dev->nr_vring > 0)
    1451                 :          0 :                 numa_node = dev->virtqueue[0]->numa_node;
    1452                 :            : 
    1453                 :          0 :         dev->nr_guest_pages = 0;
    1454         [ #  # ]:          0 :         if (dev->guest_pages == NULL) {
    1455                 :          0 :                 dev->max_guest_pages = 8;
    1456                 :          0 :                 dev->guest_pages = rte_zmalloc_socket(NULL,
    1457                 :            :                                         dev->max_guest_pages *
    1458                 :            :                                         sizeof(struct guest_page),
    1459                 :            :                                         RTE_CACHE_LINE_SIZE,
    1460                 :            :                                         numa_node);
    1461         [ #  # ]:          0 :                 if (dev->guest_pages == NULL) {
    1462                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
    1463                 :            :                                 "failed to allocate memory for dev->guest_pages");
    1464                 :          0 :                         goto close_msg_fds;
    1465                 :            :                 }
    1466                 :            :         }
    1467                 :            : 
    1468                 :          0 :         dev->mem = rte_zmalloc_socket("vhost-mem-table", sizeof(struct rte_vhost_memory) +
    1469                 :          0 :                 sizeof(struct rte_vhost_mem_region) * memory->nregions, 0, numa_node);
    1470         [ #  # ]:          0 :         if (dev->mem == NULL) {
    1471                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to allocate memory for dev->mem");
    1472                 :          0 :                 goto free_guest_pages;
    1473                 :            :         }
    1474                 :            : 
    1475         [ #  # ]:          0 :         for (i = 0; i < memory->nregions; i++) {
    1476                 :          0 :                 reg = &dev->mem->regions[i];
    1477                 :            : 
    1478                 :          0 :                 reg->guest_phys_addr = memory->regions[i].guest_phys_addr;
    1479                 :          0 :                 reg->guest_user_addr = memory->regions[i].userspace_addr;
    1480                 :          0 :                 reg->size            = memory->regions[i].memory_size;
    1481                 :          0 :                 reg->fd              = ctx->fds[i];
    1482                 :            : 
    1483                 :            :                 /*
    1484                 :            :                  * Assign invalid file descriptor value to avoid double
    1485                 :            :                  * closing on error path.
    1486                 :            :                  */
    1487                 :          0 :                 ctx->fds[i] = -1;
    1488                 :            : 
    1489                 :          0 :                 mmap_offset = memory->regions[i].mmap_offset;
    1490                 :            : 
    1491         [ #  # ]:          0 :                 if (vhost_user_mmap_region(dev, reg, mmap_offset) < 0) {
    1492                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to mmap region %u", i);
    1493                 :          0 :                         goto free_mem_table;
    1494                 :            :                 }
    1495                 :            : 
    1496                 :          0 :                 dev->mem->nregions++;
    1497                 :            :         }
    1498                 :            : 
    1499   [ #  #  #  # ]:          0 :         if (dev->async_copy && rte_vfio_is_enabled("vfio"))
    1500                 :          0 :                 async_dma_map(dev, true);
    1501                 :            : 
    1502         [ #  # ]:          0 :         if (vhost_user_postcopy_register(dev, main_fd, ctx) < 0)
    1503                 :          0 :                 goto free_mem_table;
    1504                 :            : 
    1505         [ #  # ]:          0 :         for (i = 0; i < dev->nr_vring; i++) {
    1506                 :          0 :                 struct vhost_virtqueue *vq = dev->virtqueue[i];
    1507                 :            : 
    1508         [ #  # ]:          0 :                 if (!vq)
    1509                 :          0 :                         continue;
    1510                 :            : 
    1511   [ #  #  #  #  :          0 :                 if (vq->desc || vq->avail || vq->used) {
                   #  # ]
    1512                 :            :                         /* vhost_user_lock_all_queue_pairs locked all qps */
    1513                 :          0 :                         VHOST_USER_ASSERT_LOCK(dev, vq, VHOST_USER_SET_MEM_TABLE);
    1514                 :            : 
    1515                 :            :                         /*
    1516                 :            :                          * If the memory table got updated, the ring addresses
    1517                 :            :                          * need to be translated again as virtual addresses have
    1518                 :            :                          * changed.
    1519                 :            :                          */
    1520                 :          0 :                         vring_invalidate(dev, vq);
    1521                 :            : 
    1522                 :          0 :                         translate_ring_addresses(&dev, &vq);
    1523                 :          0 :                         *pdev = dev;
    1524                 :            :                 }
    1525                 :            :         }
    1526                 :            : 
    1527                 :            :         dump_guest_pages(dev);
    1528                 :            : 
    1529         [ #  # ]:          0 :         if (async_notify) {
    1530         [ #  # ]:          0 :                 for (i = 0; i < dev->nr_vring; i++)
    1531                 :          0 :                         dev->notify_ops->vring_state_changed(dev->vid, i, 1);
    1532                 :            :         }
    1533                 :            : 
    1534                 :            :         return RTE_VHOST_MSG_RESULT_OK;
    1535                 :            : 
    1536                 :          0 : free_mem_table:
    1537                 :          0 :         free_mem_region(dev);
    1538                 :          0 :         rte_free(dev->mem);
    1539                 :          0 :         dev->mem = NULL;
    1540                 :            : 
    1541                 :          0 : free_guest_pages:
    1542                 :          0 :         rte_free(dev->guest_pages);
    1543                 :          0 :         dev->guest_pages = NULL;
    1544                 :          0 : close_msg_fds:
    1545                 :          0 :         close_msg_fds(ctx);
    1546                 :          0 :         return RTE_VHOST_MSG_RESULT_ERR;
    1547                 :            : }
    1548                 :            : 
    1549                 :            : static bool
    1550                 :          0 : vq_is_ready(struct virtio_net *dev, struct vhost_virtqueue *vq)
    1551                 :            : {
    1552                 :            :         bool rings_ok;
    1553                 :            : 
    1554         [ #  # ]:          0 :         if (!vq)
    1555                 :            :                 return false;
    1556                 :            : 
    1557         [ #  # ]:          0 :         if (vq_is_packed(dev))
    1558   [ #  #  #  # ]:          0 :                 rings_ok = vq->desc_packed && vq->driver_event &&
    1559         [ #  # ]:          0 :                         vq->device_event;
    1560                 :            :         else
    1561   [ #  #  #  #  :          0 :                 rings_ok = vq->desc && vq->avail && vq->used;
                   #  # ]
    1562                 :            : 
    1563                 :          0 :         return rings_ok &&
    1564         [ #  # ]:          0 :                vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD &&
    1565   [ #  #  #  # ]:          0 :                vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD &&
    1566         [ #  # ]:          0 :                vq->enabled;
    1567                 :            : }
    1568                 :            : 
    1569                 :            : #define VIRTIO_BUILTIN_NUM_VQS_TO_BE_READY 2u
    1570                 :            : #define VIRTIO_BLK_NUM_VQS_TO_BE_READY 1u
    1571                 :            : 
    1572                 :            : static int
    1573                 :          0 : virtio_is_ready(struct virtio_net *dev)
    1574                 :            : {
    1575                 :            :         struct rte_vdpa_device *vdpa_dev;
    1576                 :            :         struct vhost_virtqueue *vq;
    1577                 :            :         uint32_t vdpa_type;
    1578                 :          0 :         uint32_t i, nr_vring = dev->nr_vring;
    1579                 :            : 
    1580         [ #  # ]:          0 :         if (dev->flags & VIRTIO_DEV_READY)
    1581                 :            :                 return 1;
    1582                 :            : 
    1583         [ #  # ]:          0 :         if (!dev->nr_vring)
    1584                 :            :                 return 0;
    1585                 :            : 
    1586                 :          0 :         vdpa_dev = dev->vdpa_dev;
    1587         [ #  # ]:          0 :         if (vdpa_dev)
    1588                 :          0 :                 vdpa_type = vdpa_dev->type;
    1589                 :            :         else
    1590                 :            :                 vdpa_type = -1;
    1591                 :            : 
    1592         [ #  # ]:          0 :         if (vdpa_type == RTE_VHOST_VDPA_DEVICE_TYPE_BLK) {
    1593                 :            :                 nr_vring = VIRTIO_BLK_NUM_VQS_TO_BE_READY;
    1594                 :            :         } else {
    1595         [ #  # ]:          0 :                 if (dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET)
    1596                 :            :                         nr_vring = VIRTIO_BUILTIN_NUM_VQS_TO_BE_READY;
    1597                 :            :         }
    1598                 :            : 
    1599         [ #  # ]:          0 :         if (dev->nr_vring < nr_vring)
    1600                 :            :                 return 0;
    1601                 :            : 
    1602         [ #  # ]:          0 :         for (i = 0; i < nr_vring; i++) {
    1603                 :          0 :                 vq = dev->virtqueue[i];
    1604                 :            : 
    1605         [ #  # ]:          0 :                 if (!vq_is_ready(dev, vq))
    1606                 :            :                         return 0;
    1607                 :            :         }
    1608                 :            : 
    1609                 :            :         /* If supported, ensure the frontend is really done with config */
    1610         [ #  # ]:          0 :         if (dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_STATUS))
    1611         [ #  # ]:          0 :                 if (!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK))
    1612                 :            :                         return 0;
    1613                 :            : 
    1614                 :          0 :         dev->flags |= VIRTIO_DEV_READY;
    1615                 :            : 
    1616         [ #  # ]:          0 :         if (!(dev->flags & VIRTIO_DEV_RUNNING))
    1617                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, INFO, "virtio is now ready for processing.");
    1618                 :            :         return 1;
    1619                 :            : }
    1620                 :            : 
    1621                 :            : static void *
    1622                 :          0 : inflight_mem_alloc(struct virtio_net *dev, const char *name, size_t size, int *fd)
    1623                 :            : {
    1624                 :            :         void *ptr;
    1625                 :            :         int mfd = -1;
    1626                 :            :         uint64_t alignment;
    1627                 :          0 :         char fname[20] = "/tmp/memfd-XXXXXX";
    1628                 :            : 
    1629                 :          0 :         *fd = -1;
    1630                 :            : #ifdef MEMFD_SUPPORTED
    1631                 :          0 :         mfd = memfd_create(name, MFD_CLOEXEC);
    1632                 :            : #else
    1633                 :            :         RTE_SET_USED(name);
    1634                 :            : #endif
    1635         [ #  # ]:          0 :         if (mfd == -1) {
    1636                 :          0 :                 mfd = mkstemp(fname);
    1637         [ #  # ]:          0 :                 if (mfd == -1) {
    1638                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to get inflight buffer fd");
    1639                 :          0 :                         return NULL;
    1640                 :            :                 }
    1641                 :            : 
    1642                 :          0 :                 unlink(fname);
    1643                 :            :         }
    1644                 :            : 
    1645         [ #  # ]:          0 :         if (ftruncate(mfd, size) == -1) {
    1646                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc inflight buffer");
    1647                 :          0 :                 close(mfd);
    1648                 :          0 :                 return NULL;
    1649                 :            :         }
    1650                 :            : 
    1651                 :          0 :         ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
    1652         [ #  # ]:          0 :         if (ptr == MAP_FAILED) {
    1653                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to mmap inflight buffer");
    1654                 :          0 :                 close(mfd);
    1655                 :          0 :                 return NULL;
    1656                 :            :         }
    1657                 :            : 
    1658                 :            :         alignment = get_blk_size(mfd);
    1659                 :          0 :         mem_set_dump(dev, ptr, size, false, alignment);
    1660                 :          0 :         *fd = mfd;
    1661                 :          0 :         return ptr;
    1662                 :            : }
    1663                 :            : 
    1664                 :            : static uint32_t
    1665                 :            : get_pervq_shm_size_split(uint16_t queue_size)
    1666                 :            : {
    1667                 :          0 :         return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_split) *
    1668                 :            :                                   queue_size + sizeof(uint64_t) +
    1669                 :            :                                   sizeof(uint16_t) * 4, INFLIGHT_ALIGNMENT);
    1670                 :            : }
    1671                 :            : 
    1672                 :            : static uint32_t
    1673                 :            : get_pervq_shm_size_packed(uint16_t queue_size)
    1674                 :            : {
    1675                 :          0 :         return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_packed)
    1676                 :            :                                   * queue_size + sizeof(uint64_t) +
    1677                 :            :                                   sizeof(uint16_t) * 6 + sizeof(uint8_t) * 9,
    1678                 :            :                                   INFLIGHT_ALIGNMENT);
    1679                 :            : }
    1680                 :            : 
    1681                 :            : static int
    1682                 :          0 : vhost_user_get_inflight_fd(struct virtio_net **pdev,
    1683                 :            :                            struct vhu_msg_context *ctx,
    1684                 :            :                            int main_fd __rte_unused)
    1685                 :            : {
    1686                 :            :         struct rte_vhost_inflight_info_packed *inflight_packed;
    1687                 :            :         uint64_t pervq_inflight_size, mmap_size;
    1688                 :            :         uint16_t num_queues, queue_size;
    1689                 :          0 :         struct virtio_net *dev = *pdev;
    1690                 :            :         int fd, i, j;
    1691                 :            :         int numa_node = SOCKET_ID_ANY;
    1692                 :            :         void *addr;
    1693                 :            : 
    1694         [ #  # ]:          0 :         if (ctx->msg.size != sizeof(ctx->msg.payload.inflight)) {
    1695                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
    1696                 :            :                         "invalid get_inflight_fd message size is %d",
    1697                 :            :                         ctx->msg.size);
    1698                 :          0 :                 return RTE_VHOST_MSG_RESULT_ERR;
    1699                 :            :         }
    1700                 :            : 
    1701                 :            :         /*
    1702                 :            :          * If VQ 0 has already been allocated, try to allocate on the same
    1703                 :            :          * NUMA node. It can be reallocated later in numa_realloc().
    1704                 :            :          */
    1705         [ #  # ]:          0 :         if (dev->nr_vring > 0)
    1706                 :          0 :                 numa_node = dev->virtqueue[0]->numa_node;
    1707                 :            : 
    1708         [ #  # ]:          0 :         if (dev->inflight_info == NULL) {
    1709                 :          0 :                 dev->inflight_info = rte_zmalloc_socket("inflight_info",
    1710                 :            :                                 sizeof(struct inflight_mem_info), 0, numa_node);
    1711         [ #  # ]:          0 :                 if (!dev->inflight_info) {
    1712                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc dev inflight area");
    1713                 :          0 :                         return RTE_VHOST_MSG_RESULT_ERR;
    1714                 :            :                 }
    1715                 :          0 :                 dev->inflight_info->fd = -1;
    1716                 :            :         }
    1717                 :            : 
    1718                 :          0 :         num_queues = ctx->msg.payload.inflight.num_queues;
    1719                 :          0 :         queue_size = ctx->msg.payload.inflight.queue_size;
    1720                 :            : 
    1721                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    1722                 :            :                 "get_inflight_fd num_queues: %u",
    1723                 :            :                 ctx->msg.payload.inflight.num_queues);
    1724                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    1725                 :            :                 "get_inflight_fd queue_size: %u",
    1726                 :            :                 ctx->msg.payload.inflight.queue_size);
    1727                 :            : 
    1728         [ #  # ]:          0 :         if (vq_is_packed(dev))
    1729                 :          0 :                 pervq_inflight_size = get_pervq_shm_size_packed(queue_size);
    1730                 :            :         else
    1731                 :          0 :                 pervq_inflight_size = get_pervq_shm_size_split(queue_size);
    1732                 :            : 
    1733                 :          0 :         mmap_size = num_queues * pervq_inflight_size;
    1734                 :          0 :         addr = inflight_mem_alloc(dev, "vhost-inflight", mmap_size, &fd);
    1735         [ #  # ]:          0 :         if (!addr) {
    1736                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc vhost inflight area");
    1737                 :          0 :                         ctx->msg.payload.inflight.mmap_size = 0;
    1738                 :          0 :                 return RTE_VHOST_MSG_RESULT_ERR;
    1739                 :            :         }
    1740                 :            :         memset(addr, 0, mmap_size);
    1741                 :            : 
    1742         [ #  # ]:          0 :         if (dev->inflight_info->addr) {
    1743                 :          0 :                 munmap(dev->inflight_info->addr, dev->inflight_info->size);
    1744                 :          0 :                 dev->inflight_info->addr = NULL;
    1745                 :            :         }
    1746                 :            : 
    1747         [ #  # ]:          0 :         if (dev->inflight_info->fd >= 0) {
    1748                 :          0 :                 close(dev->inflight_info->fd);
    1749                 :          0 :                 dev->inflight_info->fd = -1;
    1750                 :            :         }
    1751                 :            : 
    1752                 :          0 :         dev->inflight_info->addr = addr;
    1753                 :          0 :         dev->inflight_info->size = ctx->msg.payload.inflight.mmap_size = mmap_size;
    1754                 :          0 :         dev->inflight_info->fd = ctx->fds[0] = fd;
    1755                 :          0 :         ctx->msg.payload.inflight.mmap_offset = 0;
    1756         [ #  # ]:          0 :         ctx->fd_num = 1;
    1757                 :            : 
    1758         [ #  # ]:          0 :         if (vq_is_packed(dev)) {
    1759         [ #  # ]:          0 :                 for (i = 0; i < num_queues; i++) {
    1760                 :            :                         inflight_packed =
    1761                 :            :                                 (struct rte_vhost_inflight_info_packed *)addr;
    1762                 :          0 :                         inflight_packed->used_wrap_counter = 1;
    1763                 :          0 :                         inflight_packed->old_used_wrap_counter = 1;
    1764         [ #  # ]:          0 :                         for (j = 0; j < queue_size; j++)
    1765                 :          0 :                                 inflight_packed->desc[j].next = j + 1;
    1766                 :          0 :                         addr = (void *)((char *)addr + pervq_inflight_size);
    1767                 :            :                 }
    1768                 :            :         }
    1769                 :            : 
    1770                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    1771                 :            :                 "send inflight mmap_size: %"PRIu64,
    1772                 :            :                 ctx->msg.payload.inflight.mmap_size);
    1773                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    1774                 :            :                 "send inflight mmap_offset: %"PRIu64,
    1775                 :            :                 ctx->msg.payload.inflight.mmap_offset);
    1776                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    1777                 :            :                 "send inflight fd: %d", ctx->fds[0]);
    1778                 :            : 
    1779                 :          0 :         return RTE_VHOST_MSG_RESULT_REPLY;
    1780                 :            : }
    1781                 :            : 
    1782                 :            : static int
    1783                 :          0 : vhost_user_set_inflight_fd(struct virtio_net **pdev,
    1784                 :            :                            struct vhu_msg_context *ctx,
    1785                 :            :                            int main_fd __rte_unused)
    1786                 :            : {
    1787                 :            :         uint64_t mmap_size, mmap_offset;
    1788                 :            :         uint16_t num_queues, queue_size;
    1789                 :          0 :         struct virtio_net *dev = *pdev;
    1790                 :            :         uint32_t pervq_inflight_size;
    1791                 :            :         struct vhost_virtqueue *vq;
    1792                 :            :         void *addr;
    1793                 :            :         int fd, i;
    1794                 :            :         int numa_node = SOCKET_ID_ANY;
    1795                 :            : 
    1796         [ #  # ]:          0 :         if (validate_msg_fds(dev, ctx, 1) != 0)
    1797                 :            :                 return RTE_VHOST_MSG_RESULT_ERR;
    1798                 :            : 
    1799                 :          0 :         fd = ctx->fds[0];
    1800   [ #  #  #  # ]:          0 :         if (ctx->msg.size != sizeof(ctx->msg.payload.inflight) || fd < 0) {
    1801                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
    1802                 :            :                         "invalid set_inflight_fd message size is %d,fd is %d",
    1803                 :            :                         ctx->msg.size, fd);
    1804                 :          0 :                 return RTE_VHOST_MSG_RESULT_ERR;
    1805                 :            :         }
    1806                 :            : 
    1807                 :          0 :         mmap_size = ctx->msg.payload.inflight.mmap_size;
    1808                 :          0 :         mmap_offset = ctx->msg.payload.inflight.mmap_offset;
    1809                 :          0 :         num_queues = ctx->msg.payload.inflight.num_queues;
    1810         [ #  # ]:          0 :         queue_size = ctx->msg.payload.inflight.queue_size;
    1811                 :            : 
    1812         [ #  # ]:          0 :         if (vq_is_packed(dev))
    1813                 :            :                 pervq_inflight_size = get_pervq_shm_size_packed(queue_size);
    1814                 :            :         else
    1815                 :            :                 pervq_inflight_size = get_pervq_shm_size_split(queue_size);
    1816                 :            : 
    1817                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO, "set_inflight_fd mmap_size: %"PRIu64, mmap_size);
    1818                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    1819                 :            :                 "set_inflight_fd mmap_offset: %"PRIu64,
    1820                 :            :                 mmap_offset);
    1821                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    1822                 :            :                 "set_inflight_fd num_queues: %u",
    1823                 :            :                 num_queues);
    1824                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    1825                 :            :                 "set_inflight_fd queue_size: %u",
    1826                 :            :                 queue_size);
    1827                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    1828                 :            :                 "set_inflight_fd fd: %d",
    1829                 :            :                 fd);
    1830                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    1831                 :            :                 "set_inflight_fd pervq_inflight_size: %d",
    1832                 :            :                 pervq_inflight_size);
    1833                 :            : 
    1834                 :            :         /*
    1835                 :            :          * If VQ 0 has already been allocated, try to allocate on the same
    1836                 :            :          * NUMA node. It can be reallocated later in numa_realloc().
    1837                 :            :          */
    1838         [ #  # ]:          0 :         if (dev->nr_vring > 0)
    1839                 :          0 :                 numa_node = dev->virtqueue[0]->numa_node;
    1840                 :            : 
    1841         [ #  # ]:          0 :         if (!dev->inflight_info) {
    1842                 :          0 :                 dev->inflight_info = rte_zmalloc_socket("inflight_info",
    1843                 :            :                                 sizeof(struct inflight_mem_info), 0, numa_node);
    1844         [ #  # ]:          0 :                 if (dev->inflight_info == NULL) {
    1845                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc dev inflight area");
    1846                 :          0 :                         return RTE_VHOST_MSG_RESULT_ERR;
    1847                 :            :                 }
    1848                 :          0 :                 dev->inflight_info->fd = -1;
    1849                 :            :         }
    1850                 :            : 
    1851         [ #  # ]:          0 :         if (dev->inflight_info->addr) {
    1852                 :          0 :                 munmap(dev->inflight_info->addr, dev->inflight_info->size);
    1853                 :          0 :                 dev->inflight_info->addr = NULL;
    1854                 :            :         }
    1855                 :            : 
    1856                 :          0 :         addr = mmap(0, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
    1857                 :            :                     fd, mmap_offset);
    1858         [ #  # ]:          0 :         if (addr == MAP_FAILED) {
    1859                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to mmap share memory.");
    1860                 :          0 :                 return RTE_VHOST_MSG_RESULT_ERR;
    1861                 :            :         }
    1862                 :            : 
    1863         [ #  # ]:          0 :         if (dev->inflight_info->fd >= 0) {
    1864                 :          0 :                 close(dev->inflight_info->fd);
    1865                 :          0 :                 dev->inflight_info->fd = -1;
    1866                 :            :         }
    1867                 :            : 
    1868                 :          0 :         mem_set_dump(dev, addr, mmap_size, false, get_blk_size(fd));
    1869                 :          0 :         dev->inflight_info->fd = fd;
    1870                 :          0 :         dev->inflight_info->addr = addr;
    1871                 :          0 :         dev->inflight_info->size = mmap_size;
    1872                 :            : 
    1873         [ #  # ]:          0 :         for (i = 0; i < num_queues; i++) {
    1874                 :          0 :                 vq = dev->virtqueue[i];
    1875         [ #  # ]:          0 :                 if (!vq)
    1876                 :          0 :                         continue;
    1877                 :            : 
    1878                 :          0 :                 cleanup_vq_inflight(dev, vq);
    1879         [ #  # ]:          0 :                 if (vq_is_packed(dev)) {
    1880                 :          0 :                         vq->inflight_packed = addr;
    1881                 :          0 :                         vq->inflight_packed->desc_num = queue_size;
    1882                 :            :                 } else {
    1883                 :          0 :                         vq->inflight_split = addr;
    1884                 :          0 :                         vq->inflight_split->desc_num = queue_size;
    1885                 :            :                 }
    1886                 :          0 :                 addr = (void *)((char *)addr + pervq_inflight_size);
    1887                 :            :         }
    1888                 :            : 
    1889                 :            :         return RTE_VHOST_MSG_RESULT_OK;
    1890                 :            : }
    1891                 :            : 
    1892                 :            : static int
    1893                 :          0 : vhost_user_set_vring_call(struct virtio_net **pdev,
    1894                 :            :                         struct vhu_msg_context *ctx,
    1895                 :            :                         int main_fd __rte_unused)
    1896                 :            : {
    1897                 :          0 :         struct virtio_net *dev = *pdev;
    1898                 :            :         struct vhost_vring_file file;
    1899                 :            :         struct vhost_virtqueue *vq;
    1900                 :            :         int expected_fds;
    1901                 :            : 
    1902                 :          0 :         expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
    1903         [ #  # ]:          0 :         if (validate_msg_fds(dev, ctx, expected_fds) != 0)
    1904                 :            :                 return RTE_VHOST_MSG_RESULT_ERR;
    1905                 :            : 
    1906                 :          0 :         file.index = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
    1907         [ #  # ]:          0 :         if (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK)
    1908                 :            :                 file.fd = VIRTIO_INVALID_EVENTFD;
    1909                 :            :         else
    1910                 :          0 :                 file.fd = ctx->fds[0];
    1911                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    1912                 :            :                 "vring call idx:%d file:%d",
    1913                 :            :                 file.index, file.fd);
    1914                 :            : 
    1915                 :          0 :         vq = dev->virtqueue[file.index];
    1916                 :            : 
    1917         [ #  # ]:          0 :         if (vq->ready) {
    1918                 :          0 :                 vq->ready = false;
    1919                 :          0 :                 vhost_user_notify_queue_state(dev, vq, 0);
    1920                 :            :         }
    1921                 :            : 
    1922         [ #  # ]:          0 :         if (vq->callfd >= 0)
    1923                 :          0 :                 close(vq->callfd);
    1924                 :            : 
    1925                 :          0 :         vq->callfd = file.fd;
    1926                 :            : 
    1927                 :          0 :         return RTE_VHOST_MSG_RESULT_OK;
    1928                 :            : }
    1929                 :            : 
    1930                 :          0 : static int vhost_user_set_vring_err(struct virtio_net **pdev,
    1931                 :            :                         struct vhu_msg_context *ctx,
    1932                 :            :                         int main_fd __rte_unused)
    1933                 :            : {
    1934                 :          0 :         struct virtio_net *dev = *pdev;
    1935                 :            :         int expected_fds;
    1936                 :            : 
    1937                 :          0 :         expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
    1938         [ #  # ]:          0 :         if (validate_msg_fds(dev, ctx, expected_fds) != 0)
    1939                 :            :                 return RTE_VHOST_MSG_RESULT_ERR;
    1940                 :            : 
    1941         [ #  # ]:          0 :         if (!(ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
    1942                 :          0 :                 close(ctx->fds[0]);
    1943                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, DEBUG, "not implemented");
    1944                 :            : 
    1945                 :          0 :         return RTE_VHOST_MSG_RESULT_OK;
    1946                 :            : }
    1947                 :            : 
    1948                 :            : static int
    1949                 :          0 : resubmit_desc_compare(const void *a, const void *b)
    1950                 :            : {
    1951                 :            :         const struct rte_vhost_resubmit_desc *desc0 = a;
    1952                 :            :         const struct rte_vhost_resubmit_desc *desc1 = b;
    1953                 :            : 
    1954         [ #  # ]:          0 :         if (desc1->counter > desc0->counter)
    1955                 :          0 :                 return 1;
    1956                 :            : 
    1957                 :            :         return -1;
    1958                 :            : }
    1959                 :            : 
    1960                 :            : static int
    1961                 :          0 : vhost_check_queue_inflights_split(struct virtio_net *dev,
    1962                 :            :                                   struct vhost_virtqueue *vq)
    1963                 :            : {
    1964                 :            :         uint16_t i;
    1965                 :            :         uint16_t resubmit_num = 0, last_io, num;
    1966                 :          0 :         struct vring_used *used = vq->used;
    1967                 :            :         struct rte_vhost_resubmit_info *resubmit;
    1968                 :            :         struct rte_vhost_inflight_info_split *inflight_split;
    1969                 :            : 
    1970         [ #  # ]:          0 :         if (!(dev->protocol_features &
    1971                 :            :             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
    1972                 :            :                 return RTE_VHOST_MSG_RESULT_OK;
    1973                 :            : 
    1974                 :            :         /* The frontend may still not support the inflight feature
    1975                 :            :          * although we negotiate the protocol feature.
    1976                 :            :          */
    1977         [ #  # ]:          0 :         if ((!vq->inflight_split))
    1978                 :            :                 return RTE_VHOST_MSG_RESULT_OK;
    1979                 :            : 
    1980         [ #  # ]:          0 :         if (!vq->inflight_split->version) {
    1981                 :          0 :                 vq->inflight_split->version = INFLIGHT_VERSION;
    1982                 :          0 :                 return RTE_VHOST_MSG_RESULT_OK;
    1983                 :            :         }
    1984                 :            : 
    1985         [ #  # ]:          0 :         if (vq->resubmit_inflight)
    1986                 :            :                 return RTE_VHOST_MSG_RESULT_OK;
    1987                 :            : 
    1988                 :            :         inflight_split = vq->inflight_split;
    1989                 :          0 :         vq->global_counter = 0;
    1990                 :          0 :         last_io = inflight_split->last_inflight_io;
    1991                 :            : 
    1992         [ #  # ]:          0 :         if (inflight_split->used_idx != used->idx) {
    1993                 :          0 :                 inflight_split->desc[last_io].inflight = 0;
    1994                 :            :                 rte_atomic_thread_fence(rte_memory_order_seq_cst);
    1995                 :          0 :                 inflight_split->used_idx = used->idx;
    1996                 :            :         }
    1997                 :            : 
    1998         [ #  # ]:          0 :         for (i = 0; i < inflight_split->desc_num; i++) {
    1999         [ #  # ]:          0 :                 if (inflight_split->desc[i].inflight == 1)
    2000                 :          0 :                         resubmit_num++;
    2001                 :            :         }
    2002                 :            : 
    2003         [ #  # ]:          0 :         vq->last_avail_idx += resubmit_num;
    2004                 :            :         vhost_virtqueue_reconnect_log_split(vq);
    2005                 :            : 
    2006         [ #  # ]:          0 :         if (resubmit_num) {
    2007                 :          0 :                 resubmit = rte_zmalloc_socket("resubmit", sizeof(struct rte_vhost_resubmit_info),
    2008                 :            :                                 0, vq->numa_node);
    2009         [ #  # ]:          0 :                 if (!resubmit) {
    2010                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
    2011                 :            :                                 "failed to allocate memory for resubmit info.");
    2012                 :          0 :                         return RTE_VHOST_MSG_RESULT_ERR;
    2013                 :            :                 }
    2014                 :            : 
    2015                 :          0 :                 resubmit->resubmit_list = rte_zmalloc_socket("resubmit_list",
    2016                 :            :                                 resubmit_num * sizeof(struct rte_vhost_resubmit_desc),
    2017                 :            :                                 0, vq->numa_node);
    2018         [ #  # ]:          0 :                 if (!resubmit->resubmit_list) {
    2019                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
    2020                 :            :                                         "failed to allocate memory for inflight desc.");
    2021                 :          0 :                         rte_free(resubmit);
    2022                 :          0 :                         return RTE_VHOST_MSG_RESULT_ERR;
    2023                 :            :                 }
    2024                 :            : 
    2025                 :            :                 num = 0;
    2026         [ #  # ]:          0 :                 for (i = 0; i < vq->inflight_split->desc_num; i++) {
    2027         [ #  # ]:          0 :                         if (vq->inflight_split->desc[i].inflight == 1) {
    2028                 :          0 :                                 resubmit->resubmit_list[num].index = i;
    2029                 :          0 :                                 resubmit->resubmit_list[num].counter =
    2030                 :          0 :                                         inflight_split->desc[i].counter;
    2031                 :          0 :                                 num++;
    2032                 :            :                         }
    2033                 :            :                 }
    2034                 :          0 :                 resubmit->resubmit_num = num;
    2035                 :            : 
    2036         [ #  # ]:          0 :                 if (resubmit->resubmit_num > 1)
    2037                 :          0 :                         qsort(resubmit->resubmit_list, resubmit->resubmit_num,
    2038                 :            :                               sizeof(struct rte_vhost_resubmit_desc),
    2039                 :            :                               resubmit_desc_compare);
    2040                 :            : 
    2041                 :          0 :                 vq->global_counter = resubmit->resubmit_list[0].counter + 1;
    2042                 :          0 :                 vq->resubmit_inflight = resubmit;
    2043                 :            :         }
    2044                 :            : 
    2045                 :            :         return RTE_VHOST_MSG_RESULT_OK;
    2046                 :            : }
    2047                 :            : 
    2048                 :            : static int
    2049                 :          0 : vhost_check_queue_inflights_packed(struct virtio_net *dev,
    2050                 :            :                                    struct vhost_virtqueue *vq)
    2051                 :            : {
    2052                 :            :         uint16_t i;
    2053                 :            :         uint16_t resubmit_num = 0, old_used_idx, num;
    2054                 :            :         struct rte_vhost_resubmit_info *resubmit;
    2055                 :            :         struct rte_vhost_inflight_info_packed *inflight_packed;
    2056                 :            : 
    2057         [ #  # ]:          0 :         if (!(dev->protocol_features &
    2058                 :            :             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
    2059                 :            :                 return RTE_VHOST_MSG_RESULT_OK;
    2060                 :            : 
    2061                 :            :         /* The frontend may still not support the inflight feature
    2062                 :            :          * although we negotiate the protocol feature.
    2063                 :            :          */
    2064         [ #  # ]:          0 :         if ((!vq->inflight_packed))
    2065                 :            :                 return RTE_VHOST_MSG_RESULT_OK;
    2066                 :            : 
    2067         [ #  # ]:          0 :         if (!vq->inflight_packed->version) {
    2068                 :          0 :                 vq->inflight_packed->version = INFLIGHT_VERSION;
    2069                 :          0 :                 return RTE_VHOST_MSG_RESULT_OK;
    2070                 :            :         }
    2071                 :            : 
    2072         [ #  # ]:          0 :         if (vq->resubmit_inflight)
    2073                 :            :                 return RTE_VHOST_MSG_RESULT_OK;
    2074                 :            : 
    2075                 :            :         inflight_packed = vq->inflight_packed;
    2076                 :          0 :         vq->global_counter = 0;
    2077                 :          0 :         old_used_idx = inflight_packed->old_used_idx;
    2078                 :            : 
    2079         [ #  # ]:          0 :         if (inflight_packed->used_idx != old_used_idx) {
    2080         [ #  # ]:          0 :                 if (inflight_packed->desc[old_used_idx].inflight == 0) {
    2081                 :          0 :                         inflight_packed->old_used_idx =
    2082                 :            :                                 inflight_packed->used_idx;
    2083                 :          0 :                         inflight_packed->old_used_wrap_counter =
    2084                 :          0 :                                 inflight_packed->used_wrap_counter;
    2085                 :          0 :                         inflight_packed->old_free_head =
    2086                 :          0 :                                 inflight_packed->free_head;
    2087                 :            :                 } else {
    2088                 :          0 :                         inflight_packed->used_idx =
    2089                 :            :                                 inflight_packed->old_used_idx;
    2090                 :          0 :                         inflight_packed->used_wrap_counter =
    2091                 :          0 :                                 inflight_packed->old_used_wrap_counter;
    2092                 :          0 :                         inflight_packed->free_head =
    2093                 :          0 :                                 inflight_packed->old_free_head;
    2094                 :            :                 }
    2095                 :            :         }
    2096                 :            : 
    2097         [ #  # ]:          0 :         for (i = 0; i < inflight_packed->desc_num; i++) {
    2098         [ #  # ]:          0 :                 if (inflight_packed->desc[i].inflight == 1)
    2099                 :          0 :                         resubmit_num++;
    2100                 :            :         }
    2101                 :            : 
    2102         [ #  # ]:          0 :         if (resubmit_num) {
    2103                 :          0 :                 resubmit = rte_zmalloc_socket("resubmit", sizeof(struct rte_vhost_resubmit_info),
    2104                 :            :                                 0, vq->numa_node);
    2105         [ #  # ]:          0 :                 if (resubmit == NULL) {
    2106                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
    2107                 :            :                                 "failed to allocate memory for resubmit info.");
    2108                 :          0 :                         return RTE_VHOST_MSG_RESULT_ERR;
    2109                 :            :                 }
    2110                 :            : 
    2111                 :          0 :                 resubmit->resubmit_list = rte_zmalloc_socket("resubmit_list",
    2112                 :            :                                 resubmit_num * sizeof(struct rte_vhost_resubmit_desc),
    2113                 :            :                                 0, vq->numa_node);
    2114         [ #  # ]:          0 :                 if (resubmit->resubmit_list == NULL) {
    2115                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
    2116                 :            :                                 "failed to allocate memory for resubmit desc.");
    2117                 :          0 :                         rte_free(resubmit);
    2118                 :          0 :                         return RTE_VHOST_MSG_RESULT_ERR;
    2119                 :            :                 }
    2120                 :            : 
    2121                 :            :                 num = 0;
    2122         [ #  # ]:          0 :                 for (i = 0; i < inflight_packed->desc_num; i++) {
    2123         [ #  # ]:          0 :                         if (vq->inflight_packed->desc[i].inflight == 1) {
    2124                 :          0 :                                 resubmit->resubmit_list[num].index = i;
    2125                 :          0 :                                 resubmit->resubmit_list[num].counter =
    2126                 :          0 :                                         inflight_packed->desc[i].counter;
    2127                 :          0 :                                 num++;
    2128                 :            :                         }
    2129                 :            :                 }
    2130                 :          0 :                 resubmit->resubmit_num = num;
    2131                 :            : 
    2132         [ #  # ]:          0 :                 if (resubmit->resubmit_num > 1)
    2133                 :          0 :                         qsort(resubmit->resubmit_list, resubmit->resubmit_num,
    2134                 :            :                               sizeof(struct rte_vhost_resubmit_desc),
    2135                 :            :                               resubmit_desc_compare);
    2136                 :            : 
    2137                 :          0 :                 vq->global_counter = resubmit->resubmit_list[0].counter + 1;
    2138                 :          0 :                 vq->resubmit_inflight = resubmit;
    2139                 :            :         }
    2140                 :            : 
    2141                 :            :         return RTE_VHOST_MSG_RESULT_OK;
    2142                 :            : }
    2143                 :            : 
    2144                 :            : static int
    2145                 :          0 : vhost_user_set_vring_kick(struct virtio_net **pdev,
    2146                 :            :                         struct vhu_msg_context *ctx,
    2147                 :            :                         int main_fd __rte_unused)
    2148                 :            : {
    2149                 :          0 :         struct virtio_net *dev = *pdev;
    2150                 :            :         struct vhost_vring_file file;
    2151                 :            :         struct vhost_virtqueue *vq;
    2152                 :            :         int expected_fds;
    2153                 :            : 
    2154                 :          0 :         expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
    2155         [ #  # ]:          0 :         if (validate_msg_fds(dev, ctx, expected_fds) != 0)
    2156                 :            :                 return RTE_VHOST_MSG_RESULT_ERR;
    2157                 :            : 
    2158                 :          0 :         file.index = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
    2159         [ #  # ]:          0 :         if (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK)
    2160                 :            :                 file.fd = VIRTIO_INVALID_EVENTFD;
    2161                 :            :         else
    2162                 :          0 :                 file.fd = ctx->fds[0];
    2163                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    2164                 :            :                 "vring kick idx:%d file:%d",
    2165                 :            :                 file.index, file.fd);
    2166                 :            : 
    2167                 :            :         /* Interpret ring addresses only when ring is started. */
    2168                 :          0 :         vq = dev->virtqueue[file.index];
    2169                 :          0 :         translate_ring_addresses(&dev, &vq);
    2170                 :          0 :         *pdev = dev;
    2171                 :            : 
    2172                 :            :         /*
    2173                 :            :          * When VHOST_USER_F_PROTOCOL_FEATURES is not negotiated,
    2174                 :            :          * the ring starts already enabled. Otherwise, it is enabled via
    2175                 :            :          * the SET_VRING_ENABLE message.
    2176                 :            :          */
    2177         [ #  # ]:          0 :         if (!(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) {
    2178                 :          0 :                 vq->enabled = true;
    2179                 :            :         }
    2180                 :            : 
    2181         [ #  # ]:          0 :         if (vq->ready) {
    2182                 :          0 :                 vq->ready = false;
    2183                 :          0 :                 vhost_user_notify_queue_state(dev, vq, 0);
    2184                 :            :         }
    2185                 :            : 
    2186         [ #  # ]:          0 :         if (vq->kickfd >= 0)
    2187                 :          0 :                 close(vq->kickfd);
    2188         [ #  # ]:          0 :         vq->kickfd = file.fd;
    2189                 :            : 
    2190         [ #  # ]:          0 :         if (vq_is_packed(dev)) {
    2191         [ #  # ]:          0 :                 if (vhost_check_queue_inflights_packed(dev, vq)) {
    2192                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
    2193                 :            :                                 "failed to inflights for vq: %d",
    2194                 :            :                                 file.index);
    2195                 :          0 :                         return RTE_VHOST_MSG_RESULT_ERR;
    2196                 :            :                 }
    2197                 :            :         } else {
    2198         [ #  # ]:          0 :                 if (vhost_check_queue_inflights_split(dev, vq)) {
    2199                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
    2200                 :            :                                 "failed to inflights for vq: %d",
    2201                 :            :                                 file.index);
    2202                 :          0 :                         return RTE_VHOST_MSG_RESULT_ERR;
    2203                 :            :                 }
    2204                 :            :         }
    2205                 :            : 
    2206                 :            :         return RTE_VHOST_MSG_RESULT_OK;
    2207                 :            : }
    2208                 :            : 
    2209                 :            : /*
    2210                 :            :  * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
    2211                 :            :  */
    2212                 :            : static int
    2213                 :          0 : vhost_user_get_vring_base(struct virtio_net **pdev,
    2214                 :            :                         struct vhu_msg_context *ctx,
    2215                 :            :                         int main_fd __rte_unused)
    2216                 :            : {
    2217                 :          0 :         struct virtio_net *dev = *pdev;
    2218                 :          0 :         struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index];
    2219                 :            :         uint64_t val;
    2220                 :            : 
    2221                 :            :         /* We have to stop the queue (virtio) if it is running. */
    2222                 :          0 :         vhost_destroy_device_notify(dev);
    2223                 :            : 
    2224                 :          0 :         dev->flags &= ~VIRTIO_DEV_READY;
    2225         [ #  # ]:          0 :         dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
    2226                 :            : 
    2227                 :            :         /* Here we are safe to get the indexes */
    2228         [ #  # ]:          0 :         if (vq_is_packed(dev)) {
    2229                 :            :                 /*
    2230                 :            :                  * Bit[0:14]: avail index
    2231                 :            :                  * Bit[15]: avail wrap counter
    2232                 :            :                  */
    2233                 :          0 :                 val = vq->last_avail_idx & 0x7fff;
    2234                 :          0 :                 val |= vq->avail_wrap_counter << 15;
    2235                 :          0 :                 ctx->msg.payload.state.num = val;
    2236                 :            :         } else {
    2237                 :          0 :                 ctx->msg.payload.state.num = vq->last_avail_idx;
    2238                 :            :         }
    2239                 :            : 
    2240                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    2241                 :            :                 "vring base idx:%d file:%d",
    2242                 :            :                 ctx->msg.payload.state.index, ctx->msg.payload.state.num);
    2243                 :            :         /*
    2244                 :            :          * Based on current qemu vhost-user implementation, this message is
    2245                 :            :          * sent and only sent in vhost_vring_stop.
    2246                 :            :          * TODO: cleanup the vring, it isn't usable since here.
    2247                 :            :          */
    2248         [ #  # ]:          0 :         if (vq->kickfd >= 0)
    2249                 :          0 :                 close(vq->kickfd);
    2250                 :            : 
    2251                 :          0 :         vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
    2252                 :            : 
    2253         [ #  # ]:          0 :         if (vq->callfd >= 0)
    2254                 :          0 :                 close(vq->callfd);
    2255                 :            : 
    2256                 :          0 :         vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
    2257                 :            : 
    2258         [ #  # ]:          0 :         vq->signalled_used_valid = false;
    2259                 :            : 
    2260         [ #  # ]:          0 :         if (vq_is_packed(dev)) {
    2261                 :          0 :                 rte_free(vq->shadow_used_packed);
    2262                 :          0 :                 vq->shadow_used_packed = NULL;
    2263                 :            :         } else {
    2264                 :          0 :                 rte_free(vq->shadow_used_split);
    2265                 :          0 :                 vq->shadow_used_split = NULL;
    2266                 :            :         }
    2267                 :            : 
    2268                 :          0 :         rte_free(vq->batch_copy_elems);
    2269                 :          0 :         vq->batch_copy_elems = NULL;
    2270                 :            : 
    2271                 :          0 :         rte_free(vq->log_cache);
    2272                 :          0 :         vq->log_cache = NULL;
    2273                 :            : 
    2274                 :          0 :         ctx->msg.size = sizeof(ctx->msg.payload.state);
    2275                 :          0 :         ctx->fd_num = 0;
    2276                 :            : 
    2277                 :          0 :         vhost_user_iotlb_flush_all(dev);
    2278                 :            : 
    2279                 :          0 :         rte_rwlock_write_lock(&vq->access_lock);
    2280                 :          0 :         vring_invalidate(dev, vq);
    2281                 :          0 :         memset(&vq->ring_addrs, 0, sizeof(struct vhost_vring_addr));
    2282                 :            :         rte_rwlock_write_unlock(&vq->access_lock);
    2283                 :            : 
    2284                 :          0 :         return RTE_VHOST_MSG_RESULT_REPLY;
    2285                 :            : }
    2286                 :            : 
    2287                 :            : /*
    2288                 :            :  * when virtio queues are ready to work, qemu will send us to
    2289                 :            :  * enable the virtio queue pair.
    2290                 :            :  */
    2291                 :            : static int
    2292                 :          0 : vhost_user_set_vring_enable(struct virtio_net **pdev,
    2293                 :            :                         struct vhu_msg_context *ctx,
    2294                 :            :                         int main_fd __rte_unused)
    2295                 :            : {
    2296                 :          0 :         struct virtio_net *dev = *pdev;
    2297                 :            :         struct vhost_virtqueue *vq;
    2298                 :          0 :         bool enable = !!ctx->msg.payload.state.num;
    2299                 :          0 :         int index = (int)ctx->msg.payload.state.index;
    2300                 :            : 
    2301                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    2302                 :            :                 "set queue enable: %d to qp idx: %d",
    2303                 :            :                 enable, index);
    2304                 :            : 
    2305                 :          0 :         vq = dev->virtqueue[index];
    2306         [ #  # ]:          0 :         if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) {
    2307                 :            :                 /* vhost_user_lock_all_queue_pairs locked all qps */
    2308                 :          0 :                 VHOST_USER_ASSERT_LOCK(dev, vq, VHOST_USER_SET_VRING_ENABLE);
    2309   [ #  #  #  #  :          0 :                 if (enable && vq->async && vq->async->pkts_inflight_n) {
                   #  # ]
    2310                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
    2311                 :            :                                 "failed to enable vring. Inflight packets must be completed first");
    2312                 :          0 :                         return RTE_VHOST_MSG_RESULT_ERR;
    2313                 :            :                 }
    2314                 :            :         }
    2315                 :            : 
    2316                 :          0 :         vq->enabled = enable;
    2317                 :            : 
    2318                 :          0 :         return RTE_VHOST_MSG_RESULT_OK;
    2319                 :            : }
    2320                 :            : 
    2321                 :            : static int
    2322                 :          0 : vhost_user_get_protocol_features(struct virtio_net **pdev,
    2323                 :            :                         struct vhu_msg_context *ctx,
    2324                 :            :                         int main_fd __rte_unused)
    2325                 :            : {
    2326                 :          0 :         struct virtio_net *dev = *pdev;
    2327                 :            :         uint64_t protocol_features;
    2328                 :            : 
    2329                 :          0 :         rte_vhost_driver_get_protocol_features(dev->ifname, &protocol_features);
    2330                 :            : 
    2331                 :          0 :         ctx->msg.payload.u64 = protocol_features;
    2332                 :          0 :         ctx->msg.size = sizeof(ctx->msg.payload.u64);
    2333                 :          0 :         ctx->fd_num = 0;
    2334                 :            : 
    2335                 :          0 :         return RTE_VHOST_MSG_RESULT_REPLY;
    2336                 :            : }
    2337                 :            : 
    2338                 :            : static int
    2339                 :          0 : vhost_user_set_protocol_features(struct virtio_net **pdev,
    2340                 :            :                         struct vhu_msg_context *ctx,
    2341                 :            :                         int main_fd __rte_unused)
    2342                 :            : {
    2343                 :          0 :         struct virtio_net *dev = *pdev;
    2344                 :          0 :         uint64_t protocol_features = ctx->msg.payload.u64;
    2345                 :          0 :         uint64_t backend_protocol_features = 0;
    2346                 :            : 
    2347                 :          0 :         rte_vhost_driver_get_protocol_features(dev->ifname,
    2348                 :            :                         &backend_protocol_features);
    2349         [ #  # ]:          0 :         if (protocol_features & ~backend_protocol_features) {
    2350                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "received invalid protocol features.");
    2351                 :          0 :                 return RTE_VHOST_MSG_RESULT_ERR;
    2352                 :            :         }
    2353                 :            : 
    2354                 :          0 :         dev->protocol_features = protocol_features;
    2355                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    2356                 :            :                 "negotiated Vhost-user protocol features: 0x%" PRIx64,
    2357                 :            :                 dev->protocol_features);
    2358                 :            : 
    2359                 :          0 :         return RTE_VHOST_MSG_RESULT_OK;
    2360                 :            : }
    2361                 :            : 
    2362                 :            : static int
    2363                 :          0 : vhost_user_set_log_base(struct virtio_net **pdev,
    2364                 :            :                         struct vhu_msg_context *ctx,
    2365                 :            :                         int main_fd __rte_unused)
    2366                 :            : {
    2367                 :          0 :         struct virtio_net *dev = *pdev;
    2368                 :          0 :         int fd = ctx->fds[0];
    2369                 :            :         uint64_t size, off;
    2370                 :            :         uint64_t alignment;
    2371                 :            :         void *addr;
    2372                 :            :         uint32_t i;
    2373                 :            : 
    2374         [ #  # ]:          0 :         if (validate_msg_fds(dev, ctx, 1) != 0)
    2375                 :            :                 return RTE_VHOST_MSG_RESULT_ERR;
    2376                 :            : 
    2377         [ #  # ]:          0 :         if (fd < 0) {
    2378                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "invalid log fd: %d", fd);
    2379                 :          0 :                 return RTE_VHOST_MSG_RESULT_ERR;
    2380                 :            :         }
    2381                 :            : 
    2382         [ #  # ]:          0 :         if (ctx->msg.size != sizeof(VhostUserLog)) {
    2383                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
    2384                 :            :                         "invalid log base msg size: %"PRId32" != %d",
    2385                 :            :                         ctx->msg.size, (int)sizeof(VhostUserLog));
    2386                 :          0 :                 goto close_msg_fds;
    2387                 :            :         }
    2388                 :            : 
    2389                 :          0 :         size = ctx->msg.payload.log.mmap_size;
    2390                 :          0 :         off  = ctx->msg.payload.log.mmap_offset;
    2391                 :            : 
    2392                 :            :         /* Check for mmap size and offset overflow. */
    2393         [ #  # ]:          0 :         if (off >= -size) {
    2394                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
    2395                 :            :                         "log offset %#"PRIx64" and log size %#"PRIx64" overflow",
    2396                 :            :                         off, size);
    2397                 :          0 :                 goto close_msg_fds;
    2398                 :            :         }
    2399                 :            : 
    2400                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    2401                 :            :                 "log mmap size: %"PRId64", offset: %"PRId64,
    2402                 :            :                 size, off);
    2403                 :            : 
    2404                 :            :         /*
    2405                 :            :          * mmap from 0 to workaround a hugepage mmap bug: mmap will
    2406                 :            :          * fail when offset is not page size aligned.
    2407                 :            :          */
    2408                 :          0 :         addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, off);
    2409                 :            :         alignment = get_blk_size(fd);
    2410                 :          0 :         close(fd);
    2411         [ #  # ]:          0 :         if (addr == MAP_FAILED) {
    2412                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "mmap log base failed!");
    2413                 :          0 :                 return RTE_VHOST_MSG_RESULT_ERR;
    2414                 :            :         }
    2415                 :            : 
    2416                 :            :         /*
    2417                 :            :          * Free previously mapped log memory on occasionally
    2418                 :            :          * multiple VHOST_USER_SET_LOG_BASE.
    2419                 :            :          */
    2420         [ #  # ]:          0 :         if (dev->log_addr) {
    2421                 :          0 :                 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
    2422                 :            :         }
    2423                 :          0 :         dev->log_addr = (uint64_t)(uintptr_t)addr;
    2424                 :          0 :         dev->log_base = dev->log_addr + off;
    2425                 :          0 :         dev->log_size = size;
    2426                 :          0 :         mem_set_dump(dev, addr, size + off, false, alignment);
    2427                 :            : 
    2428         [ #  # ]:          0 :         for (i = 0; i < dev->nr_vring; i++) {
    2429                 :          0 :                 struct vhost_virtqueue *vq = dev->virtqueue[i];
    2430                 :            : 
    2431                 :          0 :                 rte_free(vq->log_cache);
    2432                 :          0 :                 vq->log_cache = NULL;
    2433                 :          0 :                 vq->log_cache_nb_elem = 0;
    2434                 :          0 :                 vq->log_cache = rte_malloc_socket("vq log cache",
    2435                 :            :                                 sizeof(struct log_cache_entry) * VHOST_LOG_CACHE_NR,
    2436                 :            :                                 0, vq->numa_node);
    2437                 :            :                 /*
    2438                 :            :                  * If log cache alloc fail, don't fail migration, but no
    2439                 :            :                  * caching will be done, which will impact performance
    2440                 :            :                  */
    2441         [ #  # ]:          0 :                 if (!vq->log_cache)
    2442                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
    2443                 :            :                                 "failed to allocate VQ logging cache");
    2444                 :            :         }
    2445                 :            : 
    2446                 :            :         /*
    2447                 :            :          * The spec is not clear about it (yet), but QEMU doesn't expect
    2448                 :            :          * any payload in the reply.
    2449                 :            :          */
    2450                 :          0 :         ctx->msg.size = 0;
    2451                 :          0 :         ctx->fd_num = 0;
    2452                 :            : 
    2453                 :          0 :         return RTE_VHOST_MSG_RESULT_REPLY;
    2454                 :            : 
    2455                 :          0 : close_msg_fds:
    2456                 :          0 :         close_msg_fds(ctx);
    2457                 :          0 :         return RTE_VHOST_MSG_RESULT_ERR;
    2458                 :            : }
    2459                 :            : 
    2460                 :          0 : static int vhost_user_set_log_fd(struct virtio_net **pdev,
    2461                 :            :                         struct vhu_msg_context *ctx,
    2462                 :            :                         int main_fd __rte_unused)
    2463                 :            : {
    2464                 :          0 :         struct virtio_net *dev = *pdev;
    2465                 :            : 
    2466         [ #  # ]:          0 :         if (validate_msg_fds(dev, ctx, 1) != 0)
    2467                 :            :                 return RTE_VHOST_MSG_RESULT_ERR;
    2468                 :            : 
    2469                 :          0 :         close(ctx->fds[0]);
    2470                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, DEBUG, "not implemented.");
    2471                 :            : 
    2472                 :          0 :         return RTE_VHOST_MSG_RESULT_OK;
    2473                 :            : }
    2474                 :            : 
    2475                 :            : /*
    2476                 :            :  * An rarp packet is constructed and broadcasted to notify switches about
    2477                 :            :  * the new location of the migrated VM, so that packets from outside will
    2478                 :            :  * not be lost after migration.
    2479                 :            :  *
    2480                 :            :  * However, we don't actually "send" a rarp packet here, instead, we set
    2481                 :            :  * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
    2482                 :            :  */
    2483                 :            : static int
    2484                 :          0 : vhost_user_send_rarp(struct virtio_net **pdev,
    2485                 :            :                         struct vhu_msg_context *ctx,
    2486                 :            :                         int main_fd __rte_unused)
    2487                 :            : {
    2488                 :          0 :         struct virtio_net *dev = *pdev;
    2489                 :          0 :         uint8_t *mac = (uint8_t *)&ctx->msg.payload.u64;
    2490                 :            :         struct rte_vdpa_device *vdpa_dev;
    2491                 :            : 
    2492                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, DEBUG,
    2493                 :            :                 "MAC: " RTE_ETHER_ADDR_PRT_FMT,
    2494                 :            :                 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
    2495         [ #  # ]:          0 :         memcpy(dev->mac.addr_bytes, mac, 6);
    2496                 :            : 
    2497                 :            :         /*
    2498                 :            :          * Set the flag to inject a RARP broadcast packet at
    2499                 :            :          * rte_vhost_dequeue_burst().
    2500                 :            :          *
    2501                 :            :          * rte_memory_order_release ordering is for making sure the mac is
    2502                 :            :          * copied before the flag is set.
    2503                 :            :          */
    2504                 :          0 :         rte_atomic_store_explicit(&dev->broadcast_rarp, 1, rte_memory_order_release);
    2505                 :          0 :         vdpa_dev = dev->vdpa_dev;
    2506   [ #  #  #  # ]:          0 :         if (vdpa_dev && vdpa_dev->ops->migration_done)
    2507                 :          0 :                 vdpa_dev->ops->migration_done(dev->vid);
    2508                 :            : 
    2509                 :          0 :         return RTE_VHOST_MSG_RESULT_OK;
    2510                 :            : }
    2511                 :            : 
    2512                 :            : static int
    2513                 :          0 : vhost_user_net_set_mtu(struct virtio_net **pdev,
    2514                 :            :                         struct vhu_msg_context *ctx,
    2515                 :            :                         int main_fd __rte_unused)
    2516                 :            : {
    2517                 :          0 :         struct virtio_net *dev = *pdev;
    2518                 :            : 
    2519         [ #  # ]:          0 :         if (ctx->msg.payload.u64 < VIRTIO_MIN_MTU ||
    2520                 :            :                         ctx->msg.payload.u64 > VIRTIO_MAX_MTU) {
    2521                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
    2522                 :            :                         "invalid MTU size (%"PRIu64")",
    2523                 :            :                         ctx->msg.payload.u64);
    2524                 :            : 
    2525                 :          0 :                 return RTE_VHOST_MSG_RESULT_ERR;
    2526                 :            :         }
    2527                 :            : 
    2528                 :          0 :         dev->mtu = ctx->msg.payload.u64;
    2529                 :            : 
    2530                 :          0 :         return RTE_VHOST_MSG_RESULT_OK;
    2531                 :            : }
    2532                 :            : 
    2533                 :            : static int
    2534                 :          0 : vhost_user_set_req_fd(struct virtio_net **pdev,
    2535                 :            :                         struct vhu_msg_context *ctx,
    2536                 :            :                         int main_fd __rte_unused)
    2537                 :            : {
    2538                 :          0 :         struct virtio_net *dev = *pdev;
    2539                 :          0 :         int fd = ctx->fds[0];
    2540                 :            : 
    2541         [ #  # ]:          0 :         if (validate_msg_fds(dev, ctx, 1) != 0)
    2542                 :            :                 return RTE_VHOST_MSG_RESULT_ERR;
    2543                 :            : 
    2544         [ #  # ]:          0 :         if (fd < 0) {
    2545                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
    2546                 :            :                         "invalid file descriptor for backend channel (%d)", fd);
    2547                 :          0 :                 return RTE_VHOST_MSG_RESULT_ERR;
    2548                 :            :         }
    2549                 :            : 
    2550         [ #  # ]:          0 :         if (dev->backend_req_fd >= 0)
    2551                 :          0 :                 close(dev->backend_req_fd);
    2552                 :            : 
    2553                 :          0 :         dev->backend_req_fd = fd;
    2554                 :            : 
    2555                 :          0 :         return RTE_VHOST_MSG_RESULT_OK;
    2556                 :            : }
    2557                 :            : 
    2558                 :            : static int
    2559                 :          0 : is_vring_iotlb_split(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
    2560                 :            : {
    2561                 :            :         struct vhost_vring_addr *ra;
    2562                 :            :         uint64_t start, end, len;
    2563                 :            : 
    2564                 :          0 :         start = imsg->iova;
    2565                 :          0 :         end = start + imsg->size;
    2566                 :            : 
    2567                 :            :         ra = &vq->ring_addrs;
    2568                 :          0 :         len = sizeof(struct vring_desc) * vq->size;
    2569   [ #  #  #  # ]:          0 :         if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start)
    2570                 :            :                 return 1;
    2571                 :            : 
    2572                 :          0 :         len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
    2573   [ #  #  #  # ]:          0 :         if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start)
    2574                 :            :                 return 1;
    2575                 :            : 
    2576                 :          0 :         len = sizeof(struct vring_used) +
    2577                 :          0 :                sizeof(struct vring_used_elem) * vq->size;
    2578   [ #  #  #  # ]:          0 :         if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
    2579                 :            :                 return 1;
    2580                 :            : 
    2581         [ #  # ]:          0 :         if (ra->flags & (1 << VHOST_VRING_F_LOG)) {
    2582                 :            :                 len = sizeof(uint64_t);
    2583         [ #  # ]:          0 :                 if (ra->log_guest_addr < end &&
    2584         [ #  # ]:          0 :                     (ra->log_guest_addr + len) > start)
    2585                 :          0 :                         return 1;
    2586                 :            :         }
    2587                 :            : 
    2588                 :            :         return 0;
    2589                 :            : }
    2590                 :            : 
    2591                 :            : static int
    2592                 :          0 : is_vring_iotlb_packed(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
    2593                 :            : {
    2594                 :            :         struct vhost_vring_addr *ra;
    2595                 :            :         uint64_t start, end, len;
    2596                 :            : 
    2597                 :          0 :         start = imsg->iova;
    2598                 :          0 :         end = start + imsg->size;
    2599                 :            : 
    2600                 :            :         ra = &vq->ring_addrs;
    2601                 :          0 :         len = sizeof(struct vring_packed_desc) * vq->size;
    2602   [ #  #  #  # ]:          0 :         if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start)
    2603                 :            :                 return 1;
    2604                 :            : 
    2605                 :            :         len = sizeof(struct vring_packed_desc_event);
    2606   [ #  #  #  # ]:          0 :         if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start)
    2607                 :            :                 return 1;
    2608                 :            : 
    2609                 :            :         len = sizeof(struct vring_packed_desc_event);
    2610   [ #  #  #  # ]:          0 :         if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
    2611                 :            :                 return 1;
    2612                 :            : 
    2613         [ #  # ]:          0 :         if (ra->flags & (1 << VHOST_VRING_F_LOG)) {
    2614                 :            :                 len = sizeof(uint64_t);
    2615         [ #  # ]:          0 :                 if (ra->log_guest_addr < end &&
    2616         [ #  # ]:          0 :                     (ra->log_guest_addr + len) > start)
    2617                 :          0 :                         return 1;
    2618                 :            :         }
    2619                 :            : 
    2620                 :            :         return 0;
    2621                 :            : }
    2622                 :            : 
    2623         [ #  # ]:          0 : static int is_vring_iotlb(struct virtio_net *dev,
    2624                 :            :                           struct vhost_virtqueue *vq,
    2625                 :            :                           struct vhost_iotlb_msg *imsg)
    2626                 :            : {
    2627         [ #  # ]:          0 :         if (vq_is_packed(dev))
    2628                 :          0 :                 return is_vring_iotlb_packed(vq, imsg);
    2629                 :            :         else
    2630                 :          0 :                 return is_vring_iotlb_split(vq, imsg);
    2631                 :            : }
    2632                 :            : 
    2633                 :            : static int
    2634                 :          0 : vhost_user_get_config(struct virtio_net **pdev,
    2635                 :            :                         struct vhu_msg_context *ctx,
    2636                 :            :                         int main_fd __rte_unused)
    2637                 :            : {
    2638                 :          0 :         struct virtio_net *dev = *pdev;
    2639                 :          0 :         struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
    2640                 :            :         int ret = 0;
    2641                 :            : 
    2642         [ #  # ]:          0 :         if (validate_msg_fds(dev, ctx, 0) != 0)
    2643                 :            :                 return RTE_VHOST_MSG_RESULT_ERR;
    2644                 :            : 
    2645         [ #  # ]:          0 :         if (!vdpa_dev) {
    2646                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "is not vDPA device!");
    2647                 :          0 :                 return RTE_VHOST_MSG_RESULT_ERR;
    2648                 :            :         }
    2649                 :            : 
    2650         [ #  # ]:          0 :         if (vdpa_dev->ops->get_config) {
    2651                 :          0 :                 ret = vdpa_dev->ops->get_config(dev->vid,
    2652                 :          0 :                                            ctx->msg.payload.cfg.region,
    2653                 :            :                                            ctx->msg.payload.cfg.size);
    2654         [ #  # ]:          0 :                 if (ret != 0) {
    2655                 :          0 :                         ctx->msg.size = 0;
    2656                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR, "get_config() return error!");
    2657                 :            :                 }
    2658                 :            :         } else {
    2659                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "get_config() not supported!");
    2660                 :            :         }
    2661                 :            : 
    2662                 :            :         return RTE_VHOST_MSG_RESULT_REPLY;
    2663                 :            : }
    2664                 :            : 
    2665                 :            : static int
    2666                 :          0 : vhost_user_set_config(struct virtio_net **pdev,
    2667                 :            :                         struct vhu_msg_context *ctx,
    2668                 :            :                         int main_fd __rte_unused)
    2669                 :            : {
    2670                 :          0 :         struct virtio_net *dev = *pdev;
    2671                 :          0 :         struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
    2672                 :            :         int ret = 0;
    2673                 :            : 
    2674         [ #  # ]:          0 :         if (validate_msg_fds(dev, ctx, 0) != 0)
    2675                 :            :                 return RTE_VHOST_MSG_RESULT_ERR;
    2676                 :            : 
    2677         [ #  # ]:          0 :         if (ctx->msg.payload.cfg.size > VHOST_USER_MAX_CONFIG_SIZE) {
    2678                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
    2679                 :            :                         "vhost_user_config size: %"PRIu32", should not be larger than %d",
    2680                 :            :                         ctx->msg.payload.cfg.size, VHOST_USER_MAX_CONFIG_SIZE);
    2681                 :          0 :                 goto out;
    2682                 :            :         }
    2683                 :            : 
    2684         [ #  # ]:          0 :         if (!vdpa_dev) {
    2685                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "is not vDPA device!");
    2686                 :          0 :                 goto out;
    2687                 :            :         }
    2688                 :            : 
    2689         [ #  # ]:          0 :         if (vdpa_dev->ops->set_config) {
    2690                 :          0 :                 ret = vdpa_dev->ops->set_config(dev->vid,
    2691                 :          0 :                         ctx->msg.payload.cfg.region,
    2692                 :            :                         ctx->msg.payload.cfg.offset,
    2693                 :            :                         ctx->msg.payload.cfg.size,
    2694                 :            :                         ctx->msg.payload.cfg.flags);
    2695         [ #  # ]:          0 :                 if (ret)
    2696                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR, "set_config() return error!");
    2697                 :            :         } else {
    2698                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "set_config() not supported!");
    2699                 :            :         }
    2700                 :            : 
    2701                 :            :         return RTE_VHOST_MSG_RESULT_OK;
    2702                 :            : 
    2703                 :            : out:
    2704                 :            :         return RTE_VHOST_MSG_RESULT_ERR;
    2705                 :            : }
    2706                 :            : 
    2707                 :            : static int
    2708                 :          0 : vhost_user_iotlb_msg(struct virtio_net **pdev,
    2709                 :            :                         struct vhu_msg_context *ctx,
    2710                 :            :                         int main_fd __rte_unused)
    2711                 :            : {
    2712                 :          0 :         struct virtio_net *dev = *pdev;
    2713                 :          0 :         struct vhost_iotlb_msg *imsg = &ctx->msg.payload.iotlb;
    2714                 :            :         uint16_t i;
    2715                 :            :         uint64_t vva, len, pg_sz;
    2716                 :            : 
    2717      [ #  #  # ]:          0 :         switch (imsg->type) {
    2718                 :          0 :         case VHOST_IOTLB_UPDATE:
    2719                 :          0 :                 len = imsg->size;
    2720                 :          0 :                 vva = qva_to_vva(dev, imsg->uaddr, &len);
    2721         [ #  # ]:          0 :                 if (!vva)
    2722                 :            :                         return RTE_VHOST_MSG_RESULT_ERR;
    2723                 :            : 
    2724                 :          0 :                 pg_sz = hua_to_alignment(dev->mem, (void *)(uintptr_t)vva);
    2725                 :            : 
    2726                 :          0 :                 vhost_user_iotlb_cache_insert(dev, imsg->iova, vva, 0, len, pg_sz, imsg->perm);
    2727                 :            : 
    2728         [ #  # ]:          0 :                 for (i = 0; i < dev->nr_vring; i++) {
    2729                 :          0 :                         struct vhost_virtqueue *vq = dev->virtqueue[i];
    2730                 :            : 
    2731         [ #  # ]:          0 :                         if (!vq)
    2732                 :          0 :                                 continue;
    2733                 :            : 
    2734         [ #  # ]:          0 :                         if (is_vring_iotlb(dev, vq, imsg)) {
    2735                 :          0 :                                 rte_rwlock_write_lock(&vq->access_lock);
    2736                 :          0 :                                 translate_ring_addresses(&dev, &vq);
    2737                 :          0 :                                 *pdev = dev;
    2738                 :          0 :                                 rte_rwlock_write_unlock(&vq->access_lock);
    2739                 :            :                         }
    2740                 :            :                 }
    2741                 :            :                 break;
    2742                 :          0 :         case VHOST_IOTLB_INVALIDATE:
    2743                 :          0 :                 vhost_user_iotlb_cache_remove(dev, imsg->iova, imsg->size);
    2744                 :            : 
    2745         [ #  # ]:          0 :                 for (i = 0; i < dev->nr_vring; i++) {
    2746                 :          0 :                         struct vhost_virtqueue *vq = dev->virtqueue[i];
    2747                 :            : 
    2748         [ #  # ]:          0 :                         if (!vq)
    2749                 :          0 :                                 continue;
    2750                 :            : 
    2751         [ #  # ]:          0 :                         if (is_vring_iotlb(dev, vq, imsg)) {
    2752                 :          0 :                                 rte_rwlock_write_lock(&vq->access_lock);
    2753                 :          0 :                                 vring_invalidate(dev, vq);
    2754                 :            :                                 rte_rwlock_write_unlock(&vq->access_lock);
    2755                 :            :                         }
    2756                 :            :                 }
    2757                 :            :                 break;
    2758                 :          0 :         default:
    2759                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "invalid IOTLB message type (%d)",
    2760                 :            :                         imsg->type);
    2761                 :          0 :                 return RTE_VHOST_MSG_RESULT_ERR;
    2762                 :            :         }
    2763                 :            : 
    2764                 :            :         return RTE_VHOST_MSG_RESULT_OK;
    2765                 :            : }
    2766                 :            : 
    2767                 :            : static int
    2768                 :          0 : vhost_user_set_postcopy_advise(struct virtio_net **pdev,
    2769                 :            :                         struct vhu_msg_context *ctx,
    2770                 :            :                         int main_fd __rte_unused)
    2771                 :            : {
    2772                 :          0 :         struct virtio_net *dev = *pdev;
    2773                 :            : #ifdef RTE_LIBRTE_VHOST_POSTCOPY
    2774                 :            :         struct uffdio_api api_struct;
    2775                 :            : 
    2776                 :          0 :         dev->postcopy_ufd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
    2777                 :            : 
    2778         [ #  # ]:          0 :         if (dev->postcopy_ufd == -1) {
    2779                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
    2780                 :            :                         "userfaultfd not available: %s",
    2781                 :            :                         strerror(errno));
    2782                 :          0 :                 return RTE_VHOST_MSG_RESULT_ERR;
    2783                 :            :         }
    2784                 :          0 :         api_struct.api = UFFD_API;
    2785                 :          0 :         api_struct.features = 0;
    2786         [ #  # ]:          0 :         if (ioctl(dev->postcopy_ufd, UFFDIO_API, &api_struct)) {
    2787                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
    2788                 :            :                         "UFFDIO_API ioctl failure: %s",
    2789                 :            :                         strerror(errno));
    2790                 :          0 :                 close(dev->postcopy_ufd);
    2791                 :          0 :                 dev->postcopy_ufd = -1;
    2792                 :          0 :                 return RTE_VHOST_MSG_RESULT_ERR;
    2793                 :            :         }
    2794                 :          0 :         ctx->fds[0] = dev->postcopy_ufd;
    2795                 :          0 :         ctx->fd_num = 1;
    2796                 :            : 
    2797                 :          0 :         return RTE_VHOST_MSG_RESULT_REPLY;
    2798                 :            : #else
    2799                 :            :         dev->postcopy_ufd = -1;
    2800                 :            :         ctx->fd_num = 0;
    2801                 :            : 
    2802                 :            :         return RTE_VHOST_MSG_RESULT_ERR;
    2803                 :            : #endif
    2804                 :            : }
    2805                 :            : 
    2806                 :            : static int
    2807                 :          0 : vhost_user_set_postcopy_listen(struct virtio_net **pdev,
    2808                 :            :                         struct vhu_msg_context *ctx __rte_unused,
    2809                 :            :                         int main_fd __rte_unused)
    2810                 :            : {
    2811                 :          0 :         struct virtio_net *dev = *pdev;
    2812                 :            : 
    2813   [ #  #  #  # ]:          0 :         if (dev->mem && dev->mem->nregions) {
    2814                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
    2815                 :            :                         "regions already registered at postcopy-listen");
    2816                 :          0 :                 return RTE_VHOST_MSG_RESULT_ERR;
    2817                 :            :         }
    2818                 :          0 :         dev->postcopy_listening = 1;
    2819                 :            : 
    2820                 :          0 :         return RTE_VHOST_MSG_RESULT_OK;
    2821                 :            : }
    2822                 :            : 
    2823                 :            : static int
    2824                 :          0 : vhost_user_postcopy_end(struct virtio_net **pdev,
    2825                 :            :                         struct vhu_msg_context *ctx,
    2826                 :            :                         int main_fd __rte_unused)
    2827                 :            : {
    2828                 :          0 :         struct virtio_net *dev = *pdev;
    2829                 :            : 
    2830                 :          0 :         dev->postcopy_listening = 0;
    2831         [ #  # ]:          0 :         if (dev->postcopy_ufd >= 0) {
    2832                 :          0 :                 close(dev->postcopy_ufd);
    2833                 :          0 :                 dev->postcopy_ufd = -1;
    2834                 :            :         }
    2835                 :            : 
    2836                 :          0 :         ctx->msg.payload.u64 = 0;
    2837                 :          0 :         ctx->msg.size = sizeof(ctx->msg.payload.u64);
    2838                 :          0 :         ctx->fd_num = 0;
    2839                 :            : 
    2840                 :          0 :         return RTE_VHOST_MSG_RESULT_REPLY;
    2841                 :            : }
    2842                 :            : 
    2843                 :            : static int
    2844                 :          0 : vhost_user_get_status(struct virtio_net **pdev,
    2845                 :            :                       struct vhu_msg_context *ctx,
    2846                 :            :                       int main_fd __rte_unused)
    2847                 :            : {
    2848                 :          0 :         struct virtio_net *dev = *pdev;
    2849                 :            : 
    2850                 :          0 :         ctx->msg.payload.u64 = dev->status;
    2851                 :          0 :         ctx->msg.size = sizeof(ctx->msg.payload.u64);
    2852                 :          0 :         ctx->fd_num = 0;
    2853                 :            : 
    2854                 :          0 :         return RTE_VHOST_MSG_RESULT_REPLY;
    2855                 :            : }
    2856                 :            : 
    2857                 :            : static int
    2858                 :          0 : vhost_user_set_status(struct virtio_net **pdev,
    2859                 :            :                         struct vhu_msg_context *ctx,
    2860                 :            :                         int main_fd __rte_unused)
    2861                 :            : {
    2862                 :          0 :         struct virtio_net *dev = *pdev;
    2863                 :            : 
    2864                 :            :         /* As per Virtio specification, the device status is 8bits long */
    2865         [ #  # ]:          0 :         if (ctx->msg.payload.u64 > UINT8_MAX) {
    2866                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
    2867                 :            :                         "invalid VHOST_USER_SET_STATUS payload 0x%" PRIx64,
    2868                 :            :                         ctx->msg.payload.u64);
    2869                 :          0 :                 return RTE_VHOST_MSG_RESULT_ERR;
    2870                 :            :         }
    2871                 :            : 
    2872                 :          0 :         dev->status = ctx->msg.payload.u64;
    2873                 :            : 
    2874         [ #  # ]:          0 :         if ((dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK) &&
    2875         [ #  # ]:          0 :             (dev->flags & VIRTIO_DEV_FEATURES_FAILED)) {
    2876                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
    2877                 :            :                         "FEATURES_OK bit is set but feature negotiation failed");
    2878                 :            :                 /*
    2879                 :            :                  * Clear the bit to let the driver know about the feature
    2880                 :            :                  * negotiation failure
    2881                 :            :                  */
    2882                 :          0 :                 dev->status &= ~VIRTIO_DEVICE_STATUS_FEATURES_OK;
    2883                 :            :         }
    2884                 :            : 
    2885                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO, "new device status(0x%08x):", dev->status);
    2886                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    2887                 :            :                 "\t-RESET: %u",
    2888                 :            :                 (dev->status == VIRTIO_DEVICE_STATUS_RESET));
    2889                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    2890                 :            :                 "\t-ACKNOWLEDGE: %u",
    2891                 :            :                 !!(dev->status & VIRTIO_DEVICE_STATUS_ACK));
    2892                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    2893                 :            :                 "\t-DRIVER: %u",
    2894                 :            :                 !!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER));
    2895                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    2896                 :            :                 "\t-FEATURES_OK: %u",
    2897                 :            :                 !!(dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK));
    2898                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    2899                 :            :                 "\t-DRIVER_OK: %u",
    2900                 :            :                 !!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK));
    2901                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    2902                 :            :                 "\t-DEVICE_NEED_RESET: %u",
    2903                 :            :                 !!(dev->status & VIRTIO_DEVICE_STATUS_DEV_NEED_RESET));
    2904                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO,
    2905                 :            :                 "\t-FAILED: %u",
    2906                 :            :                 !!(dev->status & VIRTIO_DEVICE_STATUS_FAILED));
    2907                 :            : 
    2908                 :          0 :         return RTE_VHOST_MSG_RESULT_OK;
    2909                 :            : }
    2910                 :            : 
    2911                 :            : #define VHOST_MESSAGE_HANDLER(id, handler, accepts_fd, lock_all_qps) \
    2912                 :            :         [id] = { #id, handler, accepts_fd, id ## _LOCK_ALL_QPS },
    2913                 :            : static vhost_message_handler_t vhost_message_handlers[] = {
    2914                 :            :         VHOST_MESSAGE_HANDLERS
    2915                 :            : };
    2916                 :            : #undef VHOST_MESSAGE_HANDLER
    2917                 :            : 
    2918                 :            : /* return bytes# of read on success or negative val on failure. */
    2919                 :            : static int
    2920                 :          0 : read_vhost_message(struct virtio_net *dev, int sockfd, struct  vhu_msg_context *ctx)
    2921                 :            : {
    2922                 :            :         int ret;
    2923                 :            : 
    2924                 :          0 :         ret = read_fd_message(dev->ifname, sockfd, (char *)&ctx->msg, VHOST_USER_HDR_SIZE,
    2925                 :          0 :                 ctx->fds, VHOST_MEMORY_MAX_NREGIONS, &ctx->fd_num);
    2926         [ #  # ]:          0 :         if (ret <= 0)
    2927                 :          0 :                 goto out;
    2928                 :            : 
    2929         [ #  # ]:          0 :         if (ret != VHOST_USER_HDR_SIZE) {
    2930                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "Unexpected header size read");
    2931                 :            :                 ret = -1;
    2932                 :          0 :                 goto out;
    2933                 :            :         }
    2934                 :            : 
    2935         [ #  # ]:          0 :         if (ctx->msg.size) {
    2936         [ #  # ]:          0 :                 if (ctx->msg.size > sizeof(ctx->msg.payload)) {
    2937                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR, "invalid msg size: %d",
    2938                 :            :                                 ctx->msg.size);
    2939                 :            :                         ret = -1;
    2940                 :          0 :                         goto out;
    2941                 :            :                 }
    2942         [ #  # ]:          0 :                 ret = read(sockfd, &ctx->msg.payload, ctx->msg.size);
    2943         [ #  # ]:          0 :                 if (ret <= 0)
    2944                 :          0 :                         goto out;
    2945         [ #  # ]:          0 :                 if (ret != (int)ctx->msg.size) {
    2946                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR, "read control message failed");
    2947                 :            :                         ret = -1;
    2948                 :          0 :                         goto out;
    2949                 :            :                 }
    2950                 :            :         }
    2951                 :            : 
    2952                 :          0 : out:
    2953         [ #  # ]:          0 :         if (ret <= 0)
    2954                 :          0 :                 close_msg_fds(ctx);
    2955                 :            : 
    2956                 :          0 :         return ret;
    2957                 :            : }
    2958                 :            : 
    2959                 :            : static int
    2960                 :            : send_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx)
    2961                 :            : {
    2962                 :          0 :         if (!ctx)
    2963                 :            :                 return 0;
    2964                 :            : 
    2965                 :          0 :         return send_fd_message(dev->ifname, sockfd, (char *)&ctx->msg,
    2966                 :          0 :                 VHOST_USER_HDR_SIZE + ctx->msg.size, ctx->fds, ctx->fd_num);
    2967                 :            : }
    2968                 :            : 
    2969                 :            : static int
    2970                 :          0 : send_vhost_reply(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx)
    2971                 :            : {
    2972         [ #  # ]:          0 :         if (!ctx)
    2973                 :            :                 return 0;
    2974                 :            : 
    2975                 :          0 :         ctx->msg.flags &= ~VHOST_USER_VERSION_MASK;
    2976                 :          0 :         ctx->msg.flags &= ~VHOST_USER_NEED_REPLY;
    2977                 :          0 :         ctx->msg.flags |= VHOST_USER_VERSION;
    2978                 :          0 :         ctx->msg.flags |= VHOST_USER_REPLY_MASK;
    2979                 :            : 
    2980                 :          0 :         return send_vhost_message(dev, sockfd, ctx);
    2981                 :            : }
    2982                 :            : 
    2983                 :            : static int
    2984                 :          0 : send_vhost_backend_message(struct virtio_net *dev, struct vhu_msg_context *ctx)
    2985                 :            : {
    2986         [ #  # ]:          0 :         return send_vhost_message(dev, dev->backend_req_fd, ctx);
    2987                 :            : }
    2988                 :            : 
    2989                 :            : static int
    2990                 :          0 : send_vhost_backend_message_process_reply(struct virtio_net *dev, struct vhu_msg_context *ctx)
    2991                 :            : {
    2992                 :            :         struct vhu_msg_context msg_reply;
    2993                 :            :         int ret;
    2994                 :            : 
    2995                 :          0 :         rte_spinlock_lock(&dev->backend_req_lock);
    2996                 :          0 :         ret = send_vhost_backend_message(dev, ctx);
    2997         [ #  # ]:          0 :         if (ret < 0) {
    2998                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to send config change (%d)", ret);
    2999                 :          0 :                 goto out;
    3000                 :            :         }
    3001                 :            : 
    3002                 :          0 :         ret = read_vhost_message(dev, dev->backend_req_fd, &msg_reply);
    3003         [ #  # ]:          0 :         if (ret <= 0) {
    3004         [ #  # ]:          0 :                 if (ret < 0)
    3005                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
    3006                 :            :                                 "vhost read backend message reply failed");
    3007                 :            :                 else
    3008                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, INFO, "vhost peer closed");
    3009                 :            :                 ret = -1;
    3010                 :          0 :                 goto out;
    3011                 :            :         }
    3012                 :            : 
    3013         [ #  # ]:          0 :         if (msg_reply.msg.request.backend != ctx->msg.request.backend) {
    3014                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
    3015                 :            :                         "received unexpected msg type (%u), expected %u",
    3016                 :            :                         msg_reply.msg.request.backend, ctx->msg.request.backend);
    3017                 :            :                 ret = -1;
    3018                 :          0 :                 goto out;
    3019                 :            :         }
    3020                 :            : 
    3021         [ #  # ]:          0 :         ret = msg_reply.msg.payload.u64 ? -1 : 0;
    3022                 :          0 : out:
    3023                 :            :         rte_spinlock_unlock(&dev->backend_req_lock);
    3024                 :          0 :         return ret;
    3025                 :            : }
    3026                 :            : 
    3027                 :            : /*
    3028                 :            :  * Allocate a queue pair if it hasn't been allocated yet
    3029                 :            :  */
    3030                 :            : static int
    3031                 :          0 : vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev,
    3032                 :            :                         struct vhu_msg_context *ctx)
    3033                 :            : {
    3034                 :            :         uint32_t vring_idx;
    3035                 :            : 
    3036   [ #  #  #  #  :          0 :         switch (ctx->msg.request.frontend) {
                      # ]
    3037                 :          0 :         case VHOST_USER_SET_VRING_KICK:
    3038                 :            :         case VHOST_USER_SET_VRING_CALL:
    3039                 :            :         case VHOST_USER_SET_VRING_ERR:
    3040                 :          0 :                 vring_idx = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
    3041                 :          0 :                 break;
    3042                 :          0 :         case VHOST_USER_SET_VRING_NUM:
    3043                 :            :         case VHOST_USER_SET_VRING_BASE:
    3044                 :            :         case VHOST_USER_GET_VRING_BASE:
    3045                 :            :         case VHOST_USER_SET_VRING_ENABLE:
    3046                 :          0 :                 vring_idx = ctx->msg.payload.state.index;
    3047                 :          0 :                 break;
    3048                 :          0 :         case VHOST_USER_SET_VRING_ADDR:
    3049                 :          0 :                 vring_idx = ctx->msg.payload.addr.index;
    3050                 :          0 :                 break;
    3051                 :          0 :         case VHOST_USER_SET_INFLIGHT_FD:
    3052                 :          0 :                 vring_idx = ctx->msg.payload.inflight.num_queues - 1;
    3053                 :          0 :                 break;
    3054                 :            :         default:
    3055                 :            :                 return 0;
    3056                 :            :         }
    3057                 :            : 
    3058         [ #  # ]:          0 :         if (vring_idx >= VHOST_MAX_VRING) {
    3059                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "invalid vring index: %u", vring_idx);
    3060                 :          0 :                 return -1;
    3061                 :            :         }
    3062                 :            : 
    3063         [ #  # ]:          0 :         if (dev->virtqueue[vring_idx])
    3064                 :            :                 return 0;
    3065                 :            : 
    3066                 :          0 :         return alloc_vring_queue(dev, vring_idx);
    3067                 :            : }
    3068                 :            : 
    3069                 :            : static void
    3070                 :          0 : vhost_user_lock_all_queue_pairs(struct virtio_net *dev)
    3071                 :            :         __rte_no_thread_safety_analysis
    3072                 :            : {
    3073                 :            :         unsigned int i = 0;
    3074                 :            :         unsigned int vq_num = 0;
    3075                 :            : 
    3076         [ #  # ]:          0 :         while (vq_num < dev->nr_vring) {
    3077                 :          0 :                 struct vhost_virtqueue *vq = dev->virtqueue[i];
    3078                 :            : 
    3079         [ #  # ]:          0 :                 if (vq) {
    3080                 :          0 :                         rte_rwlock_write_lock(&vq->access_lock);
    3081                 :          0 :                         vq_num++;
    3082                 :            :                 }
    3083                 :          0 :                 i++;
    3084                 :            :         }
    3085                 :          0 : }
    3086                 :            : 
    3087                 :            : static void
    3088                 :          0 : vhost_user_unlock_all_queue_pairs(struct virtio_net *dev)
    3089                 :            :         __rte_no_thread_safety_analysis
    3090                 :            : {
    3091                 :            :         unsigned int i = 0;
    3092                 :            :         unsigned int vq_num = 0;
    3093                 :            : 
    3094         [ #  # ]:          0 :         while (vq_num < dev->nr_vring) {
    3095                 :          0 :                 struct vhost_virtqueue *vq = dev->virtqueue[i];
    3096                 :            : 
    3097         [ #  # ]:          0 :                 if (vq) {
    3098                 :            :                         rte_rwlock_write_unlock(&vq->access_lock);
    3099                 :          0 :                         vq_num++;
    3100                 :            :                 }
    3101                 :          0 :                 i++;
    3102                 :            :         }
    3103                 :          0 : }
    3104                 :            : 
    3105                 :            : int
    3106         [ #  # ]:          0 : vhost_user_msg_handler(int vid, int fd)
    3107                 :            : {
    3108                 :            :         struct virtio_net *dev;
    3109                 :            :         struct vhu_msg_context ctx;
    3110                 :            :         vhost_message_handler_t *msg_handler;
    3111                 :            :         struct rte_vdpa_device *vdpa_dev;
    3112                 :            :         int msg_result = RTE_VHOST_MSG_RESULT_OK;
    3113                 :            :         int ret;
    3114                 :            :         int unlock_required = 0;
    3115                 :            :         bool handled;
    3116                 :            :         uint32_t request;
    3117                 :            :         uint32_t i;
    3118                 :            :         uint16_t blk_call_fd;
    3119                 :            : 
    3120                 :          0 :         dev = get_device(vid);
    3121         [ #  # ]:          0 :         if (dev == NULL)
    3122                 :            :                 return -1;
    3123                 :            : 
    3124         [ #  # ]:          0 :         if (!dev->notify_ops) {
    3125                 :          0 :                 dev->notify_ops = vhost_driver_callback_get(dev->ifname);
    3126         [ #  # ]:          0 :                 if (!dev->notify_ops) {
    3127                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
    3128                 :            :                                 "failed to get callback ops for driver");
    3129                 :          0 :                         return -1;
    3130                 :            :                 }
    3131                 :            :         }
    3132                 :            : 
    3133                 :          0 :         ctx.msg.request.frontend = VHOST_USER_NONE;
    3134                 :          0 :         ret = read_vhost_message(dev, fd, &ctx);
    3135         [ #  # ]:          0 :         if (ret == 0) {
    3136                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, INFO, "vhost peer closed");
    3137                 :          0 :                 return -1;
    3138                 :            :         }
    3139                 :            : 
    3140                 :          0 :         request = ctx.msg.request.frontend;
    3141         [ #  # ]:          0 :         if (request > VHOST_USER_NONE && request < RTE_DIM(vhost_message_handlers))
    3142                 :          0 :                 msg_handler = &vhost_message_handlers[request];
    3143                 :            :         else
    3144                 :            :                 msg_handler = NULL;
    3145                 :            : 
    3146         [ #  # ]:          0 :         if (ret < 0) {
    3147   [ #  #  #  #  :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "vhost read message %s%s%sfailed",
                   #  # ]
    3148                 :            :                                 msg_handler != NULL ? "for " : "",
    3149                 :            :                                 msg_handler != NULL ? msg_handler->description : "",
    3150                 :            :                                 msg_handler != NULL ? " " : "");
    3151                 :          0 :                 return -1;
    3152                 :            :         }
    3153                 :            : 
    3154   [ #  #  #  # ]:          0 :         if (msg_handler != NULL && msg_handler->description != NULL) {
    3155         [ #  # ]:          0 :                 if (request != VHOST_USER_IOTLB_MSG)
    3156                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, INFO,
    3157                 :            :                                 "read message %s",
    3158                 :            :                                 msg_handler->description);
    3159                 :            :                 else
    3160                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, DEBUG,
    3161                 :            :                                 "read message %s",
    3162                 :            :                                 msg_handler->description);
    3163                 :            :         } else {
    3164                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, DEBUG, "external request %d", request);
    3165                 :            :         }
    3166                 :            : 
    3167                 :          0 :         ret = vhost_user_check_and_alloc_queue_pair(dev, &ctx);
    3168         [ #  # ]:          0 :         if (ret < 0) {
    3169                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc queue");
    3170                 :          0 :                 return -1;
    3171                 :            :         }
    3172                 :            : 
    3173                 :            :         /*
    3174                 :            :          * Note: we don't lock all queues on VHOST_USER_GET_VRING_BASE
    3175                 :            :          * and VHOST_USER_RESET_OWNER, since it is sent when virtio stops
    3176                 :            :          * and device is destroyed. destroy_device waits for queues to be
    3177                 :            :          * inactive, so it is safe. Otherwise taking the access_lock
    3178                 :            :          * would cause a dead lock.
    3179                 :            :          */
    3180   [ #  #  #  # ]:          0 :         if (msg_handler != NULL && msg_handler->lock_all_qps) {
    3181         [ #  # ]:          0 :                 if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) {
    3182                 :          0 :                         vhost_user_lock_all_queue_pairs(dev);
    3183                 :            :                         unlock_required = 1;
    3184                 :            :                 }
    3185                 :            :         }
    3186                 :            : 
    3187                 :            :         handled = false;
    3188         [ #  # ]:          0 :         if (dev->extern_ops.pre_msg_handle) {
    3189                 :            :                 RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0);
    3190                 :          0 :                 msg_result = (*dev->extern_ops.pre_msg_handle)(dev->vid, &ctx);
    3191      [ #  #  # ]:          0 :                 switch (msg_result) {
    3192                 :          0 :                 case RTE_VHOST_MSG_RESULT_REPLY:
    3193                 :          0 :                         send_vhost_reply(dev, fd, &ctx);
    3194                 :            :                         /* Fall-through */
    3195                 :          0 :                 case RTE_VHOST_MSG_RESULT_ERR:
    3196                 :            :                 case RTE_VHOST_MSG_RESULT_OK:
    3197                 :            :                         handled = true;
    3198                 :          0 :                         goto skip_to_post_handle;
    3199                 :            :                 case RTE_VHOST_MSG_RESULT_NOT_HANDLED:
    3200                 :            :                 default:
    3201                 :            :                         break;
    3202                 :            :                 }
    3203                 :            :         }
    3204                 :            : 
    3205   [ #  #  #  # ]:          0 :         if (msg_handler == NULL || msg_handler->callback == NULL)
    3206                 :          0 :                 goto skip_to_post_handle;
    3207                 :            : 
    3208   [ #  #  #  # ]:          0 :         if (!msg_handler->accepts_fd && validate_msg_fds(dev, &ctx, 0) != 0) {
    3209                 :            :                 msg_result = RTE_VHOST_MSG_RESULT_ERR;
    3210                 :            :         } else {
    3211                 :          0 :                 msg_result = msg_handler->callback(&dev, &ctx, fd);
    3212                 :            :         }
    3213                 :            : 
    3214   [ #  #  #  # ]:          0 :         switch (msg_result) {
    3215                 :          0 :         case RTE_VHOST_MSG_RESULT_ERR:
    3216                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
    3217                 :            :                         "processing %s failed.",
    3218                 :            :                         msg_handler->description);
    3219                 :            :                 handled = true;
    3220                 :          0 :                 break;
    3221                 :          0 :         case RTE_VHOST_MSG_RESULT_OK:
    3222                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, DEBUG,
    3223                 :            :                         "processing %s succeeded.",
    3224                 :            :                         msg_handler->description);
    3225                 :            :                 handled = true;
    3226                 :          0 :                 break;
    3227                 :          0 :         case RTE_VHOST_MSG_RESULT_REPLY:
    3228                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, DEBUG,
    3229                 :            :                         "processing %s succeeded and needs reply.",
    3230                 :            :                         msg_handler->description);
    3231                 :          0 :                 send_vhost_reply(dev, fd, &ctx);
    3232                 :            :                 handled = true;
    3233                 :          0 :                 break;
    3234                 :            :         default:
    3235                 :            :                 break;
    3236                 :            :         }
    3237                 :            : 
    3238                 :          0 : skip_to_post_handle:
    3239         [ #  # ]:          0 :         if (msg_result != RTE_VHOST_MSG_RESULT_ERR &&
    3240         [ #  # ]:          0 :                         dev->extern_ops.post_msg_handle) {
    3241                 :            :                 RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0);
    3242                 :          0 :                 msg_result = (*dev->extern_ops.post_msg_handle)(dev->vid, &ctx);
    3243      [ #  #  # ]:          0 :                 switch (msg_result) {
    3244                 :          0 :                 case RTE_VHOST_MSG_RESULT_REPLY:
    3245                 :          0 :                         send_vhost_reply(dev, fd, &ctx);
    3246                 :            :                         /* Fall-through */
    3247                 :            :                 case RTE_VHOST_MSG_RESULT_ERR:
    3248                 :            :                 case RTE_VHOST_MSG_RESULT_OK:
    3249                 :            :                         handled = true;
    3250                 :            :                 case RTE_VHOST_MSG_RESULT_NOT_HANDLED:
    3251                 :            :                 default:
    3252                 :            :                         break;
    3253                 :            :                 }
    3254                 :            :         }
    3255                 :            : 
    3256                 :            :         /* If message was not handled at this stage, treat it as an error */
    3257         [ #  # ]:          0 :         if (!handled) {
    3258                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
    3259                 :            :                         "vhost message (req: %d) was not handled.",
    3260                 :            :                         request);
    3261                 :          0 :                 close_msg_fds(&ctx);
    3262                 :            :                 msg_result = RTE_VHOST_MSG_RESULT_ERR;
    3263                 :            :         }
    3264                 :            : 
    3265                 :            :         /*
    3266                 :            :          * If the request required a reply that was already sent,
    3267                 :            :          * this optional reply-ack won't be sent as the
    3268                 :            :          * VHOST_USER_NEED_REPLY was cleared in send_vhost_reply().
    3269                 :            :          */
    3270         [ #  # ]:          0 :         if (ctx.msg.flags & VHOST_USER_NEED_REPLY) {
    3271                 :          0 :                 ctx.msg.payload.u64 = msg_result == RTE_VHOST_MSG_RESULT_ERR;
    3272                 :          0 :                 ctx.msg.size = sizeof(ctx.msg.payload.u64);
    3273                 :          0 :                 ctx.fd_num = 0;
    3274                 :          0 :                 send_vhost_reply(dev, fd, &ctx);
    3275         [ #  # ]:          0 :         } else if (msg_result == RTE_VHOST_MSG_RESULT_ERR) {
    3276                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "vhost message handling failed.");
    3277                 :            :                 ret = -1;
    3278                 :          0 :                 goto unlock;
    3279                 :            :         }
    3280                 :            : 
    3281         [ #  # ]:          0 :         for (i = 0; i < dev->nr_vring; i++) {
    3282                 :          0 :                 struct vhost_virtqueue *vq = dev->virtqueue[i];
    3283                 :          0 :                 bool cur_ready = vq_is_ready(dev, vq);
    3284                 :            : 
    3285   [ #  #  #  #  :          0 :                 if (cur_ready != (vq && vq->ready)) {
                   #  # ]
    3286                 :          0 :                         vq->ready = cur_ready;
    3287                 :          0 :                         vhost_user_notify_queue_state(dev, vq, cur_ready);
    3288                 :            :                 }
    3289                 :            :         }
    3290                 :            : 
    3291                 :          0 : unlock:
    3292         [ #  # ]:          0 :         if (unlock_required)
    3293                 :          0 :                 vhost_user_unlock_all_queue_pairs(dev);
    3294                 :            : 
    3295   [ #  #  #  # ]:          0 :         if (ret != 0 || !virtio_is_ready(dev))
    3296                 :          0 :                 goto out;
    3297                 :            : 
    3298                 :            :         /*
    3299                 :            :          * Virtio is now ready. If not done already, it is time
    3300                 :            :          * to notify the application it can process the rings and
    3301                 :            :          * configure the vDPA device if present.
    3302                 :            :          */
    3303                 :            : 
    3304         [ #  # ]:          0 :         if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
    3305   [ #  #  #  # ]:          0 :                 if (!dev->notify_ops->new_device ||
    3306                 :          0 :                         dev->notify_ops->new_device(dev->vid) == 0)
    3307                 :          0 :                         dev->flags |= VIRTIO_DEV_RUNNING;
    3308                 :            :         }
    3309                 :            : 
    3310                 :          0 :         vdpa_dev = dev->vdpa_dev;
    3311         [ #  # ]:          0 :         if (!vdpa_dev)
    3312                 :          0 :                 goto out;
    3313                 :            : 
    3314         [ #  # ]:          0 :         if (vdpa_dev->type == RTE_VHOST_VDPA_DEVICE_TYPE_BLK) {
    3315         [ #  # ]:          0 :                 if (request == VHOST_USER_SET_VRING_CALL) {
    3316                 :          0 :                         blk_call_fd = ctx.msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
    3317         [ #  # ]:          0 :                         if (blk_call_fd != dev->nr_vring - 1)
    3318                 :          0 :                                 goto out;
    3319                 :            :                 } else {
    3320                 :          0 :                         goto out;
    3321                 :            :                 }
    3322                 :            :         }
    3323                 :            : 
    3324         [ #  # ]:          0 :         if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) {
    3325         [ #  # ]:          0 :                 if (vdpa_dev->ops->dev_conf(dev->vid))
    3326                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to configure vDPA device");
    3327                 :            :                 else
    3328                 :          0 :                         dev->flags |= VIRTIO_DEV_VDPA_CONFIGURED;
    3329                 :            :         }
    3330                 :            : 
    3331                 :          0 : out:
    3332                 :            :         return ret;
    3333                 :            : }
    3334                 :            : 
    3335                 :            : static int
    3336                 :          0 : vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm)
    3337                 :            : {
    3338                 :            :         int ret;
    3339                 :          0 :         struct vhu_msg_context ctx = {
    3340                 :            :                 .msg = {
    3341                 :            :                         .request.backend = VHOST_USER_BACKEND_IOTLB_MSG,
    3342                 :            :                         .flags = VHOST_USER_VERSION,
    3343                 :            :                         .size = sizeof(ctx.msg.payload.iotlb),
    3344                 :            :                         .payload.iotlb = {
    3345                 :            :                                 .iova = iova,
    3346                 :            :                                 .perm = perm,
    3347                 :            :                                 .type = VHOST_IOTLB_MISS,
    3348                 :            :                         },
    3349                 :            :                 },
    3350                 :            :         };
    3351                 :            : 
    3352                 :          0 :         ret = send_vhost_message(dev, dev->backend_req_fd, &ctx);
    3353         [ #  # ]:          0 :         if (ret < 0) {
    3354                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
    3355                 :            :                         "failed to send IOTLB miss message (%d)",
    3356                 :            :                         ret);
    3357                 :          0 :                 return ret;
    3358                 :            :         }
    3359                 :            : 
    3360                 :            :         return 0;
    3361                 :            : }
    3362                 :            : 
    3363                 :            : RTE_EXPORT_SYMBOL(rte_vhost_backend_config_change)
    3364                 :            : int
    3365                 :          0 : rte_vhost_backend_config_change(int vid, bool need_reply)
    3366                 :            : {
    3367         [ #  # ]:          0 :         struct vhu_msg_context ctx = {
    3368                 :            :                 .msg = {
    3369                 :            :                         .request.backend = VHOST_USER_BACKEND_CONFIG_CHANGE_MSG,
    3370                 :            :                         .flags = VHOST_USER_VERSION,
    3371                 :            :                         .size = 0,
    3372                 :            :                 }
    3373                 :            :         };
    3374                 :            :         struct virtio_net *dev;
    3375                 :            :         int ret;
    3376                 :            : 
    3377                 :            :         dev = get_device(vid);
    3378         [ #  # ]:          0 :         if (!dev)
    3379                 :            :                 return -ENODEV;
    3380                 :            : 
    3381         [ #  # ]:          0 :         if (!need_reply) {
    3382                 :            :                 ret = send_vhost_backend_message(dev, &ctx);
    3383                 :            :         } else {
    3384                 :          0 :                 ctx.msg.flags |= VHOST_USER_NEED_REPLY;
    3385                 :          0 :                 ret = send_vhost_backend_message_process_reply(dev, &ctx);
    3386                 :            :         }
    3387                 :            : 
    3388         [ #  # ]:          0 :         if (ret < 0)
    3389                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to send config change (%d)", ret);
    3390                 :            :         return ret;
    3391                 :            : }
    3392                 :            : 
    3393                 :          0 : static int vhost_user_backend_set_vring_host_notifier(struct virtio_net *dev,
    3394                 :            :                                                     int index, int fd,
    3395                 :            :                                                     uint64_t offset,
    3396                 :            :                                                     uint64_t size)
    3397                 :            : {
    3398                 :            :         int ret;
    3399                 :          0 :         struct vhu_msg_context ctx = {
    3400                 :            :                 .msg = {
    3401                 :            :                         .request.backend = VHOST_USER_BACKEND_VRING_HOST_NOTIFIER_MSG,
    3402                 :            :                         .flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY,
    3403                 :            :                         .size = sizeof(ctx.msg.payload.area),
    3404                 :            :                         .payload.area = {
    3405                 :          0 :                                 .u64 = index & VHOST_USER_VRING_IDX_MASK,
    3406                 :            :                                 .size = size,
    3407                 :            :                                 .offset = offset,
    3408                 :            :                         },
    3409                 :            :                 },
    3410                 :            :         };
    3411                 :            : 
    3412         [ #  # ]:          0 :         if (fd < 0)
    3413                 :          0 :                 ctx.msg.payload.area.u64 |= VHOST_USER_VRING_NOFD_MASK;
    3414                 :            :         else {
    3415                 :          0 :                 ctx.fds[0] = fd;
    3416                 :          0 :                 ctx.fd_num = 1;
    3417                 :            :         }
    3418                 :            : 
    3419                 :          0 :         ret = send_vhost_backend_message_process_reply(dev, &ctx);
    3420         [ #  # ]:          0 :         if (ret < 0)
    3421                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to set host notifier (%d)", ret);
    3422                 :            : 
    3423                 :          0 :         return ret;
    3424                 :            : }
    3425                 :            : 
    3426                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_vhost_host_notifier_ctrl)
    3427         [ #  # ]:          0 : int rte_vhost_host_notifier_ctrl(int vid, uint16_t qid, bool enable)
    3428                 :            : {
    3429                 :            :         struct virtio_net *dev;
    3430                 :            :         struct rte_vdpa_device *vdpa_dev;
    3431                 :            :         int vfio_device_fd, ret = 0;
    3432                 :            :         uint64_t offset, size;
    3433                 :            :         unsigned int i, q_start, q_last;
    3434                 :            : 
    3435                 :            :         dev = get_device(vid);
    3436         [ #  # ]:          0 :         if (!dev)
    3437                 :            :                 return -ENODEV;
    3438                 :            : 
    3439                 :          0 :         vdpa_dev = dev->vdpa_dev;
    3440         [ #  # ]:          0 :         if (vdpa_dev == NULL)
    3441                 :            :                 return -ENODEV;
    3442                 :            : 
    3443         [ #  # ]:          0 :         if (!(dev->features & (1ULL << VIRTIO_F_VERSION_1)) ||
    3444                 :            :             !(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) ||
    3445                 :            :             !(dev->protocol_features &
    3446                 :            :                         (1ULL << VHOST_USER_PROTOCOL_F_BACKEND_REQ)) ||
    3447                 :            :             !(dev->protocol_features &
    3448         [ #  # ]:          0 :                         (1ULL << VHOST_USER_PROTOCOL_F_BACKEND_SEND_FD)) ||
    3449                 :            :             !(dev->protocol_features &
    3450                 :            :                         (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER)))
    3451                 :            :                 return -ENOTSUP;
    3452                 :            : 
    3453         [ #  # ]:          0 :         if (qid == RTE_VHOST_QUEUE_ALL) {
    3454                 :            :                 q_start = 0;
    3455                 :          0 :                 q_last = dev->nr_vring - 1;
    3456                 :            :         } else {
    3457         [ #  # ]:          0 :                 if (qid >= dev->nr_vring)
    3458                 :            :                         return -EINVAL;
    3459                 :            :                 q_start = qid;
    3460                 :            :                 q_last = qid;
    3461                 :            :         }
    3462                 :            : 
    3463         [ #  # ]:          0 :         if (vdpa_dev->ops->get_vfio_device_fd == NULL)
    3464                 :            :                 return -ENOTSUP;
    3465         [ #  # ]:          0 :         if (vdpa_dev->ops->get_notify_area == NULL)
    3466                 :            :                 return -ENOTSUP;
    3467                 :            : 
    3468                 :          0 :         vfio_device_fd = vdpa_dev->ops->get_vfio_device_fd(vid);
    3469         [ #  # ]:          0 :         if (vfio_device_fd < 0)
    3470                 :            :                 return -ENOTSUP;
    3471                 :            : 
    3472         [ #  # ]:          0 :         if (enable) {
    3473         [ #  # ]:          0 :                 for (i = q_start; i <= q_last; i++) {
    3474         [ #  # ]:          0 :                         if (vdpa_dev->ops->get_notify_area(vid, i, &offset,
    3475                 :            :                                         &size) < 0) {
    3476                 :            :                                 ret = -ENOTSUP;
    3477                 :          0 :                                 goto disable;
    3478                 :            :                         }
    3479                 :            : 
    3480         [ #  # ]:          0 :                         if (vhost_user_backend_set_vring_host_notifier(dev, i,
    3481                 :            :                                         vfio_device_fd, offset, size) < 0) {
    3482                 :            :                                 ret = -EFAULT;
    3483                 :          0 :                                 goto disable;
    3484                 :            :                         }
    3485                 :            :                 }
    3486                 :            :         } else {
    3487                 :          0 : disable:
    3488         [ #  # ]:          0 :                 for (i = q_start; i <= q_last; i++) {
    3489                 :          0 :                         vhost_user_backend_set_vring_host_notifier(dev, i, -1,
    3490                 :            :                                         0, 0);
    3491                 :            :                 }
    3492                 :            :         }
    3493                 :            : 
    3494                 :            :         return ret;
    3495                 :            : }
    3496                 :            : 
    3497                 :            : static int
    3498                 :          0 : vhost_user_inject_irq(struct virtio_net *dev __rte_unused, struct vhost_virtqueue *vq)
    3499                 :            : {
    3500         [ #  # ]:          0 :         if (vq->callfd < 0)
    3501                 :            :                 return -1;
    3502                 :            : 
    3503                 :          0 :         return eventfd_write(vq->callfd, (eventfd_t)1);
    3504                 :            : }
    3505                 :            : 
    3506                 :            : static struct vhost_backend_ops vhost_user_backend_ops = {
    3507                 :            :         .iotlb_miss = vhost_user_iotlb_miss,
    3508                 :            :         .inject_irq = vhost_user_inject_irq,
    3509                 :            : };
    3510                 :            : 
    3511                 :            : int
    3512                 :          0 : vhost_user_new_device(void)
    3513                 :            : {
    3514                 :          0 :         return vhost_new_device(&vhost_user_backend_ops);
    3515                 :            : }

Generated by: LCOV version 1.14