From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 07DEB5A004E for ; Mon, 24 Jun 2024 05:02:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1719198132; bh=/qipA3+SFMCGMGzaT4qwUL4mu2KIj2JFkakTZpfZAsM=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=RXTEl2HwDom1jVwUXQmw0PfonwJIXHyfqrrxgLLoIT0qkHFQNBew7DJdLPORn5kSa 4f9Jmg3Zqxk2Sh0Cs/Mx8k1H1y33Qydzy8dYrnfA2cCe8QNRMeEKGKV6ZlV2PK1cg7 HI5N2p/I8ug6zwuAotLbUmmZk+TxeU2jX56WfGOSSRzwSSwjC2iRGYelQZVMlXrmXc ErbiGDY1hZ/pl3VNhFmmd96C1vQwzU1+sUhZuFX5vTVMiCWqE1vIuG2w851GFXnXHX 1Py2tMhSjjtqSdPsS1qkR4GQKb/mrq9Ta+zpPf/xzOKtpwh2MWpG6e5QKYIcr0Pxt0 Bdt4i90JDZNUw== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4W6t6X0rJpz4wqM; Mon, 24 Jun 2024 13:02:12 +1000 (AEST) Date: Mon, 24 Jun 2024 13:02:05 +1000 From: David Gibson To: Laurent Vivier Subject: Re: [PATCH 3/5] vhost-user: introduce vhost-user API Message-ID: References: <20240621145640.1914287-1-lvivier@redhat.com> <20240621145640.1914287-4-lvivier@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="NpuO+nFaI+ORL/6D" Content-Disposition: inline In-Reply-To: <20240621145640.1914287-4-lvivier@redhat.com> Message-ID-Hash: 77DL2367EOO3533X4DFPGFIKJLLPJV32 X-Message-ID-Hash: 77DL2367EOO3533X4DFPGFIKJLLPJV32 X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --NpuO+nFaI+ORL/6D Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Jun 21, 2024 at 04:56:38PM +0200, Laurent Vivier wrote: > Add vhost_user.c and vhost_user.h that define the functions needed > to implement vhost-user backend. >=20 > Signed-off-by: Laurent Vivier I'm a little confused by this patch. The commit message seems to suggest that like the previous patch it's basically just a code import =66rom qemu. However... [snip] > diff --git a/passt.c b/passt.c > index a5e2c5a8e151..9d21c545b9cf 100644 > --- a/passt.c > +++ b/passt.c > @@ -73,6 +73,8 @@ char *epoll_type_str[] =3D { > [EPOLL_TYPE_TAP_PASTA] =3D "/dev/net/tun device", > [EPOLL_TYPE_TAP_PASST] =3D "connected qemu socket", > [EPOLL_TYPE_TAP_LISTEN] =3D "listening qemu socket", > + [EPOLL_TYPE_VHOST_CMD] =3D "vhost-user command socket", > + [EPOLL_TYPE_VHOST_KICK] =3D "vhost-user kick socket", =2E.. we also have real changes to passt specific code. It's not very obvious to me what the boundaries of that are. > }; > static_assert(ARRAY_SIZE(epoll_type_str) =3D=3D EPOLL_NUM_TYPES, > "epoll_type_str[] doesn't match enum epoll_type"); > diff --git a/passt.h b/passt.h > index 46d073a2a6fd..af10d0bfe4ef 100644 > --- a/passt.h > +++ b/passt.h > @@ -22,6 +22,7 @@ union epoll_ref; > #include "fwd.h" > #include "tcp.h" > #include "udp.h" > +#include "vhost_user.h" > =20 > /** > * enum epoll_type - Different types of fds we poll over > @@ -51,6 +52,10 @@ enum epoll_type { > EPOLL_TYPE_TAP_PASST, > /* socket listening for qemu socket connections */ > EPOLL_TYPE_TAP_LISTEN, > + /* vhost-user command socket */ > + EPOLL_TYPE_VHOST_CMD, > + /* vhost-user kick event socket */ > + EPOLL_TYPE_VHOST_KICK, > =20 > EPOLL_NUM_TYPES, > }; > @@ -224,6 +229,7 @@ struct ip6_ctx { > * @no_map_gw: Don't map connections, untracked UDP to gateway to host > * @low_wmem: Low probed net.core.wmem_max > * @low_rmem: Low probed net.core.rmem_max > + * @vdev: vhost-user device > */ > struct ctx { > enum passt_modes mode; > @@ -288,6 +294,8 @@ struct ctx { > =20 > int low_wmem; > int low_rmem; > + > + struct VuDev vdev; > }; > =20 > void proto_update_l2_buf(const unsigned char *eth_d, > diff --git a/tap.c b/tap.c > index c9aeff19f177..be272d25b642 100644 > --- a/tap.c > +++ b/tap.c > @@ -977,7 +977,7 @@ void tap_add_packet(struct ctx *c, ssize_t l2len, cha= r *p) > * tap_sock_reset() - Handle closing or failure of connect AF_UNIX socket > * @c: Execution context > */ > -static void tap_sock_reset(struct ctx *c) > +void tap_sock_reset(struct ctx *c) > { > if (c->one_off) { > info("Client closed connection, exiting"); > @@ -1296,6 +1296,23 @@ static void tap_sock_tun_init(struct ctx *c) > epoll_ctl(c->epollfd, EPOLL_CTL_ADD, c->fd_tap, &ev); > } > =20 > +void tap_sock_update_buf(void *base, size_t size) > +{ > + int i; > + > + pool_tap4_storage.buf =3D base; > + pool_tap4_storage.buf_size =3D size; > + pool_tap6_storage.buf =3D base; > + pool_tap6_storage.buf_size =3D size; > + > + for (i =3D 0; i < TAP_SEQS; i++) { > + tap4_l4[i].p.buf =3D base; > + tap4_l4[i].p.buf_size =3D size; > + tap6_l4[i].p.buf =3D base; > + tap6_l4[i].p.buf_size =3D size; > + } > +} > + > /** > * tap_sock_init() - Create and set up AF_UNIX socket or tuntap file des= criptor > * @c: Execution context > diff --git a/tap.h b/tap.h > index d496bd0e4b99..3b2dde41ae8d 100644 > --- a/tap.h > +++ b/tap.h > @@ -69,6 +69,8 @@ void tap_handler_pasta(struct ctx *c, uint32_t events, > void tap_handler_passt(struct ctx *c, uint32_t events, > const struct timespec *now); > int tap_sock_unix_open(char *sock_path); > +void tap_sock_reset(struct ctx *c); > +void tap_sock_update_buf(void *base, size_t size); > void tap_sock_init(struct ctx *c); > void tap_flush_pools(void); > void tap_handler(struct ctx *c, const struct timespec *now); > diff --git a/vhost_user.c b/vhost_user.c > new file mode 100644 > index 000000000000..4ac0a3e53499 > --- /dev/null > +++ b/vhost_user.c > @@ -0,0 +1,1083 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > + > +/* some parts from QEMU subprojects/libvhost-user/libvhost-user.c */ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#include "util.h" > +#include "passt.h" > +#include "tap.h" > +#include "vhost_user.h" > + > +#define VHOST_USER_VERSION 1 > + > +/* cppcheck-suppress unusedFunction */ > +void vu_print_capabilities(void) > +{ > + printf("{\n"); > + printf(" \"type\": \"net\"\n"); > + printf("}\n"); > + exit(EXIT_SUCCESS); > +} > + > +static const char * > +vu_request_to_string(unsigned int req) > +{ > + if (req < VHOST_USER_MAX) { > +#define REQ(req) [req] =3D #req > + static const char * const vu_request_str[] =3D { > + REQ(VHOST_USER_NONE), > + REQ(VHOST_USER_GET_FEATURES), > + REQ(VHOST_USER_SET_FEATURES), > + REQ(VHOST_USER_SET_OWNER), > + REQ(VHOST_USER_RESET_OWNER), > + REQ(VHOST_USER_SET_MEM_TABLE), > + REQ(VHOST_USER_SET_LOG_BASE), > + REQ(VHOST_USER_SET_LOG_FD), > + REQ(VHOST_USER_SET_VRING_NUM), > + REQ(VHOST_USER_SET_VRING_ADDR), > + REQ(VHOST_USER_SET_VRING_BASE), > + REQ(VHOST_USER_GET_VRING_BASE), > + REQ(VHOST_USER_SET_VRING_KICK), > + REQ(VHOST_USER_SET_VRING_CALL), > + REQ(VHOST_USER_SET_VRING_ERR), > + REQ(VHOST_USER_GET_PROTOCOL_FEATURES), > + REQ(VHOST_USER_SET_PROTOCOL_FEATURES), > + REQ(VHOST_USER_GET_QUEUE_NUM), > + REQ(VHOST_USER_SET_VRING_ENABLE), > + REQ(VHOST_USER_SEND_RARP), > + REQ(VHOST_USER_NET_SET_MTU), > + REQ(VHOST_USER_SET_BACKEND_REQ_FD), > + REQ(VHOST_USER_IOTLB_MSG), > + REQ(VHOST_USER_SET_VRING_ENDIAN), > + REQ(VHOST_USER_GET_CONFIG), > + REQ(VHOST_USER_SET_CONFIG), > + REQ(VHOST_USER_POSTCOPY_ADVISE), > + REQ(VHOST_USER_POSTCOPY_LISTEN), > + REQ(VHOST_USER_POSTCOPY_END), > + REQ(VHOST_USER_GET_INFLIGHT_FD), > + REQ(VHOST_USER_SET_INFLIGHT_FD), > + REQ(VHOST_USER_GPU_SET_SOCKET), > + REQ(VHOST_USER_VRING_KICK), > + REQ(VHOST_USER_GET_MAX_MEM_SLOTS), > + REQ(VHOST_USER_ADD_MEM_REG), > + REQ(VHOST_USER_REM_MEM_REG), > + REQ(VHOST_USER_MAX), > + }; > +#undef REQ > + return vu_request_str[req]; > + } > + > + return "unknown"; > +} > + > +/* Translate qemu virtual address to our virtual address. */ Now that this code is not in qemu, it's not very clear what either of these "virtual addresses" is. > +static void *qva_to_va(VuDev *dev, uint64_t qemu_addr) > +{ > + unsigned int i; > + > + /* Find matching memory region. */ > + for (i =3D 0; i < dev->nregions; i++) { > + const VuDevRegion *r =3D &dev->regions[i]; > + > + if ((qemu_addr >=3D r->qva) && (qemu_addr < (r->qva + r->size))) { > + /* NOLINTNEXTLINE(performance-no-int-to-ptr) */ > + return (void *)(qemu_addr - r->qva + r->mmap_addr + > + r->mmap_offset); > + } > + } > + > + return NULL; > +} > + > +static void > +vmsg_close_fds(const VhostUserMsg *vmsg) > +{ > + int i; > + > + for (i =3D 0; i < vmsg->fd_num; i++) > + close(vmsg->fds[i]); > +} > + > +static void vu_remove_watch(VuDev *vdev, int fd) > +{ > + const struct ctx *c =3D (struct ctx *) ((char *)vdev - > + offsetof(struct ctx, vdev)); > + > + epoll_ctl(c->epollfd, EPOLL_CTL_DEL, fd, NULL); > +} > + > +/* Set reply payload.u64 and clear request flags and fd_num */ > +static void vmsg_set_reply_u64(struct VhostUserMsg *vmsg, uint64_t val) > +{ > + vmsg->hdr.flags =3D 0; /* defaults will be set by vu_send_reply() */ > + vmsg->hdr.size =3D sizeof(vmsg->payload.u64); > + vmsg->payload.u64 =3D val; > + vmsg->fd_num =3D 0; > +} > + > +static ssize_t vu_message_read_default(VuDev *dev, int conn_fd, struct V= hostUserMsg *vmsg) > +{ > + char control[CMSG_SPACE(VHOST_MEMORY_BASELINE_NREGIONS * > + sizeof(int))] =3D { 0 }; > + struct iovec iov =3D { > + .iov_base =3D (char *)vmsg, > + .iov_len =3D VHOST_USER_HDR_SIZE, > + }; > + struct msghdr msg =3D { > + .msg_iov =3D &iov, > + .msg_iovlen =3D 1, > + .msg_control =3D control, > + .msg_controllen =3D sizeof(control), > + }; > + size_t fd_size; > + struct cmsghdr *cmsg; > + ssize_t ret, sz_payload; > + > + ret =3D recvmsg(conn_fd, &msg, MSG_DONTWAIT); > + if (ret < 0) { > + if (errno =3D=3D EINTR || errno =3D=3D EAGAIN || errno =3D=3D EWOULDBL= OCK) > + return 0; > + vu_panic(dev, "Error while recvmsg: %s", strerror(errno)); > + goto out; > + } > + > + vmsg->fd_num =3D 0; > + for (cmsg =3D CMSG_FIRSTHDR(&msg); cmsg !=3D NULL; > + cmsg =3D CMSG_NXTHDR(&msg, cmsg)) { > + if (cmsg->cmsg_level =3D=3D SOL_SOCKET && > + cmsg->cmsg_type =3D=3D SCM_RIGHTS) { > + fd_size =3D cmsg->cmsg_len - CMSG_LEN(0); > + vmsg->fd_num =3D fd_size / sizeof(int); > + memcpy(vmsg->fds, CMSG_DATA(cmsg), fd_size); > + break; > + } > + } > + > + sz_payload =3D vmsg->hdr.size; > + if ((size_t)sz_payload > sizeof(vmsg->payload)) { > + vu_panic(dev, > + "Error: too big message request: %d, size: vmsg->size: %zd, " > + "while sizeof(vmsg->payload) =3D %zu", > + vmsg->hdr.request, sz_payload, sizeof(vmsg->payload)); > + goto out; > + } > + > + if (sz_payload) { > + do { > + ret =3D recv(conn_fd, &vmsg->payload, sz_payload, 0); > + } while (ret < 0 && (errno =3D=3D EINTR || errno =3D=3D EAGAIN)); > + > + if (ret < sz_payload) { > + vu_panic(dev, "Error while reading: %s", strerror(errno)); > + goto out; > + } > + } > + > + return 1; > +out: > + vmsg_close_fds(vmsg); > + > + return -ECONNRESET; > +} > + > +static int vu_message_write(VuDev *dev, int conn_fd, struct VhostUserMsg= *vmsg) > +{ > + int rc; > + const uint8_t *p =3D (uint8_t *)vmsg; > + char control[CMSG_SPACE(VHOST_MEMORY_BASELINE_NREGIONS * sizeof(int))] = =3D { 0 }; > + struct iovec iov =3D { > + .iov_base =3D (char *)vmsg, > + .iov_len =3D VHOST_USER_HDR_SIZE, > + }; > + struct msghdr msg =3D { > + .msg_iov =3D &iov, > + .msg_iovlen =3D 1, > + .msg_control =3D control, > + }; > + > + memset(control, 0, sizeof(control)); > + assert(vmsg->fd_num <=3D VHOST_MEMORY_BASELINE_NREGIONS); > + if (vmsg->fd_num > 0) { > + size_t fdsize =3D vmsg->fd_num * sizeof(int); > + struct cmsghdr *cmsg; > + > + msg.msg_controllen =3D CMSG_SPACE(fdsize); > + cmsg =3D CMSG_FIRSTHDR(&msg); > + cmsg->cmsg_len =3D CMSG_LEN(fdsize); > + cmsg->cmsg_level =3D SOL_SOCKET; > + cmsg->cmsg_type =3D SCM_RIGHTS; > + memcpy(CMSG_DATA(cmsg), vmsg->fds, fdsize); > + } else { > + msg.msg_controllen =3D 0; > + } > + > + do { > + rc =3D sendmsg(conn_fd, &msg, 0); > + } while (rc < 0 && (errno =3D=3D EINTR || errno =3D=3D EAGAIN)); > + > + if (vmsg->hdr.size) { > + do { > + if (vmsg->data) > + rc =3D write(conn_fd, vmsg->data, vmsg->hdr.size); > + else > + rc =3D write(conn_fd, p + VHOST_USER_HDR_SIZE, vmsg->hdr.size); > + } while (rc < 0 && (errno =3D=3D EINTR || errno =3D=3D EAGAIN)); > + } > + > + if (rc <=3D 0) { > + vu_panic(dev, "Error while writing: %s", strerror(errno)); > + return false; > + } > + > + return true; > +} > + > +static int vu_send_reply(VuDev *dev, int conn_fd, struct VhostUserMsg *m= sg) > +{ > + msg->hdr.flags &=3D ~VHOST_USER_VERSION_MASK; > + msg->hdr.flags |=3D VHOST_USER_VERSION; > + msg->hdr.flags |=3D VHOST_USER_REPLY_MASK; > + > + return vu_message_write(dev, conn_fd, msg); > +} > + > +static bool vu_get_features_exec(struct VhostUserMsg *msg) > +{ > + uint64_t features =3D > + 1ULL << VIRTIO_F_VERSION_1 | > + 1ULL << VIRTIO_NET_F_MRG_RXBUF | > + 1ULL << VHOST_USER_F_PROTOCOL_FEATURES; > + > + vmsg_set_reply_u64(msg, features); > + > + debug("Sending back to guest u64: 0x%016"PRIx64, msg->payload.u64); > + > + return true; > +} > + > +static void > +vu_set_enable_all_rings(VuDev *vdev, bool enabled) > +{ > + uint16_t i; > + > + for (i =3D 0; i < VHOST_USER_MAX_QUEUES; i++) > + vdev->vq[i].enable =3D enabled; > +} > + > +static bool > +vu_set_features_exec(VuDev *vdev, struct VhostUserMsg *msg) > +{ > + debug("u64: 0x%016"PRIx64, msg->payload.u64); > + > + vdev->features =3D msg->payload.u64; > + if (!vu_has_feature(vdev, VIRTIO_F_VERSION_1)) { > + /* > + * We only support devices conforming to VIRTIO 1.0 or > + * later > + */ > + vu_panic(vdev, "virtio legacy devices aren't supported by passt"); > + return false; > + } > + > + if (!vu_has_feature(vdev, VHOST_USER_F_PROTOCOL_FEATURES)) > + vu_set_enable_all_rings(vdev, true); > + > + /* virtio-net features */ > + > + if (vu_has_feature(vdev, VIRTIO_F_VERSION_1) || > + vu_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) { > + vdev->hdrlen =3D sizeof(struct virtio_net_hdr_mrg_rxbuf); > + } else { > + vdev->hdrlen =3D sizeof(struct virtio_net_hdr); > + } > + > + return false; > +} > + > +static bool > +vu_set_owner_exec(void) > +{ > + return false; > +} > + > +static bool map_ring(VuDev *vdev, VuVirtq *vq) > +{ > + vq->vring.desc =3D qva_to_va(vdev, vq->vra.desc_user_addr); > + vq->vring.used =3D qva_to_va(vdev, vq->vra.used_user_addr); > + vq->vring.avail =3D qva_to_va(vdev, vq->vra.avail_user_addr); > + > + debug("Setting virtq addresses:"); > + debug(" vring_desc at %p", (void *)vq->vring.desc); > + debug(" vring_used at %p", (void *)vq->vring.used); > + debug(" vring_avail at %p", (void *)vq->vring.avail); > + > + return !(vq->vring.desc && vq->vring.used && vq->vring.avail); > +} > + > +/* cppcheck-suppress unusedFunction */ > +int vu_packet_check_range(void *buf, size_t offset, size_t len, const ch= ar *start, > + const char *func, int line) > +{ > + VuDevRegion *dev_region; > + > + for (dev_region =3D buf; dev_region->mmap_addr; dev_region++) { > + /* NOLINTNEXTLINE(performance-no-int-to-ptr) */ > + char *m =3D (char *)dev_region->mmap_addr; > + > + if (m <=3D start && > + start + offset + len < m + dev_region->mmap_offset + > + dev_region->size) > + return 0; > + } > + if (func) > + trace("cannot find region, %s:%i", func, line); > + > + return -1; > +} > + > +/* > + * #syscalls:passt mmap munmap > + */ > + > +static bool vu_set_mem_table_exec(VuDev *vdev, > + struct VhostUserMsg *msg) > +{ > + unsigned int i; > + struct VhostUserMemory m =3D msg->payload.memory, *memory =3D &m; > + > + for (i =3D 0; i < vdev->nregions; i++) { > + VuDevRegion *r =3D &vdev->regions[i]; > + /* NOLINTNEXTLINE(performance-no-int-to-ptr) */ > + void *mm =3D (void *)r->mmap_addr; > + > + if (mm) > + munmap(mm, r->size + r->mmap_offset); > + } > + vdev->nregions =3D memory->nregions; > + > + debug("Nregions: %u", memory->nregions); > + for (i =3D 0; i < vdev->nregions; i++) { > + void *mmap_addr; > + VhostUserMemory_region *msg_region =3D &memory->regions[i]; > + VuDevRegion *dev_region =3D &vdev->regions[i]; > + > + debug("Region %d", i); > + debug(" guest_phys_addr: 0x%016"PRIx64, > + msg_region->guest_phys_addr); > + debug(" memory_size: 0x%016"PRIx64, > + msg_region->memory_size); > + debug(" userspace_addr 0x%016"PRIx64, > + msg_region->userspace_addr); > + debug(" mmap_offset 0x%016"PRIx64, > + msg_region->mmap_offset); > + > + dev_region->gpa =3D msg_region->guest_phys_addr; > + dev_region->size =3D msg_region->memory_size; > + dev_region->qva =3D msg_region->userspace_addr; > + dev_region->mmap_offset =3D msg_region->mmap_offset; > + > + /* We don't use offset argument of mmap() since the > + * mapped address has to be page aligned, and we use huge > + * pages. > + */ > + mmap_addr =3D mmap(0, dev_region->size + dev_region->mmap_offset, > + PROT_READ | PROT_WRITE, MAP_SHARED | MAP_NORESERVE, > + msg->fds[i], 0); > + > + if (mmap_addr =3D=3D MAP_FAILED) { > + vu_panic(vdev, "region mmap error: %s", strerror(errno)); > + } else { > + dev_region->mmap_addr =3D (uint64_t)(uintptr_t)mmap_addr; > + debug(" mmap_addr: 0x%016"PRIx64, > + dev_region->mmap_addr); > + } > + > + close(msg->fds[i]); > + } > + > + for (i =3D 0; i < VHOST_USER_MAX_QUEUES; i++) { > + if (vdev->vq[i].vring.desc) { > + if (map_ring(vdev, &vdev->vq[i])) > + vu_panic(vdev, "remapping queue %d during setmemtable", i); > + } > + } > + > + /* XXX */ > + ASSERT(vdev->nregions < VHOST_USER_MAX_RAM_SLOTS - 1); > + vdev->regions[vdev->nregions].mmap_addr =3D 0; /* mark EOF for vu_packe= t_check_range() */ > + > + tap_sock_update_buf(vdev->regions, 0); > + > + return false; > +} > + > +static bool vu_set_vring_num_exec(VuDev *vdev, > + struct VhostUserMsg *msg) > +{ > + unsigned int idx =3D msg->payload.state.index; > + unsigned int num =3D msg->payload.state.num; > + > + debug("State.index: %u", idx); > + debug("State.num: %u", num); > + vdev->vq[idx].vring.num =3D num; > + > + return false; > +} > + > +static bool vu_set_vring_addr_exec(VuDev *vdev, > + struct VhostUserMsg *msg) > +{ > + struct vhost_vring_addr addr =3D msg->payload.addr, *vra =3D &addr; > + VuVirtq *vq =3D &vdev->vq[vra->index]; > + > + debug("vhost_vring_addr:"); > + debug(" index: %d", vra->index); > + debug(" flags: %d", vra->flags); > + debug(" desc_user_addr: 0x%016" PRIx64, (uint64_t)vra->desc_user_a= ddr); > + debug(" used_user_addr: 0x%016" PRIx64, (uint64_t)vra->used_user_a= ddr); > + debug(" avail_user_addr: 0x%016" PRIx64, (uint64_t)vra->avail_user_= addr); > + debug(" log_guest_addr: 0x%016" PRIx64, (uint64_t)vra->log_guest_a= ddr); > + > + vq->vra =3D *vra; > + vq->vring.flags =3D vra->flags; > + vq->vring.log_guest_addr =3D vra->log_guest_addr; > + > + if (map_ring(vdev, vq)) { > + vu_panic(vdev, "Invalid vring_addr message"); > + return false; > + } > + > + vq->used_idx =3D le16toh(vq->vring.used->idx); > + > + if (vq->last_avail_idx !=3D vq->used_idx) { > + debug("Last avail index !=3D used index: %u !=3D %u", > + vq->last_avail_idx, vq->used_idx); > + } > + > + return false; > +} > + > +static bool vu_set_vring_base_exec(VuDev *vdev, > + struct VhostUserMsg *msg) > +{ > + unsigned int idx =3D msg->payload.state.index; > + unsigned int num =3D msg->payload.state.num; > + > + debug("State.index: %u", idx); > + debug("State.num: %u", num); > + vdev->vq[idx].shadow_avail_idx =3D vdev->vq[idx].last_avail_idx =3D num; > + > + return false; > +} > + > +static bool vu_get_vring_base_exec(VuDev *vdev, > + struct VhostUserMsg *msg) > +{ > + unsigned int idx =3D msg->payload.state.index; > + > + debug("State.index: %u", idx); > + msg->payload.state.num =3D vdev->vq[idx].last_avail_idx; > + msg->hdr.size =3D sizeof(msg->payload.state); > + > + vdev->vq[idx].started =3D false; > + > + if (vdev->vq[idx].call_fd !=3D -1) { > + close(vdev->vq[idx].call_fd); > + vdev->vq[idx].call_fd =3D -1; > + } > + if (vdev->vq[idx].kick_fd !=3D -1) { > + vu_remove_watch(vdev, vdev->vq[idx].kick_fd); > + close(vdev->vq[idx].kick_fd); > + vdev->vq[idx].kick_fd =3D -1; > + } > + > + return true; > +} > + > +static void vu_set_watch(VuDev *vdev, int fd) > +{ > + const struct ctx *c =3D (struct ctx *) > + ((char *)vdev - offsetof(struct ctx, vdev)); > + union epoll_ref ref =3D { .type =3D EPOLL_TYPE_VHOST_KICK, .fd =3D fd }; > + struct epoll_event ev =3D { 0 }; > + > + ev.data.u64 =3D ref.u64; > + ev.events =3D EPOLLIN; > + epoll_ctl(c->epollfd, EPOLL_CTL_ADD, fd, &ev); > +} > + > +static int vu_wait_queue(const VuVirtq *vq) > +{ > + eventfd_t kick_data; > + ssize_t rc; > + int status; > + > + /* wait the kernel to put new entries in the queue */ > + > + status =3D fcntl(vq->kick_fd, F_GETFL); > + if (status =3D=3D -1) > + return -1; > + > + fcntl(vq->kick_fd, F_SETFL, status & ~O_NONBLOCK); > + rc =3D eventfd_read(vq->kick_fd, &kick_data); > + fcntl(vq->kick_fd, F_SETFL, status); > + if (rc =3D=3D -1) > + return -1; > + > + return 0; > +} > + > +/* cppcheck-suppress unusedFunction */ > +int vu_send(const struct ctx *c, const void *buf, size_t size) > +{ > + VuDev *vdev =3D (VuDev *)&c->vdev; > + size_t hdrlen =3D vdev->hdrlen; > + VuVirtq *vq =3D &vdev->vq[VHOST_USER_RX_QUEUE]; > + VuVirtqElement elem[VIRTQUEUE_MAX_SIZE]; > + struct iovec in_sg[VIRTQUEUE_MAX_SIZE]; > + size_t lens[VIRTQUEUE_MAX_SIZE]; > + size_t offset; > + int i, j; > + __virtio16 *num_buffers_ptr; > + int in_sg_count; > + > + debug("vu_send size %zu hdrlen %zu", size, hdrlen); > + > + if (!vu_queue_enabled(vq) || !vu_queue_started(vq)) { > + err("Got packet, but no available descriptors on RX virtq."); > + return 0; > + } > + > + offset =3D 0; > + i =3D 0; > + num_buffers_ptr =3D NULL; > + in_sg_count =3D 0; > + while (offset < size) { > + size_t len; > + int total; > + int ret; > + > + total =3D 0; > + > + if (i =3D=3D ARRAY_SIZE(elem) || > + in_sg_count =3D=3D ARRAY_SIZE(in_sg)) { > + err("virtio-net unexpected long buffer chain"); > + goto err; > + } > + > + elem[i].out_num =3D 0; > + elem[i].out_sg =3D NULL; > + elem[i].in_num =3D ARRAY_SIZE(in_sg) - in_sg_count; > + elem[i].in_sg =3D &in_sg[in_sg_count]; > + > + ret =3D vu_queue_pop(vdev, vq, &elem[i]); > + if (ret < 0) { > + if (!vdev->broken) { > + if (vu_wait_queue(vq) !=3D -1) > + continue; > + } > + if (i) { > + err("virtio-net unexpected empty queue: " > + "i %d mergeable %d offset %zd, size %zd, " > + "features 0x%" PRIx64, > + i, vu_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF), > + offset, size, vdev->features); > + } > + offset =3D -1; > + goto err; > + } > + in_sg_count +=3D elem[i].in_num; > + > + if (elem[i].in_num < 1) { > + err("virtio-net receive queue contains no in buffers"); > + vu_queue_detach_element(vdev, vq, elem[i].index, 0); > + offset =3D -1; > + goto err; > + } > + > + if (i =3D=3D 0) { > + struct virtio_net_hdr hdr =3D { > + .flags =3D VIRTIO_NET_HDR_F_DATA_VALID, > + .gso_type =3D VIRTIO_NET_HDR_GSO_NONE, > + }; > + > + ASSERT(offset =3D=3D 0); > + ASSERT(elem[i].in_sg[0].iov_len >=3D hdrlen); > + > + len =3D iov_from_buf(elem[i].in_sg, elem[i].in_num, 0, > + &hdr, sizeof(hdr)); > + > + num_buffers_ptr =3D (__virtio16 *)((char *)elem[i].in_sg[0].iov_base + > + len); > + > + total +=3D hdrlen; > + } > + > + len =3D iov_from_buf(elem[i].in_sg, elem[i].in_num, total, (char *)buf= + offset, > + size - offset); > + > + total +=3D len; > + offset +=3D len; > + > + /* If buffers can't be merged, at this point we > + * must have consumed the complete packet. > + * Otherwise, drop it. > + */ > + if (!vu_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) && offset < size) { > + vu_queue_unpop(vdev, vq, elem[i].index, total); > + goto err; > + } > + > + lens[i] =3D total; > + i++; > + } > + > + if (num_buffers_ptr && vu_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) > + *num_buffers_ptr =3D htole16(i); > + > + for (j =3D 0; j < i; j++) { > + debug("filling total %zd idx %d", lens[j], j); > + vu_queue_fill(vdev, vq, &elem[j], lens[j], j); > + } > + > + vu_queue_flush(vdev, vq, i); > + vu_queue_notify(vdev, vq); > + > + debug("sent %zu", offset); > + > + return offset; > +err: > + for (j =3D 0; j < i; j++) > + vu_queue_detach_element(vdev, vq, elem[j].index, lens[j]); > + > + return offset; > +} > + > +static void vu_handle_tx(VuDev *vdev, int index) > +{ > + struct ctx *c =3D (struct ctx *) ((char *)vdev - offsetof(struct ctx, v= dev)); > + VuVirtq *vq =3D &vdev->vq[index]; > + int hdrlen =3D vdev->hdrlen; > + struct timespec now; > + VuVirtqElement elem[VIRTQUEUE_MAX_SIZE]; > + struct iovec out_sg[VIRTQUEUE_MAX_SIZE]; > + int out_sg_count; > + > + int count; > + > + if (index % 2 !=3D VHOST_USER_TX_QUEUE) { > + debug("index %d is not an TX queue", index); > + return; > + } > + > + clock_gettime(CLOCK_MONOTONIC, &now); > + > + tap_flush_pools(); > + > + count =3D 0; > + out_sg_count =3D 0; > + while (1) { > + int ret; > + > + ASSERT(index =3D=3D VHOST_USER_TX_QUEUE); > + > + elem[count].out_num =3D 1; > + elem[count].out_sg =3D &out_sg[out_sg_count]; > + elem[count].in_num =3D 0; > + elem[count].in_sg =3D NULL; > + ret =3D vu_queue_pop(vdev, vq, &elem[count]); > + if (ret < 0) > + break; > + out_sg_count +=3D elem[count].out_num; > + > + if (elem[count].out_num < 1) { > + debug("virtio-net header not in first element"); > + break; > + } > + ASSERT(elem[count].out_num =3D=3D 1); > + > + tap_add_packet(c, elem[count].out_sg[0].iov_len - hdrlen, > + (char *)elem[count].out_sg[0].iov_base + hdrlen); > + count++; > + } > + tap_handler(c, &now); > + > + if (count) { > + int i; > + > + for (i =3D 0; i < count; i++) > + vu_queue_fill(vdev, vq, &elem[i], 0, i); > + vu_queue_flush(vdev, vq, count); > + vu_queue_notify(vdev, vq); > + } > +} > + > +/* cppcheck-suppress unusedFunction */ > +void vu_kick_cb(struct ctx *c, union epoll_ref ref) > +{ > + VuDev *vdev =3D &c->vdev; > + eventfd_t kick_data; > + ssize_t rc; > + int idx; > + > + for (idx =3D 0; idx < VHOST_USER_MAX_QUEUES; idx++) > + if (c->vdev.vq[idx].kick_fd =3D=3D ref.fd) > + break; > + > + if (idx =3D=3D VHOST_USER_MAX_QUEUES) > + return; > + > + rc =3D eventfd_read(ref.fd, &kick_data); > + if (rc =3D=3D -1) { > + vu_panic(vdev, "kick eventfd_read(): %s", strerror(errno)); > + vu_remove_watch(vdev, ref.fd); > + } else { > + debug("Got kick_data: %016"PRIx64" idx:%d", > + kick_data, idx); > + if (idx % 2 =3D=3D VHOST_USER_TX_QUEUE) > + vu_handle_tx(vdev, idx); > + } > +} > + > +static bool vu_check_queue_msg_file(VuDev *vdev, struct VhostUserMsg *ms= g) > +{ > + int idx =3D msg->payload.u64 & VHOST_USER_VRING_IDX_MASK; > + bool nofd =3D msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK; > + > + if (idx >=3D VHOST_USER_MAX_QUEUES) { > + vmsg_close_fds(msg); > + vu_panic(vdev, "Invalid queue index: %u", idx); > + return false; > + } > + > + if (nofd) { > + vmsg_close_fds(msg); > + return true; > + } > + > + if (msg->fd_num !=3D 1) { > + vmsg_close_fds(msg); > + vu_panic(vdev, "Invalid fds in request: %d", msg->hdr.request); > + return false; > + } > + > + return true; > +} > + > +static bool vu_set_vring_kick_exec(VuDev *vdev, > + struct VhostUserMsg *msg) > +{ > + int idx =3D msg->payload.u64 & VHOST_USER_VRING_IDX_MASK; > + bool nofd =3D msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK; > + > + debug("u64: 0x%016"PRIx64, msg->payload.u64); > + > + if (!vu_check_queue_msg_file(vdev, msg)) > + return false; > + > + if (vdev->vq[idx].kick_fd !=3D -1) { > + vu_remove_watch(vdev, vdev->vq[idx].kick_fd); > + close(vdev->vq[idx].kick_fd); > + vdev->vq[idx].kick_fd =3D -1; > + } > + > + /* cppcheck-suppress redundantAssignment */ > + vdev->vq[idx].kick_fd =3D nofd ? -1 : msg->fds[0]; > + debug("Got kick_fd: %d for vq: %d", vdev->vq[idx].kick_fd, idx); > + > + vdev->vq[idx].started =3D true; > + > + if (vdev->vq[idx].kick_fd !=3D -1 && idx % 2 =3D=3D VHOST_USER_TX_QUEUE= ) { > + vu_set_watch(vdev, vdev->vq[idx].kick_fd); > + debug("Waiting for kicks on fd: %d for vq: %d", > + vdev->vq[idx].kick_fd, idx); > + } > + > + return false; > +} > + > +static bool vu_set_vring_call_exec(VuDev *vdev, > + struct VhostUserMsg *msg) > +{ > + int idx =3D msg->payload.u64 & VHOST_USER_VRING_IDX_MASK; > + bool nofd =3D msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK; > + > + debug("u64: 0x%016"PRIx64, msg->payload.u64); > + > + if (!vu_check_queue_msg_file(vdev, msg)) > + return false; > + > + if (vdev->vq[idx].call_fd !=3D -1) { > + close(vdev->vq[idx].call_fd); > + vdev->vq[idx].call_fd =3D -1; > + } > + > + /* cppcheck-suppress redundantAssignment */ > + vdev->vq[idx].call_fd =3D nofd ? -1 : msg->fds[0]; > + > + /* in case of I/O hang after reconnecting */ > + if (vdev->vq[idx].call_fd !=3D -1) > + eventfd_write(msg->fds[0], 1); > + > + debug("Got call_fd: %d for vq: %d", vdev->vq[idx].call_fd, idx); > + > + return false; > +} > + > +static bool vu_set_vring_err_exec(VuDev *vdev, > + struct VhostUserMsg *msg) > +{ > + int idx =3D msg->payload.u64 & VHOST_USER_VRING_IDX_MASK; > + bool nofd =3D msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK; > + > + debug("u64: 0x%016"PRIx64, msg->payload.u64); > + > + if (!vu_check_queue_msg_file(vdev, msg)) > + return false; > + > + if (vdev->vq[idx].err_fd !=3D -1) { > + close(vdev->vq[idx].err_fd); > + vdev->vq[idx].err_fd =3D -1; > + } > + > + /* cppcheck-suppress redundantAssignment */ > + vdev->vq[idx].err_fd =3D nofd ? -1 : msg->fds[0]; > + > + return false; > +} > + > +static bool vu_get_protocol_features_exec(struct VhostUserMsg *msg) > +{ > + uint64_t features =3D 1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK; > + > + vmsg_set_reply_u64(msg, features); > + > + return true; > +} > + > +static bool vu_set_protocol_features_exec(VuDev *vdev, struct VhostUserM= sg *msg) > +{ > + uint64_t features =3D msg->payload.u64; > + > + debug("u64: 0x%016"PRIx64, features); > + > + vdev->protocol_features =3D msg->payload.u64; > + > + if (vu_has_protocol_feature(vdev, > + VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS) && > + (!vu_has_protocol_feature(vdev, VHOST_USER_PROTOCOL_F_BACKEND_REQ) = || > + !vu_has_protocol_feature(vdev, VHOST_USER_PROTOCOL_F_REPLY_ACK))) { > + /* > + * The use case for using messages for kick/call is simulation, to make > + * the kick and call synchronous. To actually get that behaviour, both > + * of the other features are required. > + * Theoretically, one could use only kick messages, or do them without > + * having F_REPLY_ACK, but too many (possibly pending) messages on the > + * socket will eventually cause the master to hang, to avoid this in > + * scenarios where not desired enforce that the settings are in a way > + * that actually enables the simulation case. > + */ > + vu_panic(vdev, > + "F_IN_BAND_NOTIFICATIONS requires F_BACKEND_REQ && F_REPLY_ACK"); > + return false; > + } > + > + return false; > +} > + > + > +static bool vu_get_queue_num_exec(struct VhostUserMsg *msg) > +{ > + vmsg_set_reply_u64(msg, VHOST_USER_MAX_QUEUES); > + return true; > +} > + > +static bool vu_set_vring_enable_exec(VuDev *vdev, struct VhostUserMsg *m= sg) > +{ > + unsigned int idx =3D msg->payload.state.index; > + unsigned int enable =3D msg->payload.state.num; > + > + debug("State.index: %u", idx); > + debug("State.enable: %u", enable); > + > + if (idx >=3D VHOST_USER_MAX_QUEUES) { > + vu_panic(vdev, "Invalid vring_enable index: %u", idx); > + return false; > + } > + > + vdev->vq[idx].enable =3D enable; > + return false; > +} > + > +/* cppcheck-suppress unusedFunction */ > +void vu_init(struct ctx *c) > +{ > + int i; > + > + c->vdev.hdrlen =3D 0; > + for (i =3D 0; i < VHOST_USER_MAX_QUEUES; i++) > + c->vdev.vq[i] =3D (VuVirtq){ > + .call_fd =3D -1, > + .kick_fd =3D -1, > + .err_fd =3D -1, > + .notification =3D true, > + }; > +} > + > +static void vu_cleanup(VuDev *vdev) > +{ > + unsigned int i; > + > + for (i =3D 0; i < VHOST_USER_MAX_QUEUES; i++) { > + VuVirtq *vq =3D &vdev->vq[i]; > + > + vq->started =3D false; > + vq->notification =3D true; > + > + if (vq->call_fd !=3D -1) { > + close(vq->call_fd); > + vq->call_fd =3D -1; > + } > + if (vq->err_fd !=3D -1) { > + close(vq->err_fd); > + vq->err_fd =3D -1; > + } > + if (vq->kick_fd !=3D -1) { > + vu_remove_watch(vdev, vq->kick_fd); > + close(vq->kick_fd); > + vq->kick_fd =3D -1; > + } > + > + vq->vring.desc =3D 0; > + vq->vring.used =3D 0; > + vq->vring.avail =3D 0; > + } > + vdev->hdrlen =3D 0; > + > + for (i =3D 0; i < vdev->nregions; i++) { > + const VuDevRegion *r =3D &vdev->regions[i]; > + /* NOLINTNEXTLINE(performance-no-int-to-ptr) */ > + void *m =3D (void *)r->mmap_addr; > + > + if (m) > + munmap(m, r->size + r->mmap_offset); > + } > + vdev->nregions =3D 0; > +} > + > +/** > + * tap_handler_vu() - Packet handler for vhost-user > + * @c: Execution context > + * @events: epoll events > + */ > +/* cppcheck-suppress unusedFunction */ > +void tap_handler_vu(struct ctx *c, uint32_t events) > +{ > + VuDev *dev =3D &c->vdev; > + struct VhostUserMsg msg =3D { 0 }; > + bool need_reply, reply_requested; > + int ret; > + > + if (events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR)) { > + tap_sock_reset(c); > + return; > + } > + > + > + ret =3D vu_message_read_default(dev, c->fd_tap, &msg); > + if (ret <=3D 0) { > + if (errno !=3D EINTR && errno !=3D EAGAIN && errno !=3D EWOULDBLOCK) > + tap_sock_reset(c); > + return; > + } > + debug("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Vhost user mess= age =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D"); > + debug("Request: %s (%d)", vu_request_to_string(msg.hdr.request), > + msg.hdr.request); > + debug("Flags: 0x%x", msg.hdr.flags); > + debug("Size: %u", msg.hdr.size); > + > + need_reply =3D msg.hdr.flags & VHOST_USER_NEED_REPLY_MASK; > + switch (msg.hdr.request) { > + case VHOST_USER_GET_FEATURES: > + reply_requested =3D vu_get_features_exec(&msg); > + break; > + case VHOST_USER_SET_FEATURES: > + reply_requested =3D vu_set_features_exec(dev, &msg); > + break; > + case VHOST_USER_GET_PROTOCOL_FEATURES: > + reply_requested =3D vu_get_protocol_features_exec(&msg); > + break; > + case VHOST_USER_SET_PROTOCOL_FEATURES: > + reply_requested =3D vu_set_protocol_features_exec(dev, &msg); > + break; > + case VHOST_USER_GET_QUEUE_NUM: > + reply_requested =3D vu_get_queue_num_exec(&msg); > + break; > + case VHOST_USER_SET_OWNER: > + reply_requested =3D vu_set_owner_exec(); > + break; > + case VHOST_USER_SET_MEM_TABLE: > + reply_requested =3D vu_set_mem_table_exec(dev, &msg); > + break; > + case VHOST_USER_SET_VRING_NUM: > + reply_requested =3D vu_set_vring_num_exec(dev, &msg); > + break; > + case VHOST_USER_SET_VRING_ADDR: > + reply_requested =3D vu_set_vring_addr_exec(dev, &msg); > + break; > + case VHOST_USER_SET_VRING_BASE: > + reply_requested =3D vu_set_vring_base_exec(dev, &msg); > + break; > + case VHOST_USER_GET_VRING_BASE: > + reply_requested =3D vu_get_vring_base_exec(dev, &msg); > + break; > + case VHOST_USER_SET_VRING_KICK: > + reply_requested =3D vu_set_vring_kick_exec(dev, &msg); > + break; > + case VHOST_USER_SET_VRING_CALL: > + reply_requested =3D vu_set_vring_call_exec(dev, &msg); > + break; > + case VHOST_USER_SET_VRING_ERR: > + reply_requested =3D vu_set_vring_err_exec(dev, &msg); > + break; > + case VHOST_USER_SET_VRING_ENABLE: > + reply_requested =3D vu_set_vring_enable_exec(dev, &msg); > + break; > + case VHOST_USER_NONE: > + vu_cleanup(dev); > + return; > + default: > + vu_panic(dev, "Unhandled request: %d", msg.hdr.request); > + return; > + } > + > + if (!reply_requested && need_reply) { > + msg.payload.u64 =3D 0; > + msg.hdr.flags =3D 0; > + msg.hdr.size =3D sizeof(msg.payload.u64); > + msg.fd_num =3D 0; > + reply_requested =3D true; > + } > + > + if (reply_requested) > + vu_send_reply(dev, c->fd_tap, &msg); > + free(msg.data); > +} > diff --git a/vhost_user.h b/vhost_user.h > new file mode 100644 > index 000000000000..c6edc49a3bb9 > --- /dev/null > +++ b/vhost_user.h > @@ -0,0 +1,137 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > + > +/* some parts from subprojects/libvhost-user/libvhost-user.h */ > + > +#ifndef VHOST_USER_H > +#define VHOST_USER_H > + > +#include "virtio.h" > +#include "iov.h" > + > +#define VHOST_USER_F_PROTOCOL_FEATURES 30 > + > +#define VHOST_MEMORY_BASELINE_NREGIONS 8 > + > +enum vhost_user_protocol_feature { > + VHOST_USER_PROTOCOL_F_MQ =3D 0, > + VHOST_USER_PROTOCOL_F_LOG_SHMFD =3D 1, > + VHOST_USER_PROTOCOL_F_RARP =3D 2, > + VHOST_USER_PROTOCOL_F_REPLY_ACK =3D 3, > + VHOST_USER_PROTOCOL_F_NET_MTU =3D 4, > + VHOST_USER_PROTOCOL_F_BACKEND_REQ =3D 5, > + VHOST_USER_PROTOCOL_F_CROSS_ENDIAN =3D 6, > + VHOST_USER_PROTOCOL_F_CRYPTO_SESSION =3D 7, > + VHOST_USER_PROTOCOL_F_PAGEFAULT =3D 8, > + VHOST_USER_PROTOCOL_F_CONFIG =3D 9, > + VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD =3D 10, > + VHOST_USER_PROTOCOL_F_HOST_NOTIFIER =3D 11, > + VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD =3D 12, > + VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS =3D 14, > + VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS =3D 15, > + > + VHOST_USER_PROTOCOL_F_MAX > +}; > + > +enum vhost_user_request { > + VHOST_USER_NONE =3D 0, > + VHOST_USER_GET_FEATURES =3D 1, > + VHOST_USER_SET_FEATURES =3D 2, > + VHOST_USER_SET_OWNER =3D 3, > + VHOST_USER_RESET_OWNER =3D 4, > + VHOST_USER_SET_MEM_TABLE =3D 5, > + VHOST_USER_SET_LOG_BASE =3D 6, > + VHOST_USER_SET_LOG_FD =3D 7, > + VHOST_USER_SET_VRING_NUM =3D 8, > + VHOST_USER_SET_VRING_ADDR =3D 9, > + VHOST_USER_SET_VRING_BASE =3D 10, > + VHOST_USER_GET_VRING_BASE =3D 11, > + VHOST_USER_SET_VRING_KICK =3D 12, > + VHOST_USER_SET_VRING_CALL =3D 13, > + VHOST_USER_SET_VRING_ERR =3D 14, > + VHOST_USER_GET_PROTOCOL_FEATURES =3D 15, > + VHOST_USER_SET_PROTOCOL_FEATURES =3D 16, > + VHOST_USER_GET_QUEUE_NUM =3D 17, > + VHOST_USER_SET_VRING_ENABLE =3D 18, > + VHOST_USER_SEND_RARP =3D 19, > + VHOST_USER_NET_SET_MTU =3D 20, > + VHOST_USER_SET_BACKEND_REQ_FD =3D 21, > + VHOST_USER_IOTLB_MSG =3D 22, > + VHOST_USER_SET_VRING_ENDIAN =3D 23, > + VHOST_USER_GET_CONFIG =3D 24, > + VHOST_USER_SET_CONFIG =3D 25, > + VHOST_USER_CREATE_CRYPTO_SESSION =3D 26, > + VHOST_USER_CLOSE_CRYPTO_SESSION =3D 27, > + VHOST_USER_POSTCOPY_ADVISE =3D 28, > + VHOST_USER_POSTCOPY_LISTEN =3D 29, > + VHOST_USER_POSTCOPY_END =3D 30, > + VHOST_USER_GET_INFLIGHT_FD =3D 31, > + VHOST_USER_SET_INFLIGHT_FD =3D 32, > + VHOST_USER_GPU_SET_SOCKET =3D 33, > + VHOST_USER_VRING_KICK =3D 35, > + VHOST_USER_GET_MAX_MEM_SLOTS =3D 36, > + VHOST_USER_ADD_MEM_REG =3D 37, > + VHOST_USER_REM_MEM_REG =3D 38, > + VHOST_USER_MAX > +}; > + > +typedef struct { > + enum vhost_user_request request; > + > +#define VHOST_USER_VERSION_MASK 0x3 > +#define VHOST_USER_REPLY_MASK (0x1 << 2) > +#define VHOST_USER_NEED_REPLY_MASK (0x1 << 3) > + uint32_t flags; > + uint32_t size; /* the following payload size */ > +} __attribute__ ((__packed__)) vhost_user_header; > + > +typedef struct VhostUserMemory_region { > + uint64_t guest_phys_addr; > + uint64_t memory_size; > + uint64_t userspace_addr; > + uint64_t mmap_offset; > +} VhostUserMemory_region; > + > +struct VhostUserMemory { > + uint32_t nregions; > + uint32_t padding; > + struct VhostUserMemory_region regions[VHOST_MEMORY_BASELINE_NREGIONS]; > +}; > + > +typedef union { > +#define VHOST_USER_VRING_IDX_MASK 0xff > +#define VHOST_USER_VRING_NOFD_MASK (0x1 << 8) > + uint64_t u64; > + struct vhost_vring_state state; > + struct vhost_vring_addr addr; > + struct VhostUserMemory memory; > +} vhost_user_payload; > + > +typedef struct VhostUserMsg { > + vhost_user_header hdr; > + vhost_user_payload payload; > + > + int fds[VHOST_MEMORY_BASELINE_NREGIONS]; > + int fd_num; > + uint8_t *data; > +} __attribute__ ((__packed__)) VhostUserMsg; > +#define VHOST_USER_HDR_SIZE sizeof(vhost_user_header) > + > +#define VHOST_USER_RX_QUEUE 0 > +#define VHOST_USER_TX_QUEUE 1 > + > +static inline bool vu_queue_enabled(const VuVirtq *vq) > +{ > + return vq->enable; > +} > + > +static inline bool vu_queue_started(const VuVirtq *vq) > +{ > + return vq->started; > +} > + > +int vu_send(const struct ctx *c, const void *buf, size_t size); > +void vu_print_capabilities(void); > +void vu_init(struct ctx *c); > +void vu_kick_cb(struct ctx *c, union epoll_ref ref); > +void tap_handler_vu(struct ctx *c, uint32_t events); > +#endif /* VHOST_USER_H */ > diff --git a/virtio.c b/virtio.c > index 50ec8b5119ed..5d58e56204b3 100644 > --- a/virtio.c > +++ b/virtio.c > @@ -169,7 +169,6 @@ static bool vring_notify(const VuDev *dev, VuVirtq *v= q) > return !v || vring_need_event(vring_get_used_event(vq), new, old); > } > =20 > -/* cppcheck-suppress unusedFunction */ > void vu_queue_notify(VuDev *dev, VuVirtq *vq) > { > if (dev->broken || !vq->vring.avail) > @@ -313,7 +312,6 @@ vu_queue_map_desc(VuDev *dev, VuVirtq *vq, unsigned i= nt idx, > return 0; > } > =20 > -/* cppcheck-suppress unusedFunction */ > int vu_queue_pop(VuDev *dev, VuVirtq *vq, VuVirtqElement *elem) > { > unsigned int head; > @@ -363,7 +361,6 @@ void vu_queue_detach_element(VuDev *dev, VuVirtq *vq, > /* unmap, when DMA support is added */ > } > =20 > -/* cppcheck-suppress unusedFunction */ > void vu_queue_unpop(VuDev *dev, VuVirtq *vq, unsigned int index, size_t = len) > { > vq->last_avail_idx--; > diff --git a/virtio.h b/virtio.h > index 61398bb432bc..f4a9f0c23804 100644 > --- a/virtio.h > +++ b/virtio.h > @@ -100,7 +100,6 @@ static inline bool vu_has_feature(const VuDev *vdev, = unsigned int fbit) > return has_feature(vdev->features, fbit); > } > =20 > -/* cppcheck-suppress unusedFunction */ > static inline bool vu_has_protocol_feature(const VuDev *vdev, unsigned i= nt fbit) > { > return has_feature(vdev->protocol_features, fbit); --=20 David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson --NpuO+nFaI+ORL/6D Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmZ44a0ACgkQzQJF27ox 2GeCVA//cdcb+MQMxoiMq3CZsHnKLSHchvtpQ6MF+BbfjZ42CJUxbtaYYWR5r0X6 XrsEwcJD1CKoWfZ2E0y4WGwhDrth3GbF02P2SshT9F4rBAB7KWFUvf4KeLTtATIe lxdKO2wMlL8UcMEnDhGCgOejnvS4vesSfmXuFFCMhtcGgUgaLStJYDts5/3wvmEP gvpXe6YINc2osoymMVaWeqRQ2MF989p+lSUyXEd6GevjrJHvNJEcTCGM3yxMCf+z 5jMWGafYit3KAEXlSx9pKTxxkMlP2hbEJG2d0Em9LuuSibO2981ivOtUXpsHqXT/ riifjXD42YrUcUmaWK+RlEGLBIfVeEonu19QUf5liEvcNZUuhEoOSsfkMNTBKprv kMA7uXyifm1BarOT8+eHon2cgxBLXF8rRqN6FdtCug6M36g5UJ2UuKfpO/XtPxzS XV/rzc1HuH9xkKSnH7PgLWpaXFXKj92nP7Pb86BPjBzB8pocd6sqiX2SAEuxtP8w IdpipHjURxMDZLrGkJFWMY07TVCrOLs0dq7eBKLAdmYDQ2B7qazzkkABAtdKd0Ij qQGxGtlMgejev/mjizXF8CNNwF4njYwUWM3nEt2dawZejShy2aIsU+1XSgV0SWoI /9oTBRGFvQV1Z5bxA9nyCZuU0Jp1XRqiBYcXqjWvT1kQyHadNcg= =5NFZ -----END PGP SIGNATURE----- --NpuO+nFaI+ORL/6D--