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

Generated by: LCOV version 1.14