From nobody Tue Feb 10 23:00:40 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1683102063; cv=none; d=zohomail.com; s=zohoarc; b=aeZ9M8lMEFtqev33gNTVVERdoBLcpKFpHDMMXNqRTY6OxsW5zqLTSsoCZPlEPVNLsRv6/7oxsM6KqXJ/9kQ+kcupQ2FFbpXIrc1qtokXLZeaLXgZEYxXlMBHLbDmAs9kLmbfVqgi1KZ4PGFw93lE1XNS/QOsEV8zvpf58xhYBGo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1683102063; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=rGa7aF7xzefzUg0hnqoaFanq8k4S3km/lz9lS8q1V5E=; b=IbIQcxFQWm+FUJ30foZWixbyuAO7u2Py3vgWj3zwjebrlFL7GeggiVu7aA0bAnjm05e3sJ7LjGAWyVvSLfrLElQ7qrL2riCUOMOJvqbg5wrlUzgmhmsuV/sPGGAzjH/Rce5AU/ZN8lNdq69u2SxMYnQRBTujDJvDQFS/6AMwegc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1683102063978834.4928347937031; Wed, 3 May 2023 01:21:03 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pu7j6-0000nm-VG; Wed, 03 May 2023 04:20:41 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pu7iw-0000k3-KP for qemu-devel@nongnu.org; Wed, 03 May 2023 04:20:30 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pu7iu-0002og-8j for qemu-devel@nongnu.org; Wed, 03 May 2023 04:20:30 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-110-PHHDWoEkNVmii1TC_W1lrw-1; Wed, 03 May 2023 04:19:22 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CDCB31C0950E for ; Wed, 3 May 2023 08:19:21 +0000 (UTC) Received: from localhost.localdomain (unknown [10.45.225.118]) by smtp.corp.redhat.com (Postfix) with ESMTP id EEB66C15BAE; Wed, 3 May 2023 08:19:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683102027; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rGa7aF7xzefzUg0hnqoaFanq8k4S3km/lz9lS8q1V5E=; b=NhOxJ5xvjZnrulI0Kn/m+FZVqYV5B0Q68cB69yAGMriS72GDJVDTPfOkWcxqKvfGywKuKF OzA5crJrcE3LUzeHVsdQNFcqeA/TBR+Z83k88NPyOAg3d+yNVHyL13gzIZjmB9v4SqKmx4 6h/mogNBorWlT9cTxOcTaSDpuZr9J7M= X-MC-Unique: PHHDWoEkNVmii1TC_W1lrw-1 From: Albert Esteve To: qemu-devel@nongnu.org Cc: "Michael S. Tsirkin" , Albert Esteve Subject: [PATCH 2/4] vhost-user: add shared_object msg Date: Wed, 3 May 2023 10:19:09 +0200 Message-Id: <20230503081911.119168-3-aesteve@redhat.com> In-Reply-To: <20230503081911.119168-1-aesteve@redhat.com> References: <20230503081911.119168-1-aesteve@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=aesteve@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -22 X-Spam_score: -2.3 X-Spam_bar: -- X-Spam_report: (-2.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.171, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1683102066094100001 Content-Type: text/plain; charset="utf-8" Add new vhost-user protocol message `VHOST_USER_BACKEND_SHARED_OBJECT`. This new message is sent from vhost-user back-ends to interact with the virtio-dmabuf table in order to add, remove, or lookup for virtio dma-buf shared objects. The action taken in the front-end depends on the type stored in the payload struct. In the libvhost-user library add a helper function (`vu_get_shared_object`) to send a message to lookup for the dma-buf given the UUID. Other operations, i.e., add and remove entries in the shared objects table, will have to be handled directly in the back-end. Signed-off-by: Albert Esteve --- docs/interop/vhost-user.rst | 15 +++++ hw/virtio/vhost-user.c | 68 +++++++++++++++++++++++ subprojects/libvhost-user/libvhost-user.c | 39 +++++++++++++ subprojects/libvhost-user/libvhost-user.h | 30 ++++++++++ 4 files changed, 152 insertions(+) diff --git a/docs/interop/vhost-user.rst b/docs/interop/vhost-user.rst index 5a070adbc1..d3d8db41e5 100644 --- a/docs/interop/vhost-user.rst +++ b/docs/interop/vhost-user.rst @@ -1528,6 +1528,21 @@ is sent by the front-end. =20 The state.num field is currently reserved and must be set to 0. =20 +``VHOST_USER_BACKEND_SHARED_OBJECT`` + :id: 6 + :equivalent ioctl: N/A + :request payload: ``struct VhostUserShared`` + :reply payload: ``struct VhostUserShared`` (only for ``LOOKUP`` requests) + + Backends that need to interact with the virtio-dmabuf shared table API + can send this message. The operation is determined by the ``type`` member + of the payload struct. The valid values for the operation type are + ``VHOST_SHARED_OBJECT_*`` members, i.e., ``ADD``, ``LOOKUP``, and ``REMO= VE``. + ``LOOKUP`` operations require the ``VHOST_USER_NEED_REPLY_MASK`` flag to= be + set by the back-end, and the front-end will then send the dma-buf fd as + a response if the UUID matches an object in the table, or a negative val= ue + otherwise. + .. _reply_ack: =20 VHOST_USER_PROTOCOL_F_REPLY_ACK diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index e5285df4ba..d0b889282a 100644 --- a/hw/virtio/vhost-user.c +++ b/hw/virtio/vhost-user.c @@ -10,6 +10,7 @@ =20 #include "qemu/osdep.h" #include "qapi/error.h" +#include "hw/virtio/virtio-dmabuf.h" #include "hw/virtio/vhost.h" #include "hw/virtio/vhost-user.h" #include "hw/virtio/vhost-backend.h" @@ -20,6 +21,7 @@ #include "sysemu/kvm.h" #include "qemu/error-report.h" #include "qemu/main-loop.h" +#include "qemu/uuid.h" #include "qemu/sockets.h" #include "sysemu/runstate.h" #include "sysemu/cryptodev.h" @@ -138,6 +140,7 @@ typedef enum VhostUserSlaveRequest { VHOST_USER_BACKEND_IOTLB_MSG =3D 1, VHOST_USER_BACKEND_CONFIG_CHANGE_MSG =3D 2, VHOST_USER_BACKEND_VRING_HOST_NOTIFIER_MSG =3D 3, + VHOST_USER_BACKEND_SHARED_OBJECT =3D 6, VHOST_USER_BACKEND_MAX } VhostUserSlaveRequest; =20 @@ -200,6 +203,18 @@ typedef struct VhostUserInflight { uint16_t queue_size; } VhostUserInflight; =20 +typedef enum VhostUserSharedType { + VHOST_SHARED_OBJECT_ADD =3D 0, + VHOST_SHARED_OBJECT_LOOKUP, + VHOST_SHARED_OBJECT_REMOVE, +} VhostUserSharedType; + +typedef struct VhostUserShared { + unsigned char uuid[16]; + VhostUserSharedType type; + int dmabuf_fd; +} VhostUserShared; + typedef struct { VhostUserRequest request; =20 @@ -224,6 +239,7 @@ typedef union { VhostUserCryptoSession session; VhostUserVringArea area; VhostUserInflight inflight; + VhostUserShared object; } VhostUserPayload; =20 typedef struct VhostUserMsg { @@ -1591,6 +1607,52 @@ static int vhost_user_slave_handle_vring_host_notifi= er(struct vhost_dev *dev, return 0; } =20 +static int vhost_user_backend_handle_shared_object(VhostUserShared *object) +{ + QemuUUID uuid; + memcpy(uuid.data, object->uuid, sizeof(object->uuid)); + + switch (object->type) { + case VHOST_SHARED_OBJECT_ADD: + return virtio_add_dmabuf(uuid, object->dmabuf_fd); + case VHOST_SHARED_OBJECT_LOOKUP: + object->dmabuf_fd =3D virtio_lookup_dmabuf(uuid); + if (object->dmabuf_fd < 0) { + return object->dmabuf_fd; + } + break; + case VHOST_SHARED_OBJECT_REMOVE: + return virtio_remove_resource(uuid); + } + + return 0; +} + +static bool +vhost_user_backend_send_dmabuf_fd(QIOChannel *ioc, VhostUserHeader *hdr, + VhostUserPayload *payload) +{ + Error *local_err =3D NULL; + struct iovec iov[2]; + if (hdr->flags & VHOST_USER_NEED_REPLY_MASK) { + hdr->flags &=3D ~VHOST_USER_NEED_REPLY_MASK; + hdr->flags |=3D VHOST_USER_REPLY_MASK; + + hdr->size =3D sizeof(payload->object); + + iov[0].iov_base =3D hdr; + iov[0].iov_len =3D VHOST_USER_HDR_SIZE; + iov[1].iov_base =3D payload; + iov[1].iov_len =3D hdr->size; + + if (qio_channel_writev_all(ioc, iov, ARRAY_SIZE(iov), &local_err))= { + error_report_err(local_err); + return false; + } + } + return true; +} + static void close_slave_channel(struct vhost_user *u) { g_source_destroy(u->slave_src); @@ -1648,6 +1710,12 @@ static gboolean slave_read(QIOChannel *ioc, GIOCondi= tion condition, ret =3D vhost_user_slave_handle_vring_host_notifier(dev, &payload.= area, fd ? fd[0] : -1); break; + case VHOST_USER_BACKEND_SHARED_OBJECT: + ret =3D vhost_user_backend_handle_shared_object(&payload.object); + if (!vhost_user_backend_send_dmabuf_fd(ioc, &hdr, &payload)) { + goto err; + } + break; default: error_report("Received unexpected msg type: %d.", hdr.request); ret =3D -EINVAL; diff --git a/subprojects/libvhost-user/libvhost-user.c b/subprojects/libvho= st-user/libvhost-user.c index 8fb61e2df2..6b4b721225 100644 --- a/subprojects/libvhost-user/libvhost-user.c +++ b/subprojects/libvhost-user/libvhost-user.c @@ -1403,6 +1403,45 @@ bool vu_set_queue_host_notifier(VuDev *dev, VuVirtq = *vq, int fd, return vu_process_message_reply(dev, &vmsg); } =20 +bool +vu_get_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN], int *dmabuf= _fd) +{ + int result =3D false; + VhostUserMsg msg_reply; + VhostUserMsg msg =3D { + .request =3D VHOST_USER_BACKEND_SHARED_OBJECT, + .size =3D sizeof(msg.payload.object), + .flags =3D VHOST_USER_VERSION | VHOST_USER_NEED_REPLY_MASK, + .payload.object =3D { + .type =3D VHOST_SHARED_OBJECT_LOOKUP, + }, + }; + + memcpy(msg.payload.object.uuid, uuid, sizeof(uuid[0]) * UUID_LEN); + + pthread_mutex_lock(&dev->slave_mutex); + if (!vu_message_write(dev, dev->slave_fd, &msg)) { + goto out; + } + + if (!vu_message_read_default(dev, dev->slave_fd, &msg_reply)) { + goto out; + } + + if (msg_reply.request !=3D msg.request) { + DPRINT("Received unexpected msg type. Expected %d, received %d", + msg.request, msg_reply.request); + goto out; + } + + *dmabuf_fd =3D msg_reply.payload.object.dmabuf_fd; + result =3D *dmabuf_fd > 0; +out: + pthread_mutex_unlock(&dev->slave_mutex); + + return result; +} + static bool vu_set_vring_call_exec(VuDev *dev, VhostUserMsg *vmsg) { diff --git a/subprojects/libvhost-user/libvhost-user.h b/subprojects/libvho= st-user/libvhost-user.h index 49208cceaa..784db65f7c 100644 --- a/subprojects/libvhost-user/libvhost-user.h +++ b/subprojects/libvhost-user/libvhost-user.h @@ -119,6 +119,7 @@ typedef enum VhostUserSlaveRequest { VHOST_USER_BACKEND_VRING_HOST_NOTIFIER_MSG =3D 3, VHOST_USER_BACKEND_VRING_CALL =3D 4, VHOST_USER_BACKEND_VRING_ERR =3D 5, + VHOST_USER_BACKEND_SHARED_OBJECT =3D 6, VHOST_USER_BACKEND_MAX } VhostUserSlaveRequest; =20 @@ -172,6 +173,20 @@ typedef struct VhostUserInflight { uint16_t queue_size; } VhostUserInflight; =20 +typedef enum VhostUserSharedType { + VHOST_SHARED_OBJECT_ADD =3D 0, + VHOST_SHARED_OBJECT_LOOKUP, + VHOST_SHARED_OBJECT_REMOVE, +} VhostUserSharedType; + +#define UUID_LEN 16 + +typedef struct VhostUserShared { + unsigned char uuid[UUID_LEN]; + VhostUserSharedType type; + int dmabuf_fd; +} VhostUserShared; + #if defined(_WIN32) && (defined(__x86_64__) || defined(__i386__)) # define VU_PACKED __attribute__((gcc_struct, packed)) #else @@ -199,6 +214,7 @@ typedef struct VhostUserMsg { VhostUserConfig config; VhostUserVringArea area; VhostUserInflight inflight; + VhostUserShared object; } payload; =20 int fds[VHOST_MEMORY_BASELINE_NREGIONS]; @@ -539,6 +555,20 @@ void vu_set_queue_handler(VuDev *dev, VuVirtq *vq, bool vu_set_queue_host_notifier(VuDev *dev, VuVirtq *vq, int fd, int size, int offset); =20 +/** + * vu_get_shared_object: + * @dev: a VuDev context + * @uuid: UUID of the shared object + * @dmabuf_fd: output dma-buf file descriptor + * + * Lookup for a virtio shared object (i.e., dma-buf fd) associated with the + * received UUID. Result, if found, is stored in the dmabuf_fd argument. + * + * Returns: whether the virtio object was found. + */ +bool vu_get_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN], + int *dmabuf_fd); + /** * vu_queue_set_notification: * @dev: a VuDev context --=20 2.40.0