LCOV - code coverage report
Current view: top level - lib/vhost - vduse.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 577 0.0 %
Date: 2026-11-01 19:11:22 Functions: 0 18 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 281 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright (c) 2023 Red Hat, Inc.
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <uapi/linux/vduse.h>
       6                 :            : 
       7                 :            : #include <stdint.h>
       8                 :            : #include <stdio.h>
       9                 :            : #include <unistd.h>
      10                 :            : #include <fcntl.h>
      11                 :            : 
      12                 :            : #include <linux/virtio_net.h>
      13                 :            : 
      14                 :            : #include <sys/ioctl.h>
      15                 :            : #include <sys/mman.h>
      16                 :            : #include <sys/stat.h>
      17                 :            : 
      18                 :            : #include <rte_common.h>
      19                 :            : #include <rte_eal_paging.h>
      20                 :            : #include <rte_thread.h>
      21                 :            : 
      22                 :            : #include "fd_man.h"
      23                 :            : #include "iotlb.h"
      24                 :            : #include "vduse.h"
      25                 :            : #include "vhost.h"
      26                 :            : #include "virtio_net_ctrl.h"
      27                 :            : 
      28                 :            : #define VHOST_VDUSE_API_VERSION 1ULL
      29                 :            : #define VDUSE_CTRL_PATH "/dev/vduse/control"
      30                 :            : 
      31                 :            : struct vduse {
      32                 :            :         struct fdset *fdset;
      33                 :            : };
      34                 :            : 
      35                 :            : static struct vduse vduse;
      36                 :            : 
      37                 :            : static const char * const vduse_reqs_str[] = {
      38                 :            :         "VDUSE_GET_VQ_STATE",
      39                 :            :         "VDUSE_SET_STATUS",
      40                 :            :         "VDUSE_UPDATE_IOTLB",
      41                 :            :         "VDUSE_SET_VQ_GROUP_ASID",
      42                 :            :         "VDUSE_SET_VQ_READY",
      43                 :            :         "VDUSE_SUSPEND",
      44                 :            : };
      45                 :            : 
      46                 :            : #define vduse_req_id_to_str(id) \
      47                 :            :         (id < RTE_DIM(vduse_reqs_str) ? \
      48                 :            :         vduse_reqs_str[id] : "Unknown")
      49                 :            : 
      50                 :            : static const uint64_t supported_vduse_features =
      51                 :            :         RTE_BIT64(VDUSE_F_QUEUE_READY) | RTE_BIT64(VDUSE_F_SUSPEND);
      52                 :            : 
      53                 :            : static uint64_t vduse_vq_to_group(struct virtio_net *dev, struct vhost_virtqueue *vq)
      54                 :            : {
      55                 :          0 :         if (dev->vduse_api_ver < 1)
      56                 :            :                 return 0;
      57                 :            : 
      58   [ #  #  #  # ]:          0 :         if (vq == dev->cvq)
      59                 :          0 :                 return 1;
      60                 :            : 
      61                 :            :         return 0;
      62                 :            : }
      63                 :            : 
      64                 :            : static int
      65                 :          0 : vduse_inject_irq(struct virtio_net *dev, struct vhost_virtqueue *vq)
      66                 :            : {
      67                 :          0 :         return ioctl(dev->vduse_dev_fd, VDUSE_VQ_INJECT_IRQ, &vq->index);
      68                 :            : }
      69                 :            : 
      70                 :            : static void
      71                 :          0 : vduse_iotlb_remove_notify(uint64_t addr, uint64_t offset, uint64_t size)
      72                 :            : {
      73                 :          0 :         munmap((void *)(uintptr_t)addr, offset + size);
      74                 :          0 : }
      75                 :            : 
      76                 :            : static int
      77                 :          0 : vduse_iotlb_miss(struct virtio_net *dev, int asid, uint64_t iova, uint8_t perm __rte_unused)
      78                 :            : {
      79                 :          0 :         struct vduse_iotlb_entry_v2 entry = {};
      80                 :            :         uint64_t size, page_size;
      81                 :            :         struct stat stat;
      82                 :            :         void *mmap_addr;
      83                 :            :         int fd, ret;
      84                 :            : 
      85                 :          0 :         entry.start = iova;
      86                 :          0 :         entry.last = iova + 1;
      87                 :          0 :         entry.asid = asid;
      88                 :            : 
      89         [ #  # ]:          0 :         if (dev->vduse_api_ver < 1) {
      90                 :          0 :                 ret = ioctl(dev->vduse_dev_fd, VDUSE_IOTLB_GET_FD, &entry);
      91                 :            :         } else {
      92                 :          0 :                 ret = ioctl(dev->vduse_dev_fd, VDUSE_IOTLB_GET_FD2, &entry);
      93                 :            :         }
      94         [ #  # ]:          0 :         if (ret < 0) {
      95                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to get IOTLB entry for 0x%" PRIx64,
      96                 :            :                                 iova);
      97                 :          0 :                 return -1;
      98                 :            :         }
      99                 :            : 
     100                 :            :         fd = ret;
     101                 :            : 
     102                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, DEBUG, "New IOTLB entry:");
     103                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, DEBUG, "\tASID: %d", entry.asid);
     104                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, DEBUG, "\tIOVA: %" PRIx64 " - %" PRIx64,
     105                 :            :                         (uint64_t)entry.start, (uint64_t)entry.last);
     106                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, DEBUG, "\toffset: %" PRIx64, (uint64_t)entry.offset);
     107                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, DEBUG, "\tfd: %d", fd);
     108                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, DEBUG, "\tperm: %x", entry.perm);
     109                 :            : 
     110                 :          0 :         size = entry.last - entry.start + 1;
     111                 :          0 :         mmap_addr = mmap(0, size + entry.offset, entry.perm, MAP_SHARED, fd, 0);
     112         [ #  # ]:          0 :         if (mmap_addr == MAP_FAILED) {
     113                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
     114                 :            :                                 "Failed to mmap IOTLB entry for 0x%" PRIx64 ": %s",
     115                 :            :                                 iova, strerror(errno));
     116                 :            :                 ret = -1;
     117                 :          0 :                 goto close_fd;
     118                 :            :         }
     119                 :            : 
     120                 :          0 :         ret = fstat(fd, &stat);
     121         [ #  # ]:          0 :         if (ret < 0) {
     122                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to get page size.");
     123                 :          0 :                 munmap(mmap_addr, entry.offset + size);
     124                 :          0 :                 goto close_fd;
     125                 :            :         }
     126                 :          0 :         page_size = (uint64_t)stat.st_blksize;
     127                 :            : 
     128                 :          0 :         vhost_user_iotlb_cache_insert(dev, asid, entry.start, (uint64_t)(uintptr_t)mmap_addr,
     129                 :          0 :                 entry.offset, size, page_size, entry.perm);
     130                 :            : 
     131                 :            :         ret = 0;
     132                 :          0 : close_fd:
     133                 :          0 :         close(fd);
     134                 :            : 
     135                 :          0 :         return ret;
     136                 :            : }
     137                 :            : 
     138                 :            : static struct vhost_backend_ops vduse_backend_ops = {
     139                 :            :         .iotlb_miss = vduse_iotlb_miss,
     140                 :            :         .iotlb_remove_notify = vduse_iotlb_remove_notify,
     141                 :            :         .inject_irq = vduse_inject_irq,
     142                 :            : };
     143                 :            : 
     144                 :            : static void
     145                 :          0 : vduse_control_queue_event(int fd, void *arg, int *close __rte_unused)
     146                 :            : {
     147                 :            :         struct virtio_net *dev = arg;
     148                 :            :         uint64_t buf;
     149                 :            :         int ret;
     150                 :            : 
     151                 :          0 :         ret = read(fd, &buf, sizeof(buf));
     152         [ #  # ]:          0 :         if (ret < 0) {
     153                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to read control queue event: %s",
     154                 :            :                                 strerror(errno));
     155                 :          0 :                 return;
     156                 :            :         }
     157                 :            : 
     158                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, DEBUG, "Control queue kicked");
     159         [ #  # ]:          0 :         if (virtio_net_ctrl_handle(dev))
     160                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to handle ctrl request");
     161                 :            : }
     162                 :            : 
     163                 :            : static void
     164                 :          0 : vduse_vring_setup(struct virtio_net *dev, unsigned int index, bool reconnect)
     165                 :            : {
     166                 :          0 :         struct vhost_virtqueue *vq = dev->virtqueue[index];
     167                 :            :         struct vhost_vring_addr *ra = &vq->ring_addrs;
     168                 :          0 :         struct vduse_vq_info vq_info = { 0 };
     169                 :            :         struct vduse_vq_eventfd vq_efd;
     170                 :            :         int ret;
     171                 :            : 
     172                 :          0 :         vq_info.index = index;
     173                 :          0 :         ret = ioctl(dev->vduse_dev_fd, VDUSE_VQ_GET_INFO, &vq_info);
     174         [ #  # ]:          0 :         if (ret) {
     175                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to get VQ %u info: %s",
     176                 :            :                                 index, strerror(errno));
     177                 :          0 :                 return;
     178                 :            :         }
     179                 :            : 
     180         [ #  # ]:          0 :         if (reconnect) {
     181                 :          0 :                 vq->last_avail_idx = vq->reconnect_log->last_avail_idx;
     182                 :          0 :                 vq->last_used_idx = vq->reconnect_log->last_avail_idx;
     183                 :            :         } else {
     184                 :          0 :                 vq->last_avail_idx = vq_info.split.avail_index;
     185                 :          0 :                 vq->last_used_idx = vq_info.split.avail_index;
     186                 :            :         }
     187                 :          0 :         vq->size = vq_info.num;
     188                 :          0 :         vq->ready = true;
     189                 :          0 :         vq->enabled = vq_info.ready;
     190                 :          0 :         ra->desc_user_addr = vq_info.desc_addr;
     191                 :          0 :         ra->avail_user_addr = vq_info.driver_addr;
     192                 :          0 :         ra->used_user_addr = vq_info.device_addr;
     193                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO, "VQ %u info:", index);
     194                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO, "\tnum: %u", vq_info.num);
     195                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO, "\tdesc_addr: %llx",
     196                 :            :                         (unsigned long long)vq_info.desc_addr);
     197                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO, "\tdriver_addr: %llx",
     198                 :            :                         (unsigned long long)vq_info.driver_addr);
     199                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO, "\tdevice_addr: %llx",
     200                 :            :                         (unsigned long long)vq_info.device_addr);
     201                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO, "\tavail_idx: %u", vq->last_avail_idx);
     202                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO, "\tused_idx: %u", vq->last_used_idx);
     203                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO, "\tready: %u", vq_info.ready);
     204                 :          0 :         vq->kickfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
     205         [ #  # ]:          0 :         if (vq->kickfd < 0) {
     206                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to init kickfd for VQ %u: %s",
     207                 :            :                                 index, strerror(errno));
     208                 :          0 :                 vq->kickfd = VIRTIO_INVALID_EVENTFD;
     209                 :          0 :                 return;
     210                 :            :         }
     211                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO, "\tkick fd: %d", vq->kickfd);
     212                 :            : 
     213                 :          0 :         vq->shadow_used_split = rte_malloc_socket(NULL,
     214                 :          0 :                                 vq->size * sizeof(struct vring_used_elem),
     215                 :            :                                 RTE_CACHE_LINE_SIZE, 0);
     216                 :          0 :         vq->batch_copy_elems = rte_malloc_socket(NULL,
     217                 :          0 :                                 vq->size * sizeof(struct batch_copy_elem),
     218                 :            :                                 RTE_CACHE_LINE_SIZE, 0);
     219                 :            : 
     220                 :          0 :         rte_rwlock_write_lock(&vq->access_lock);
     221                 :            :         vhost_user_iotlb_rd_lock(vq);
     222         [ #  # ]:          0 :         if (vring_translate(dev, vq))
     223                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to translate vring %d addresses",
     224                 :            :                                 index);
     225                 :            : 
     226         [ #  # ]:          0 :         if (vhost_enable_guest_notification(dev, vq, 0))
     227                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
     228                 :            :                                 "Failed to disable guest notifications on vring %d",
     229                 :            :                                 index);
     230                 :            :         vhost_user_iotlb_rd_unlock(vq);
     231                 :            :         rte_rwlock_write_unlock(&vq->access_lock);
     232                 :            : 
     233                 :          0 :         vq_efd.index = index;
     234                 :          0 :         vq_efd.fd = vq->kickfd;
     235                 :            : 
     236                 :          0 :         ret = ioctl(dev->vduse_dev_fd, VDUSE_VQ_SETUP_KICKFD, &vq_efd);
     237         [ #  # ]:          0 :         if (ret) {
     238                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to setup kickfd for VQ %u: %s",
     239                 :            :                                 index, strerror(errno));
     240                 :          0 :                 close(vq->kickfd);
     241                 :          0 :                 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
     242                 :          0 :                 return;
     243                 :            :         }
     244                 :            : 
     245         [ #  # ]:          0 :         if (vq == dev->cvq) {
     246                 :          0 :                 ret = fdset_add(vduse.fdset, vq->kickfd, vduse_control_queue_event, NULL, dev);
     247         [ #  # ]:          0 :                 if (ret) {
     248                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
     249                 :            :                                         "Failed to setup kickfd handler for VQ %u: %s",
     250                 :            :                                         index, strerror(errno));
     251                 :          0 :                         vq_efd.fd = VDUSE_EVENTFD_DEASSIGN;
     252                 :          0 :                         ioctl(dev->vduse_dev_fd, VDUSE_VQ_SETUP_KICKFD, &vq_efd);
     253                 :          0 :                         close(vq->kickfd);
     254                 :          0 :                         vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
     255                 :            :                 }
     256                 :          0 :                 vhost_enable_guest_notification(dev, vq, 1);
     257                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, INFO, "Ctrl queue event handler installed");
     258                 :            :         }
     259                 :            : }
     260                 :            : 
     261                 :            : static void
     262                 :          0 : vduse_vring_cleanup(struct virtio_net *dev, unsigned int index)
     263                 :            : {
     264                 :          0 :         struct vhost_virtqueue *vq = dev->virtqueue[index];
     265                 :            :         struct vduse_vq_eventfd vq_efd;
     266                 :            :         int ret;
     267                 :            : 
     268   [ #  #  #  # ]:          0 :         if (vq == dev->cvq && vq->kickfd >= 0)
     269                 :          0 :                 fdset_del(vduse.fdset, vq->kickfd);
     270                 :            : 
     271                 :          0 :         vq_efd.index = index;
     272                 :          0 :         vq_efd.fd = VDUSE_EVENTFD_DEASSIGN;
     273                 :            : 
     274                 :          0 :         ret = ioctl(dev->vduse_dev_fd, VDUSE_VQ_SETUP_KICKFD, &vq_efd);
     275         [ #  # ]:          0 :         if (ret)
     276                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to cleanup kickfd for VQ %u: %s",
     277                 :            :                                 index, strerror(errno));
     278                 :            : 
     279                 :          0 :         close(vq->kickfd);
     280                 :          0 :         vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
     281                 :            : 
     282                 :          0 :         rte_rwlock_write_lock(&vq->access_lock);
     283                 :          0 :         vring_invalidate(dev, vq);
     284                 :            :         rte_rwlock_write_unlock(&vq->access_lock);
     285                 :            : 
     286                 :          0 :         rte_free(vq->batch_copy_elems);
     287                 :          0 :         vq->batch_copy_elems = NULL;
     288                 :            : 
     289                 :          0 :         rte_free(vq->shadow_used_split);
     290                 :          0 :         vq->shadow_used_split = NULL;
     291                 :            : 
     292                 :          0 :         vq->enabled = false;
     293                 :          0 :         vq->ready = false;
     294                 :          0 :         vq->size = 0;
     295                 :          0 :         vq->last_used_idx = 0;
     296                 :          0 :         vq->last_avail_idx = 0;
     297                 :          0 :         vq->asid = 0;
     298                 :          0 : }
     299                 :            : 
     300                 :            : /*
     301                 :            :  * Tests show that virtqueues get ready at the first retry at worst,
     302                 :            :  * but let's be on the safe side and allow more retries.
     303                 :            :  */
     304                 :            : #define VDUSE_VQ_READY_POLL_MAX_RETRIES 100
     305                 :            : 
     306                 :            : static int
     307                 :          0 : vduse_wait_for_virtqueues_ready(struct virtio_net *dev)
     308                 :            : {
     309                 :            :         unsigned int i;
     310                 :            :         int ret;
     311                 :            : 
     312         [ #  # ]:          0 :         for (i = 0; i < dev->nr_vring; i++) {
     313                 :            :                 int retry_count = 0;
     314                 :            : 
     315         [ #  # ]:          0 :                 while (retry_count < VDUSE_VQ_READY_POLL_MAX_RETRIES) {
     316                 :          0 :                         struct vduse_vq_info vq_info = { 0 };
     317                 :            : 
     318                 :          0 :                         vq_info.index = i;
     319                 :          0 :                         ret = ioctl(dev->vduse_dev_fd, VDUSE_VQ_GET_INFO, &vq_info);
     320         [ #  # ]:          0 :                         if (ret) {
     321                 :          0 :                                 VHOST_CONFIG_LOG(dev->ifname, ERR,
     322                 :            :                                         "Failed to get VQ %u info while polling ready state: %s",
     323                 :            :                                         i, strerror(errno));
     324                 :          0 :                                 return -1;
     325                 :            :                         }
     326                 :            : 
     327         [ #  # ]:          0 :                         if (vq_info.ready) {
     328                 :          0 :                                 VHOST_CONFIG_LOG(dev->ifname, DEBUG,
     329                 :            :                                         "VQ %u is ready after %u retries", i, retry_count);
     330                 :          0 :                                 break;
     331                 :            :                         }
     332                 :            : 
     333                 :          0 :                         retry_count++;
     334                 :          0 :                         usleep(1000);
     335                 :            :                 }
     336                 :            : 
     337         [ #  # ]:          0 :                 if (retry_count >= VDUSE_VQ_READY_POLL_MAX_RETRIES) {
     338                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
     339                 :            :                                 "VQ %u ready state polling timeout after %u retries",
     340                 :            :                                 i, VDUSE_VQ_READY_POLL_MAX_RETRIES);
     341                 :          0 :                         return -1;
     342                 :            :                 }
     343                 :            :         }
     344                 :            : 
     345                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO, "All virtqueues are ready after polling");
     346                 :          0 :         return 0;
     347                 :            : }
     348                 :            : 
     349                 :            : static void
     350                 :          0 : vduse_device_start(struct virtio_net *dev, bool reconnect)
     351                 :            : {
     352                 :            :         unsigned int i, ret;
     353                 :            : 
     354                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO, "Starting device...");
     355                 :            : 
     356                 :          0 :         dev->notify_ops = vhost_driver_callback_get(dev->ifname);
     357         [ #  # ]:          0 :         if (!dev->notify_ops) {
     358                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
     359                 :            :                                 "Failed to get callback ops for driver");
     360                 :          0 :                 return;
     361                 :            :         }
     362                 :            : 
     363                 :          0 :         ret = ioctl(dev->vduse_dev_fd, VDUSE_DEV_GET_FEATURES, &dev->features);
     364         [ #  # ]:          0 :         if (ret) {
     365                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to get features: %s",
     366                 :            :                                 strerror(errno));
     367                 :          0 :                 return;
     368                 :            :         }
     369                 :            : 
     370   [ #  #  #  # ]:          0 :         if (reconnect && dev->features != dev->reconnect_log->features) {
     371                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
     372                 :            :                                 "Mismatch between reconnect file features 0x%" PRIx64 " & device features 0x%" PRIx64,
     373                 :            :                                 dev->reconnect_log->features, dev->features);
     374                 :          0 :                 return;
     375                 :            :         }
     376                 :            : 
     377                 :          0 :         dev->reconnect_log->features = dev->features;
     378                 :            : 
     379                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO, "Negotiated Virtio features: 0x%" PRIx64,
     380                 :            :                 dev->features);
     381                 :            : 
     382         [ #  # ]:          0 :         if (dev->features &
     383                 :            :                 ((1ULL << VIRTIO_NET_F_MRG_RXBUF) |
     384                 :            :                  (1ULL << VIRTIO_F_VERSION_1) |
     385                 :            :                  (1ULL << VIRTIO_F_RING_PACKED))) {
     386                 :          0 :                 dev->vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
     387                 :            :         } else {
     388                 :          0 :                 dev->vhost_hlen = sizeof(struct virtio_net_hdr);
     389                 :            :         }
     390                 :            : 
     391         [ #  # ]:          0 :         for (i = 0; i < dev->nr_vring; i++)
     392                 :          0 :                 vduse_vring_setup(dev, i, reconnect);
     393                 :            : 
     394                 :          0 :         dev->flags |= VIRTIO_DEV_READY;
     395                 :            : 
     396   [ #  #  #  # ]:          0 :         if (!dev->notify_ops->new_device ||
     397                 :          0 :                 dev->notify_ops->new_device(dev->vid) == 0)
     398                 :          0 :                 dev->flags |= VIRTIO_DEV_RUNNING;
     399                 :            : 
     400         [ #  # ]:          0 :         for (i = 0; i < dev->nr_vring; i++) {
     401                 :          0 :                 struct vhost_virtqueue *vq = dev->virtqueue[i];
     402                 :            : 
     403         [ #  # ]:          0 :                 if (vq == dev->cvq)
     404                 :          0 :                         continue;
     405                 :            : 
     406         [ #  # ]:          0 :                 if (dev->notify_ops->vring_state_changed)
     407                 :          0 :                         dev->notify_ops->vring_state_changed(dev->vid, i, vq->enabled);
     408                 :            :         }
     409                 :            : }
     410                 :            : 
     411                 :            : static void
     412                 :          0 : vduse_device_stop(struct virtio_net *dev)
     413                 :            : {
     414                 :            :         unsigned int i;
     415                 :            : 
     416                 :          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO, "Stopping device...");
     417                 :            : 
     418                 :          0 :         vhost_destroy_device_notify(dev);
     419                 :            : 
     420                 :          0 :         dev->flags &= ~VIRTIO_DEV_READY;
     421                 :          0 :         dev->vduse_suspended = false;
     422                 :            : 
     423         [ #  # ]:          0 :         for (i = 0; i < dev->nr_vring; i++)
     424                 :          0 :                 vduse_vring_cleanup(dev, i);
     425                 :            : 
     426         [ #  # ]:          0 :         for (i = 0; i < IOTLB_MAX_ASID; i++)
     427                 :          0 :                 vhost_user_iotlb_flush_all(dev, i);
     428                 :          0 : }
     429                 :            : 
     430                 :            : static void
     431                 :          0 : vduse_events_handler(int fd, void *arg, int *close __rte_unused)
     432                 :            : {
     433                 :            :         struct virtio_net *dev = arg;
     434                 :            :         struct vduse_dev_request req;
     435                 :            :         struct vduse_dev_response resp;
     436                 :            :         struct vhost_virtqueue *vq;
     437                 :          0 :         uint8_t old_status = dev->status;
     438                 :            :         int ret;
     439                 :            : 
     440                 :            :         memset(&resp, 0, sizeof(resp));
     441                 :            : 
     442                 :          0 :         ret = read(fd, &req, sizeof(req));
     443         [ #  # ]:          0 :         if (ret < 0) {
     444                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to read request: %s",
     445                 :            :                                 strerror(errno));
     446                 :          0 :                 return;
     447         [ #  # ]:          0 :         } else if (ret < (int)sizeof(req)) {
     448                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "Incomplete to read request %d", ret);
     449                 :          0 :                 return;
     450                 :            :         }
     451                 :            : 
     452         [ #  # ]:          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO, "New request: %s (%u)",
     453                 :            :                         vduse_req_id_to_str(req.type), req.type);
     454                 :            : 
     455   [ #  #  #  #  :          0 :         switch (req.type) {
                #  #  # ]
     456                 :          0 :         case VDUSE_GET_VQ_STATE:
     457                 :          0 :                 vq = dev->virtqueue[req.vq_state.index];
     458                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, INFO, "\tvq index: %u, avail_index: %u",
     459                 :            :                                 req.vq_state.index, vq->last_avail_idx);
     460                 :          0 :                 resp.vq_state.split.avail_index = vq->last_avail_idx;
     461                 :          0 :                 resp.result = VDUSE_REQ_RESULT_OK;
     462                 :          0 :                 break;
     463                 :          0 :         case VDUSE_SET_STATUS:
     464                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, INFO, "\tnew status: 0x%08x",
     465                 :            :                                 req.s.status);
     466                 :          0 :                 old_status = dev->status;
     467                 :          0 :                 dev->status = req.s.status;
     468                 :          0 :                 dev->reconnect_log->status = dev->status;
     469                 :          0 :                 resp.result = VDUSE_REQ_RESULT_OK;
     470                 :          0 :                 break;
     471                 :          0 :         case VDUSE_UPDATE_IOTLB: {
     472                 :            :                 struct vhost_iotlb_msg imsg;
     473                 :            :                 uint64_t start, last;
     474                 :            :                 uint32_t asid;
     475                 :            :                 uint32_t i;
     476                 :            : 
     477         [ #  # ]:          0 :                 if (dev->vduse_api_ver < 1) {
     478                 :          0 :                         start = req.iova.start;
     479                 :          0 :                         last = req.iova.last;
     480                 :            :                         asid = 0;
     481                 :            :                 } else {
     482                 :          0 :                         start = req.iova_v2.start;
     483                 :          0 :                         last = req.iova_v2.last;
     484                 :          0 :                         asid = req.iova_v2.asid;
     485                 :            :                 }
     486                 :            : 
     487         [ #  # ]:          0 :                 if (asid >= RTE_DIM(dev->iotlb)) {
     488                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
     489                 :            :                                 "Invalid ASID %u in IOTLB update", asid);
     490                 :          0 :                         resp.result = VDUSE_REQ_RESULT_FAILED;
     491                 :          0 :                         break;
     492                 :            :                 }
     493                 :            : 
     494                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, INFO, "\t(ASID %d) IOVA range: %" PRIx64 " - %" PRIx64,
     495                 :            :                         asid, start, last);
     496                 :          0 :                 vhost_user_iotlb_cache_remove(dev, asid, start, last - start + 1);
     497                 :            : 
     498                 :            :                 /* Invalidate vring translations if affected by this range */
     499                 :          0 :                 imsg.iova = start;
     500                 :          0 :                 imsg.size = last - start + 1;
     501                 :            : 
     502         [ #  # ]:          0 :                 for (i = 0; i < dev->nr_vring; i++) {
     503                 :          0 :                         vq = dev->virtqueue[i];
     504                 :            : 
     505         [ #  # ]:          0 :                         if (!vq)
     506                 :          0 :                                 continue;
     507                 :            : 
     508         [ #  # ]:          0 :                         if ((uint32_t)vq->asid != asid)
     509                 :          0 :                                 continue;
     510                 :            : 
     511         [ #  # ]:          0 :                         if (is_vring_iotlb(dev, vq, &imsg)) {
     512                 :          0 :                                 rte_rwlock_write_lock(&vq->access_lock);
     513                 :          0 :                                 vring_invalidate(dev, vq);
     514                 :            :                                 rte_rwlock_write_unlock(&vq->access_lock);
     515                 :            :                         }
     516                 :            :                 }
     517                 :            : 
     518                 :          0 :                 resp.result = VDUSE_REQ_RESULT_OK;
     519                 :          0 :                 break;
     520                 :            :         }
     521                 :          0 :         case VDUSE_SET_VQ_GROUP_ASID: {
     522                 :            :                 uint32_t i;
     523                 :            : 
     524         [ #  # ]:          0 :                 if (dev->vduse_api_ver < 1) {
     525                 :          0 :                         resp.result = VDUSE_REQ_RESULT_FAILED;
     526                 :          0 :                         break;
     527                 :            :                 }
     528                 :            : 
     529         [ #  # ]:          0 :                 if (req.vq_group_asid.asid >= RTE_DIM(dev->iotlb)) {
     530                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
     531                 :            :                                 "Invalid ASID %u for group %u",
     532                 :            :                                 req.vq_group_asid.asid, req.vq_group_asid.group);
     533                 :          0 :                         resp.result = VDUSE_REQ_RESULT_FAILED;
     534                 :          0 :                         break;
     535                 :            :                 }
     536                 :            : 
     537                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, INFO, "\tAssigning ASID %d to group %d",
     538                 :            :                                 req.vq_group_asid.asid, req.vq_group_asid.group);
     539                 :            : 
     540         [ #  # ]:          0 :                 for (i = 0; i < dev->nr_vring; i++) {
     541         [ #  # ]:          0 :                         vq = dev->virtqueue[i];
     542                 :            : 
     543         [ #  # ]:          0 :                         if (vduse_vq_to_group(dev, vq) == req.vq_group_asid.group) {
     544                 :          0 :                                 vq->asid = req.vq_group_asid.asid;
     545                 :          0 :                                 VHOST_CONFIG_LOG(dev->ifname, INFO, "\t\tVQ %d gets ASID %d",
     546                 :            :                                                 i, req.vq_group_asid.asid);
     547                 :            :                         }
     548                 :            :                 }
     549                 :          0 :                 resp.result = VDUSE_REQ_RESULT_OK;
     550                 :          0 :                 break;
     551                 :            :         }
     552                 :          0 :         case VDUSE_SET_VQ_READY: {
     553                 :            :                 uint32_t i;
     554                 :            : 
     555         [ #  # ]:          0 :                 if (!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK)) {
     556                 :            :                         /*
     557                 :            :                          * dev->notify_ops is NULL if !S_DRIVER_OK,
     558                 :            :                          * vduse_device_start will check the queue readiness.
     559                 :            :                          */
     560                 :          0 :                         resp.result = VDUSE_REQ_RESULT_OK;
     561                 :          0 :                         break;
     562                 :            :                 }
     563         [ #  # ]:          0 :                 if (!(dev->vduse_features & RTE_BIT64(VDUSE_F_QUEUE_READY))) {
     564                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
     565                 :            :                                 "Unexpected ready message with no ready feature acked");
     566                 :          0 :                         resp.result = VDUSE_REQ_RESULT_FAILED;
     567                 :          0 :                         break;
     568                 :            :                 }
     569         [ #  # ]:          0 :                 if (dev->vduse_suspended) {
     570                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
     571                 :            :                                 "SET_VQ_READY received on suspended device");
     572                 :          0 :                         resp.result = VDUSE_REQ_RESULT_FAILED;
     573                 :          0 :                         break;
     574                 :            :                 }
     575                 :            : 
     576                 :          0 :                 i = req.vq_ready.num;
     577         [ #  # ]:          0 :                 if (i >= dev->nr_vring) {
     578                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
     579                 :            :                                         "Invalid virtqueue index: %u", i);
     580                 :          0 :                         resp.result = VDUSE_REQ_RESULT_FAILED;
     581                 :          0 :                         break;
     582                 :            :                 }
     583                 :            : 
     584                 :          0 :                 vq = dev->virtqueue[i];
     585   [ #  #  #  # ]:          0 :                 if (dev->notify_ops == NULL || dev->notify_ops->vring_state_changed == NULL) {
     586                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
     587                 :            :                                          "No ops->vring_state_changed");
     588                 :          0 :                         resp.result = VDUSE_REQ_RESULT_FAILED;
     589                 :          0 :                         break;
     590                 :            :                 }
     591                 :            : 
     592                 :          0 :                 ret = dev->notify_ops->vring_state_changed(dev->vid, i,
     593                 :          0 :                                 req.vq_ready.ready);
     594                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, INFO,
     595                 :            :                                 "\t\t VQ %d gets ready %d ok %d", i,
     596                 :            :                                 req.vq_ready.ready, ret);
     597         [ #  # ]:          0 :                 if (ret != 0) {
     598                 :          0 :                         resp.result = VDUSE_REQ_RESULT_FAILED;
     599                 :          0 :                         break;
     600                 :            :                 }
     601                 :            : 
     602                 :          0 :                 vq->enabled = req.vq_ready.ready;
     603                 :          0 :                 resp.result = VDUSE_REQ_RESULT_OK;
     604                 :          0 :                 break;
     605                 :            :         }
     606                 :          0 :         case VDUSE_SUSPEND: {
     607                 :            :                 uint32_t i;
     608                 :            : 
     609         [ #  # ]:          0 :                 if (!(dev->vduse_features & RTE_BIT64(VDUSE_F_SUSPEND))) {
     610                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
     611                 :            :                                 "Unnegotiated suspend message");
     612                 :          0 :                         resp.result = VDUSE_REQ_RESULT_FAILED;
     613                 :          0 :                         break;
     614                 :            :                 }
     615         [ #  # ]:          0 :                 if (!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK)) {
     616                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR,
     617                 :            :                                 "Unexpected suspend message with no DRIVER_OK");
     618                 :          0 :                         resp.result = VDUSE_REQ_RESULT_FAILED;
     619                 :          0 :                         break;
     620                 :            :                 }
     621                 :          0 :                 for (i = 0; dev->notify_ops != NULL &&
     622   [ #  #  #  # ]:          0 :                             dev->notify_ops->vring_state_changed != NULL &&
     623         [ #  # ]:          0 :                             i < dev->nr_vring; i++) {
     624         [ #  # ]:          0 :                         if (dev->virtqueue[i] == dev->cvq)
     625                 :          0 :                                 continue;
     626                 :            : 
     627                 :          0 :                         ret = dev->notify_ops->vring_state_changed(dev->vid, i, false);
     628         [ #  # ]:          0 :                         if (ret) {
     629                 :          0 :                                 VHOST_CONFIG_LOG(dev->ifname, ERR,
     630                 :            :                                         "Failed to disable VQ %u for suspend", i);
     631                 :          0 :                                 resp.result = VDUSE_REQ_RESULT_FAILED;
     632                 :          0 :                                 goto out;
     633                 :            :                         }
     634                 :            :                 }
     635                 :          0 :                 dev->vduse_suspended = true;
     636                 :          0 :                 resp.result = VDUSE_REQ_RESULT_OK;
     637                 :          0 :                 break;
     638                 :            :         }
     639                 :          0 :         default:
     640                 :          0 :                 resp.result = VDUSE_REQ_RESULT_FAILED;
     641                 :          0 :                 break;
     642                 :            :         }
     643                 :            : 
     644                 :          0 : out:
     645                 :          0 :         resp.request_id = req.request_id;
     646                 :            : 
     647                 :          0 :         ret = write(dev->vduse_dev_fd, &resp, sizeof(resp));
     648         [ #  # ]:          0 :         if (ret != sizeof(resp)) {
     649                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to write response %s",
     650                 :            :                                 strerror(errno));
     651                 :          0 :                 return;
     652                 :            :         }
     653                 :            : 
     654         [ #  # ]:          0 :         if ((old_status ^ dev->status) & VIRTIO_DEVICE_STATUS_DRIVER_OK) {
     655         [ #  # ]:          0 :                 if (dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK) {
     656                 :            :                         /* Poll virtqueues ready states before starting device */
     657                 :          0 :                         ret = dev->vduse_features & RTE_BIT64(VDUSE_F_QUEUE_READY) ? 0
     658         [ #  # ]:          0 :                                 : vduse_wait_for_virtqueues_ready(dev);
     659         [ #  # ]:          0 :                         if (ret < 0) {
     660                 :          0 :                                 VHOST_CONFIG_LOG(dev->ifname, ERR,
     661                 :            :                                         "Failed to wait for virtqueues ready, aborting device start");
     662                 :          0 :                                 return;
     663                 :            :                         }
     664                 :          0 :                         vduse_device_start(dev, false);
     665                 :            :                 } else {
     666                 :          0 :                         vduse_device_stop(dev);
     667                 :            :                 }
     668                 :            :         }
     669                 :            : 
     670         [ #  # ]:          0 :         VHOST_CONFIG_LOG(dev->ifname, INFO, "Request %s (%u) handled successfully",
     671                 :            :                         vduse_req_id_to_str(req.type), req.type);
     672                 :            : }
     673                 :            : 
     674                 :            : static char vduse_reconnect_dir[PATH_MAX];
     675                 :            : static bool vduse_reconnect_path_set;
     676                 :            : 
     677                 :            : static int
     678                 :          0 : vduse_reconnect_path_init(void)
     679                 :            : {
     680                 :            :         const char *directory;
     681                 :            :         int ret;
     682                 :            : 
     683         [ #  # ]:          0 :         if (vduse_reconnect_path_set == true)
     684                 :            :                 return 0;
     685                 :            : 
     686                 :            :         /* from RuntimeDirectory= see systemd.exec */
     687                 :          0 :         directory = getenv("RUNTIME_DIRECTORY");
     688         [ #  # ]:          0 :         if (directory == NULL) {
     689                 :            :                 /*
     690                 :            :                  * Used standard convention defined in
     691                 :            :                  * XDG Base Directory Specification and
     692                 :            :                  * Filesystem Hierarchy Standard.
     693                 :            :                  */
     694         [ #  # ]:          0 :                 if (getuid() == 0)
     695                 :            :                         directory = "/var/run";
     696                 :            :                 else
     697         [ #  # ]:          0 :                         directory = getenv("XDG_RUNTIME_DIR") ? : "/tmp";
     698                 :            :         }
     699                 :            : 
     700                 :            :         ret = snprintf(vduse_reconnect_dir, sizeof(vduse_reconnect_dir), "%s/vduse",
     701                 :            :                         directory);
     702         [ #  # ]:          0 :         if (ret < 0 || ret == sizeof(vduse_reconnect_dir)) {
     703                 :          0 :                 VHOST_CONFIG_LOG("vduse", ERR, "Error creating VDUSE reconnect path name");
     704                 :          0 :                 return -1;
     705                 :            :         }
     706                 :            : 
     707                 :          0 :         ret = mkdir(vduse_reconnect_dir, 0700);
     708   [ #  #  #  # ]:          0 :         if (ret < 0 && errno != EEXIST) {
     709                 :          0 :                 VHOST_CONFIG_LOG("vduse", ERR, "Error creating '%s': %s",
     710                 :            :                                 vduse_reconnect_dir, strerror(errno));
     711                 :          0 :                 return -1;
     712                 :            :         }
     713                 :            : 
     714                 :          0 :         VHOST_CONFIG_LOG("vduse", INFO, "Created VDUSE reconnect directory in %s",
     715                 :            :                         vduse_reconnect_dir);
     716                 :            : 
     717                 :          0 :         vduse_reconnect_path_set = true;
     718                 :            : 
     719                 :          0 :         return 0;
     720                 :            : }
     721                 :            : 
     722                 :            : static int
     723                 :          0 : vduse_reconnect_log_map(struct virtio_net *dev, bool create)
     724                 :            : {
     725                 :            :         char reco_file[PATH_MAX];
     726                 :            :         int fd, ret;
     727                 :          0 :         const char *name = dev->ifname + strlen("/dev/vduse/");
     728                 :            : 
     729         [ #  # ]:          0 :         if (vduse_reconnect_path_init() < 0) {
     730                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to initialize reconnect path");
     731                 :          0 :                 return -1;
     732                 :            :         }
     733                 :            : 
     734                 :            :         ret = snprintf(reco_file, sizeof(reco_file), "%s/%s", vduse_reconnect_dir, name);
     735         [ #  # ]:          0 :         if (ret < 0 || ret == sizeof(reco_file)) {
     736                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to create vduse reconnect path name");
     737                 :          0 :                 return -1;
     738                 :            :         }
     739                 :            : 
     740         [ #  # ]:          0 :         if (create) {
     741                 :            :                 fd = open(reco_file, O_CREAT | O_EXCL | O_RDWR, 0600);
     742         [ #  # ]:          0 :                 if (fd < 0) {
     743         [ #  # ]:          0 :                         if (errno == EEXIST) {
     744                 :          0 :                                 VHOST_CONFIG_LOG(dev->ifname, ERR, "Reconnect file %s exists but not the device",
     745                 :            :                                                 reco_file);
     746                 :            :                         } else {
     747                 :          0 :                                 VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to open reconnect file %s (%s)",
     748                 :            :                                                 reco_file, strerror(errno));
     749                 :            :                         }
     750                 :          0 :                         return -1;
     751                 :            :                 }
     752                 :            : 
     753                 :          0 :                 ret = ftruncate(fd, sizeof(*dev->reconnect_log));
     754         [ #  # ]:          0 :                 if (ret < 0) {
     755                 :          0 :                         VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to truncate reconnect file %s (%s)",
     756                 :            :                                         reco_file, strerror(errno));
     757                 :          0 :                         goto out_close;
     758                 :            :                 }
     759                 :            :         } else {
     760                 :            :                 fd = open(reco_file, O_RDWR, 0600);
     761         [ #  # ]:          0 :                 if (fd < 0) {
     762         [ #  # ]:          0 :                         if (errno == ENOENT)
     763                 :          0 :                                 VHOST_CONFIG_LOG(dev->ifname, ERR, "Missing reconnect file (%s)", reco_file);
     764                 :            :                         else
     765                 :          0 :                                 VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to open reconnect file %s (%s)",
     766                 :            :                                                 reco_file, strerror(errno));
     767                 :          0 :                         return -1;
     768                 :            :                 }
     769                 :            :         }
     770                 :            : 
     771                 :          0 :         dev->reconnect_log = mmap(NULL, sizeof(*dev->reconnect_log), PROT_READ | PROT_WRITE,
     772                 :            :                                 MAP_SHARED, fd, 0);
     773         [ #  # ]:          0 :         if (dev->reconnect_log == MAP_FAILED) {
     774                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to mmap reconnect file %s (%s)",
     775                 :            :                                 reco_file, strerror(errno));
     776                 :            :                 ret = -1;
     777                 :          0 :                 goto out_close;
     778                 :            :         }
     779                 :            :         ret = 0;
     780                 :            : 
     781                 :          0 : out_close:
     782                 :          0 :         close(fd);
     783                 :            : 
     784                 :          0 :         return ret;
     785                 :            : }
     786                 :            : 
     787                 :            : static int
     788                 :          0 : vduse_reconnect_log_check(struct virtio_net *dev, uint64_t features, uint32_t total_queues)
     789                 :            : {
     790         [ #  # ]:          0 :         if (dev->reconnect_log->version != VHOST_RECONNECT_VERSION) {
     791                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
     792                 :            :                                 "Version mismatch between backend (0x%x) & reconnection file (0x%x)",
     793                 :            :                                 VHOST_RECONNECT_VERSION, dev->reconnect_log->version);
     794                 :          0 :                 return -1;
     795                 :            :         }
     796                 :            : 
     797         [ #  # ]:          0 :         if ((dev->reconnect_log->features & features) != dev->reconnect_log->features) {
     798                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
     799                 :            :                                 "Features mismatch between backend (0x%" PRIx64 ") & reconnection file (0x%" PRIx64 ")",
     800                 :            :                                 features, dev->reconnect_log->features);
     801                 :          0 :                 return -1;
     802                 :            :         }
     803                 :            : 
     804         [ #  # ]:          0 :         if (dev->reconnect_log->nr_vrings != total_queues) {
     805                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR,
     806                 :            :                                 "Queues number mismatch between backend (%u) and reconnection file (%u)",
     807                 :            :                                 total_queues, dev->reconnect_log->nr_vrings);
     808                 :          0 :                 return -1;
     809                 :            :         }
     810                 :            : 
     811                 :            :         return 0;
     812                 :            : }
     813                 :            : 
     814                 :            : static void
     815                 :          0 : vduse_reconnect_handler(int fd __rte_unused, void *arg, int *close)
     816                 :            : {
     817                 :            :         struct virtio_net *dev = arg;
     818                 :            : 
     819                 :          0 :         vduse_device_start(dev, true);
     820                 :            : 
     821                 :          0 :         *close = 1;
     822                 :          0 : }
     823                 :            : 
     824                 :            : static int
     825                 :          0 : vduse_reconnect_start_device(struct virtio_net *dev)
     826                 :            : {
     827                 :            :         int fd, ret;
     828                 :            : 
     829                 :            :         /*
     830                 :            :          * Make vduse_device_start() being executed in the same
     831                 :            :          * context for both reconnection and fresh startup.
     832                 :            :          */
     833                 :          0 :         fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
     834         [ #  # ]:          0 :         if (fd < 0) {
     835                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to create reconnect efd: %s",
     836                 :            :                                 strerror(errno));
     837                 :            :                 ret = -1;
     838                 :          0 :                 goto out_err;
     839                 :            :         }
     840                 :            : 
     841                 :          0 :         ret = fdset_add(vduse.fdset, fd, vduse_reconnect_handler, NULL, dev);
     842         [ #  # ]:          0 :         if (ret) {
     843                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to add reconnect efd %d to vduse fdset",
     844                 :            :                                 fd);
     845                 :          0 :                 goto out_err_close;
     846                 :            :         }
     847                 :            : 
     848                 :          0 :         ret = eventfd_write(fd, (eventfd_t)1);
     849         [ #  # ]:          0 :         if (ret < 0) {
     850                 :          0 :                 VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to write to reconnect eventfd");
     851                 :          0 :                 goto out_err_fdset;
     852                 :            :         }
     853                 :            : 
     854                 :            :         return 0;
     855                 :            : 
     856                 :            : out_err_fdset:
     857                 :          0 :         fdset_del(vduse.fdset, fd);
     858                 :          0 : out_err_close:
     859                 :          0 :         close(fd);
     860                 :            : out_err:
     861                 :            :         return ret;
     862                 :            : }
     863                 :            : 
     864                 :            : /* If some error occurs just continue as if the kernel exposed no features */
     865                 :            : static uint64_t
     866                 :          0 : vduse_device_get_vduse_features(int control_fd, const char *log_name)
     867                 :            : {
     868                 :            :         uint64_t vduse_kernel_features;
     869                 :            :         int ret;
     870                 :            : 
     871                 :          0 :         ret = ioctl(control_fd, VDUSE_GET_FEATURES, &vduse_kernel_features);
     872         [ #  # ]:          0 :         if (ret < 0) {
     873                 :          0 :                 VHOST_CONFIG_LOG(log_name, INFO,
     874                 :            :                         "Failed to get kernel VDUSE features, assuming not supported: %d(%s)",
     875                 :            :                         errno, strerror(errno));
     876                 :          0 :                 return 0;
     877                 :            :         }
     878                 :            : 
     879                 :          0 :         VHOST_CONFIG_LOG(log_name, DEBUG, "Setting vhost kernel features: %"PRIx64,
     880                 :            :                         vduse_kernel_features & supported_vduse_features);
     881                 :          0 :         return vduse_kernel_features & supported_vduse_features;
     882                 :            : }
     883                 :            : 
     884                 :            : int
     885                 :          0 : vduse_device_create(const char *path, bool compliant_ol_flags, bool extbuf, bool linearbuf)
     886                 :            : {
     887                 :            :         int control_fd, dev_fd, vid, ret;
     888                 :            :         uint32_t i, max_queue_pairs, total_queues;
     889                 :            :         struct virtio_net *dev;
     890                 :          0 :         struct virtio_net_config vnet_config = {{ 0 }};
     891                 :            :         uint64_t ver;
     892                 :          0 :         uint64_t features, vduse_features = 0;
     893                 :          0 :         const char *name = path + strlen("/dev/vduse/");
     894                 :            :         bool reconnect = false;
     895                 :            : 
     896         [ #  # ]:          0 :         if (vduse.fdset == NULL) {
     897                 :          0 :                 vduse.fdset = fdset_init("vduse-evt");
     898         [ #  # ]:          0 :                 if (vduse.fdset == NULL) {
     899                 :          0 :                         VHOST_CONFIG_LOG(path, ERR, "failed to init VDUSE fdset");
     900                 :          0 :                         return -1;
     901                 :            :                 }
     902                 :            :         }
     903                 :            : 
     904                 :            :         control_fd = open(VDUSE_CTRL_PATH, O_RDWR);
     905         [ #  # ]:          0 :         if (control_fd < 0) {
     906                 :          0 :                 VHOST_CONFIG_LOG(name, ERR, "Failed to open %s: %s",
     907                 :            :                                 VDUSE_CTRL_PATH, strerror(errno));
     908                 :          0 :                 return -1;
     909                 :            :         }
     910                 :            : 
     911         [ #  # ]:          0 :         if (ioctl(control_fd, VDUSE_GET_API_VERSION, &ver)) {
     912                 :          0 :                 VHOST_CONFIG_LOG(name, ERR, "Failed to get API version: %s", strerror(errno));
     913                 :            :                 ret = -1;
     914                 :          0 :                 goto out_ctrl_close;
     915                 :            :         }
     916                 :            : 
     917                 :          0 :         ver = RTE_MIN(ver, VHOST_VDUSE_API_VERSION);
     918                 :          0 :         VHOST_CONFIG_LOG(name, INFO, "Using VDUSE API version %" PRIu64 "", ver);
     919                 :            : 
     920         [ #  # ]:          0 :         if (ioctl(control_fd, VDUSE_SET_API_VERSION, &ver)) {
     921                 :          0 :                 VHOST_CONFIG_LOG(name, ERR, "Failed to set API version: %" PRIu64 ": %s",
     922                 :            :                                 ver, strerror(errno));
     923                 :            :                 ret = -1;
     924                 :          0 :                 goto out_ctrl_close;
     925                 :            :         }
     926                 :            : 
     927                 :          0 :         ret = rte_vhost_driver_get_features(path, &features);
     928         [ #  # ]:          0 :         if (ret < 0) {
     929                 :          0 :                 VHOST_CONFIG_LOG(name, ERR, "Failed to get backend features");
     930                 :          0 :                 goto out_ctrl_close;
     931                 :            :         }
     932                 :            : 
     933                 :          0 :         ret = rte_vhost_driver_get_queue_num(path, &max_queue_pairs);
     934         [ #  # ]:          0 :         if (ret < 0) {
     935                 :          0 :                 VHOST_CONFIG_LOG(name, ERR, "Failed to get max queue pairs");
     936                 :          0 :                 goto out_ctrl_close;
     937                 :            :         }
     938                 :            : 
     939                 :          0 :         VHOST_CONFIG_LOG(path, INFO, "VDUSE max queue pairs: %u", max_queue_pairs);
     940                 :          0 :         total_queues = max_queue_pairs * 2;
     941                 :            : 
     942         [ #  # ]:          0 :         if (max_queue_pairs == 1)
     943                 :          0 :                 features &= ~(RTE_BIT64(VIRTIO_NET_F_CTRL_VQ) | RTE_BIT64(VIRTIO_NET_F_MQ));
     944                 :            :         else
     945                 :          0 :                 total_queues += 1; /* Includes ctrl queue */
     946                 :            : 
     947                 :            :         dev_fd = open(path, O_RDWR);
     948         [ #  # ]:          0 :         if (dev_fd >= 0) {
     949                 :          0 :                 VHOST_CONFIG_LOG(name, INFO, "Device already exists, reconnecting...");
     950                 :            :                 reconnect = true;
     951         [ #  # ]:          0 :         } else if (errno == ENOENT) {
     952                 :            :                 struct vduse_dev_config *dev_config;
     953                 :            : 
     954                 :          0 :                 dev_config = malloc(offsetof(struct vduse_dev_config, config) +
     955                 :            :                                 sizeof(vnet_config));
     956         [ #  # ]:          0 :                 if (!dev_config) {
     957                 :          0 :                         VHOST_CONFIG_LOG(name, ERR, "Failed to allocate VDUSE config");
     958                 :            :                         ret = -1;
     959                 :          0 :                         goto out_ctrl_close;
     960                 :            :                 }
     961                 :            : 
     962                 :          0 :                 vnet_config.status = VIRTIO_NET_S_LINK_UP;
     963                 :          0 :                 vnet_config.max_virtqueue_pairs = max_queue_pairs;
     964                 :            :                 memset(dev_config, 0, sizeof(struct vduse_dev_config));
     965                 :            : 
     966                 :          0 :                 rte_strscpy(dev_config->name, name, VDUSE_NAME_MAX - 1);
     967                 :          0 :                 dev_config->device_id = VIRTIO_ID_NET;
     968                 :          0 :                 dev_config->vendor_id = 0;
     969                 :          0 :                 dev_config->features = features;
     970                 :          0 :                 dev_config->vq_num = total_queues;
     971                 :          0 :                 dev_config->vq_align = rte_mem_page_size();
     972         [ #  # ]:          0 :                 if (ver >= 1) {
     973                 :          0 :                         dev_config->ngroups = 2;
     974                 :          0 :                         dev_config->nas = 2;
     975                 :            :                 }
     976                 :            : 
     977                 :          0 :                 vduse_features = vduse_device_get_vduse_features(control_fd, name);
     978         [ #  # ]:          0 :                 if (vduse_features) {
     979                 :          0 :                         ret = ioctl(control_fd, VDUSE_SET_FEATURES, &vduse_features);
     980         [ #  # ]:          0 :                         if (ret < 0) {
     981                 :          0 :                                 VHOST_CONFIG_LOG(name, ERR, "Failed to set VDUSE features: %s",
     982                 :            :                                                 strerror(errno));
     983                 :          0 :                                 free(dev_config);
     984                 :          0 :                                 goto out_ctrl_close;
     985                 :            :                         }
     986                 :            :                 }
     987                 :            : 
     988                 :          0 :                 dev_config->config_size = sizeof(struct virtio_net_config);
     989                 :          0 :                 memcpy(dev_config->config, &vnet_config, sizeof(vnet_config));
     990                 :            : 
     991                 :          0 :                 ret = ioctl(control_fd, VDUSE_CREATE_DEV, dev_config);
     992                 :          0 :                 free(dev_config);
     993                 :            :                 dev_config = NULL;
     994         [ #  # ]:          0 :                 if (ret < 0) {
     995                 :          0 :                         VHOST_CONFIG_LOG(name, ERR, "Failed to create VDUSE device: %s",
     996                 :            :                                         strerror(errno));
     997                 :          0 :                         goto out_ctrl_close;
     998                 :            :                 }
     999                 :            : 
    1000                 :            :                 dev_fd = open(path, O_RDWR);
    1001         [ #  # ]:          0 :                 if (dev_fd < 0) {
    1002                 :          0 :                         VHOST_CONFIG_LOG(name, ERR, "Failed to open newly created device %s: %s",
    1003                 :            :                                         path, strerror(errno));
    1004                 :            :                         ret = -1;
    1005                 :          0 :                         goto out_ctrl_close;
    1006                 :            :                 }
    1007                 :            :         } else {
    1008                 :          0 :                 VHOST_CONFIG_LOG(name, ERR, "Failed to open device %s: %s",
    1009                 :            :                                 path, strerror(errno));
    1010                 :            :                 ret = -1;
    1011                 :          0 :                 goto out_ctrl_close;
    1012                 :            :         }
    1013                 :            : 
    1014                 :          0 :         ret = fcntl(dev_fd, F_SETFL, O_NONBLOCK);
    1015         [ #  # ]:          0 :         if (ret < 0) {
    1016                 :          0 :                 VHOST_CONFIG_LOG(name, ERR, "Failed to set chardev as non-blocking: %s",
    1017                 :            :                                 strerror(errno));
    1018                 :          0 :                 goto out_dev_close;
    1019                 :            :         }
    1020                 :            : 
    1021                 :          0 :         vid = vhost_new_device(&vduse_backend_ops);
    1022         [ #  # ]:          0 :         if (vid < 0) {
    1023                 :          0 :                 VHOST_CONFIG_LOG(name, ERR, "Failed to create new Vhost device");
    1024                 :            :                 ret = -1;
    1025                 :          0 :                 goto out_dev_close;
    1026                 :            :         }
    1027                 :            : 
    1028                 :            :         dev = get_device(vid);
    1029         [ #  # ]:          0 :         if (!dev) {
    1030                 :            :                 ret = -1;
    1031                 :          0 :                 goto out_dev_destroy;
    1032                 :            :         }
    1033                 :            : 
    1034                 :          0 :         strlcpy(dev->ifname, path, sizeof(dev->ifname));
    1035                 :          0 :         dev->vduse_ctrl_fd = control_fd;
    1036                 :          0 :         dev->vduse_dev_fd = dev_fd;
    1037                 :          0 :         dev->vduse_api_ver = ver;
    1038                 :          0 :         dev->vduse_features = vduse_features;
    1039                 :            : 
    1040                 :          0 :         ret = vduse_reconnect_log_map(dev, !reconnect);
    1041         [ #  # ]:          0 :         if (ret < 0)
    1042                 :          0 :                 goto out_dev_destroy;
    1043                 :            : 
    1044         [ #  # ]:          0 :         if (reconnect) {
    1045                 :          0 :                 ret = vduse_reconnect_log_check(dev, features, total_queues);
    1046         [ #  # ]:          0 :                 if (ret < 0)
    1047                 :          0 :                         goto out_log_unmap;
    1048                 :            : 
    1049                 :          0 :                 dev->status = dev->reconnect_log->status;
    1050                 :            :         } else {
    1051                 :          0 :                 dev->reconnect_log->version = VHOST_RECONNECT_VERSION;
    1052                 :          0 :                 dev->reconnect_log->nr_vrings = total_queues;
    1053                 :          0 :                 memcpy(&dev->reconnect_log->config, &vnet_config, sizeof(vnet_config));
    1054                 :            :         }
    1055                 :            : 
    1056                 :          0 :         vhost_setup_virtio_net(dev->vid, true, compliant_ol_flags, true, true);
    1057                 :            : 
    1058         [ #  # ]:          0 :         if (extbuf)
    1059                 :          0 :                 vhost_enable_extbuf(dev->vid);
    1060                 :            : 
    1061         [ #  # ]:          0 :         if (linearbuf)
    1062                 :          0 :                 vhost_enable_linearbuf(dev->vid);
    1063                 :            : 
    1064         [ #  # ]:          0 :         for (i = 0; i < total_queues; i++) {
    1065                 :          0 :                 struct vduse_vq_config vq_cfg = { 0 };
    1066                 :            :                 struct vhost_virtqueue *vq;
    1067                 :            : 
    1068                 :          0 :                 ret = alloc_vring_queue(dev, i);
    1069         [ #  # ]:          0 :                 if (ret) {
    1070                 :          0 :                         VHOST_CONFIG_LOG(name, ERR, "Failed to alloc vring %d metadata", i);
    1071                 :          0 :                         goto out_log_unmap;
    1072                 :            :                 }
    1073                 :            : 
    1074                 :          0 :                 vq = dev->virtqueue[i];
    1075                 :          0 :                 vq->reconnect_log = &dev->reconnect_log->vring[i];
    1076                 :            : 
    1077         [ #  # ]:          0 :                 if (i == max_queue_pairs * 2)
    1078                 :          0 :                         dev->cvq = vq;
    1079                 :            : 
    1080         [ #  # ]:          0 :                 if (reconnect)
    1081                 :          0 :                         continue;
    1082                 :            : 
    1083                 :          0 :                 vq_cfg.index = i;
    1084         [ #  # ]:          0 :                 vq_cfg.max_size = 1024;
    1085                 :          0 :                 vq_cfg.group = vduse_vq_to_group(dev, vq);
    1086                 :            : 
    1087                 :          0 :                 ret = ioctl(dev->vduse_dev_fd, VDUSE_VQ_SETUP, &vq_cfg);
    1088         [ #  # ]:          0 :                 if (ret) {
    1089                 :          0 :                         VHOST_CONFIG_LOG(name, ERR, "Failed to set-up VQ %d", i);
    1090                 :          0 :                         goto out_log_unmap;
    1091                 :            :                 }
    1092                 :            :         }
    1093                 :            : 
    1094                 :          0 :         ret = fdset_add(vduse.fdset, dev->vduse_dev_fd, vduse_events_handler, NULL, dev);
    1095         [ #  # ]:          0 :         if (ret) {
    1096                 :          0 :                 VHOST_CONFIG_LOG(name, ERR, "Failed to add fd %d to vduse fdset",
    1097                 :            :                                 dev->vduse_dev_fd);
    1098                 :          0 :                 goto out_log_unmap;
    1099                 :            :         }
    1100                 :            : 
    1101   [ #  #  #  # ]:          0 :         if (reconnect && dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK)  {
    1102                 :          0 :                 ret = vduse_reconnect_start_device(dev);
    1103         [ #  # ]:          0 :                 if (ret)
    1104                 :          0 :                         goto out_log_unmap;
    1105                 :            :         }
    1106                 :            : 
    1107                 :            :         return 0;
    1108                 :            : 
    1109                 :          0 : out_log_unmap:
    1110                 :          0 :         munmap(dev->reconnect_log, sizeof(*dev->reconnect_log));
    1111                 :          0 : out_dev_destroy:
    1112                 :          0 :         vhost_destroy_device(vid);
    1113                 :          0 : out_dev_close:
    1114                 :            :         if (dev_fd >= 0)
    1115                 :          0 :                 close(dev_fd);
    1116                 :          0 :         ioctl(control_fd, VDUSE_DESTROY_DEV, name);
    1117                 :          0 : out_ctrl_close:
    1118                 :          0 :         close(control_fd);
    1119                 :            : 
    1120                 :          0 :         return ret;
    1121                 :            : }
    1122                 :            : 
    1123                 :            : int
    1124                 :          0 : vduse_device_destroy(const char *path)
    1125                 :            : {
    1126                 :          0 :         const char *name = path + strlen("/dev/vduse/");
    1127                 :            :         struct virtio_net *dev;
    1128                 :            :         int vid, ret;
    1129                 :            : 
    1130         [ #  # ]:          0 :         for (vid = 0; vid < RTE_MAX_VHOST_DEVICE; vid++) {
    1131                 :          0 :                 dev = vhost_devices[vid];
    1132                 :            : 
    1133         [ #  # ]:          0 :                 if (dev == NULL)
    1134                 :          0 :                         continue;
    1135                 :            : 
    1136         [ #  # ]:          0 :                 if (!strcmp(path, dev->ifname))
    1137                 :            :                         break;
    1138                 :            :         }
    1139                 :            : 
    1140         [ #  # ]:          0 :         if (vid == RTE_MAX_VHOST_DEVICE)
    1141                 :            :                 return -1;
    1142                 :            : 
    1143         [ #  # ]:          0 :         if (dev->reconnect_log)
    1144                 :          0 :                 munmap(dev->reconnect_log, sizeof(*dev->reconnect_log));
    1145                 :            : 
    1146                 :          0 :         vduse_device_stop(dev);
    1147                 :            : 
    1148                 :          0 :         fdset_del(vduse.fdset, dev->vduse_dev_fd);
    1149                 :            : 
    1150         [ #  # ]:          0 :         if (dev->vduse_dev_fd >= 0) {
    1151                 :          0 :                 close(dev->vduse_dev_fd);
    1152                 :          0 :                 dev->vduse_dev_fd = -1;
    1153                 :            :         }
    1154                 :            : 
    1155         [ #  # ]:          0 :         if (dev->vduse_ctrl_fd >= 0) {
    1156                 :            :                 char reconnect_file[PATH_MAX];
    1157                 :            : 
    1158                 :          0 :                 ret = ioctl(dev->vduse_ctrl_fd, VDUSE_DESTROY_DEV, name);
    1159         [ #  # ]:          0 :                 if (ret) {
    1160                 :          0 :                         VHOST_CONFIG_LOG(name, ERR, "Failed to destroy VDUSE device: %s",
    1161                 :            :                                         strerror(errno));
    1162                 :            :                 } else {
    1163                 :            :                         /*
    1164                 :            :                          * VDUSE device was no more attached to the vDPA bus,
    1165                 :            :                          * so we can remove the reconnect file.
    1166                 :            :                          */
    1167                 :            :                         ret = snprintf(reconnect_file, sizeof(reconnect_file), "%s/%s",
    1168                 :            :                                         vduse_reconnect_dir, name);
    1169         [ #  # ]:          0 :                         if (ret < 0 || ret == sizeof(reconnect_file))
    1170                 :          0 :                                 VHOST_CONFIG_LOG(name, ERR,
    1171                 :            :                                                 "Failed to create vduse reconnect path name");
    1172                 :            :                         else
    1173                 :          0 :                                 unlink(reconnect_file);
    1174                 :            :                 }
    1175                 :            : 
    1176                 :          0 :                 close(dev->vduse_ctrl_fd);
    1177                 :          0 :                 dev->vduse_ctrl_fd = -1;
    1178                 :            :         }
    1179                 :            : 
    1180                 :          0 :         vhost_destroy_device(vid);
    1181                 :            : 
    1182                 :          0 :         return 0;
    1183                 :            : }

Generated by: LCOV version 1.14