From nobody Thu Nov 28 10:35:23 2024 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=1694072671; cv=none; d=zohomail.com; s=zohoarc; b=WgmUOAaktX0HqpFOrwnwNn30kJX8flITD7bEZtg2aaDX1jyVjeXgJcAHZG6/6P2ldjGPTE+imm24PiiGOFOVuQ7CpKk+WqQYmt5SJK5HAPXu6a+XRddwA3kPHjc8Au3ODymFj/FZyWFSRhXtHxUYcgQXQ7MgyuUCG4KKzuE0Sxc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1694072671; 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=pSdeVkdg9tMEg2wcLbjEiLvRGnuLTl/03NgTyHBBEJA=; b=CjGuCu9oy8F09ZViFnW2uXGZRjn4jUtgYY+ay0U399/Vr9xtMqIh230Brv4QK/FftR4PR29cBNERkdyFVrMh14VVba4xjmSZ0hzmedYjQe7EFcI/iRpLXPyai3injhy8eUhj0M18d9eqoYSOVCcA6pzAp54fu1q7a9aKSLF/iQ0= 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 1694072671694833.8174620583613; Thu, 7 Sep 2023 00:44:31 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qe9g0-0002XT-2x; Thu, 07 Sep 2023 03:43:44 -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 1qe9fy-0002Wh-S8 for qemu-devel@nongnu.org; Thu, 07 Sep 2023 03:43:42 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qe9fv-0005uN-Hr for qemu-devel@nongnu.org; Thu, 07 Sep 2023 03:43:42 -0400 Received: from mimecast-mx02.redhat.com (mx-ext.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-256-SzHaAkQfOXuUhprTSEQAtQ-1; Thu, 07 Sep 2023 03:43:34 -0400 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3A0913814957; Thu, 7 Sep 2023 07:43:34 +0000 (UTC) Received: from localhost.localdomain (unknown [10.45.225.127]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9BD9E40411F; Thu, 7 Sep 2023 07:43:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694072617; 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=pSdeVkdg9tMEg2wcLbjEiLvRGnuLTl/03NgTyHBBEJA=; b=WCdvzJs8a4acsJbB4UJZN5/P7f1oFjww+FS299BxUTc6RP1gZ1ilqQJBxaSSF3mKySUt6I 2znl25QHHtQRi4N6wyQIcxPe+Icpq2tI9X6IhbGAFBjr0+UqXyLdvVwOwuPox/AudeYoFM bv00APL7HvX9tuUIkV0Jmx4wh3Aqi2A= X-MC-Unique: SzHaAkQfOXuUhprTSEQAtQ-1 From: Albert Esteve To: qemu-devel@nongnu.org Cc: marcandre.lureau@gmail.com, cohuck@redhat.com, kraxel@redhat.com, "Michael S. Tsirkin" , Fam Zheng , philmd@linaro.org, Albert Esteve Subject: [PATCH v7 3/4] vhost-user: add shared_object msg Date: Thu, 7 Sep 2023 09:43:17 +0200 Message-ID: <20230907074318.528064-4-aesteve@redhat.com> In-Reply-To: <20230907074318.528064-1-aesteve@redhat.com> References: <20230907074318.528064-1-aesteve@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 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.133.124; envelope-from=aesteve@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, 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_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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: 1694072673784100015 Content-Type: text/plain; charset="utf-8" Add three new vhost-user protocol `VHOST_USER_BACKEND_SHARED_OBJECT_* messages`. These new messages are sent from vhost-user back-ends to interact with the virtio-dmabuf table in order to add or remove themselves as virtio exporters, or lookup for virtio dma-buf shared objects. The action taken in the front-end depends on the type stored in the virtio shared object hash table. When the table holds a pointer to a vhost backend for a given UUID, the front-end sends a VHOST_USER_GET_SHARED_OBJECT to the backend holding the shared object. The messages can only be sent after successfully negotiating a new VHOST_USER_PROTOCOL_F_SHARED_OBJECT vhost-user protocol feature bit. Finally, refactor code to send response message so that all common parts both for the common REPLY_ACK case, and other data responses, can call it and avoid code repetition. Signed-off-by: Albert Esteve --- docs/interop/vhost-user.rst | 57 ++++++++++ hw/virtio/vhost-user.c | 168 +++++++++++++++++++++++++++--- include/hw/virtio/vhost-backend.h | 3 + 3 files changed, 216 insertions(+), 12 deletions(-) diff --git a/docs/interop/vhost-user.rst b/docs/interop/vhost-user.rst index 5a070adbc1..415bb47a19 100644 --- a/docs/interop/vhost-user.rst +++ b/docs/interop/vhost-user.rst @@ -1440,6 +1440,18 @@ Front-end message types query the back-end for its device status as defined in the Virtio specification. =20 +``VHOST_USER_GET_SHARED_OBJECT`` + :id: 41 + :equivalent ioctl: N/A + :request payload: ``struct VhostUserShared`` + :reply payload: dmabuf fd + + When the ``VHOST_USER_PROTOCOL_F_SHARED_OBJECT`` protocol + feature has been successfully negotiated, and the UUID is found + in the exporters cache, this message is submitted by the front-end + to retrieve a given dma-buf fd from a given back-end, determined by + the requested UUID. Back-end will reply passing the fd when the operation + is successful, or no fd otherwise. =20 Back-end message types ---------------------- @@ -1528,6 +1540,51 @@ 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_ADD`` + :id: 6 + :equivalent ioctl: N/A + :request payload: ``struct VhostUserShared`` + :reply payload: N/A + + When the ``VHOST_USER_PROTOCOL_F_SHARED_OBJECT`` protocol + feature has been successfully negotiated, this message can be submitted + by the backends to add themselves as exporters to the virtio shared look= up + table. The back-end device gets associated with a UUID in the shared tab= le. + The back-end is responsible of keeping its own table with exported dma-b= uf fds. + When another back-end tries to import the resource associated with the U= UID, + it will send a message to the front-end, which will act as a proxy to the + exporter back-end. If ``VHOST_USER_PROTOCOL_F_REPLY_ACK`` is negotiated,= and + the back-end sets the ``VHOST_USER_NEED_REPLY`` flag, the front-end must + respond with zero when operation is successfully completed, or non-zero + otherwise. + +``VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE`` + :id: 7 + :equivalent ioctl: N/A + :request payload: ``struct VhostUserShared`` + :reply payload: N/A + + When the ``VHOST_USER_PROTOCOL_F_SHARED_OBJECT`` protocol + feature has been successfully negotiated, this message can be submitted + by the backend to remove themselves from to the virtio-dmabuf shared + table API. The shared table will remove the back-end device associated w= ith + the UUID. If ``VHOST_USER_PROTOCOL_F_REPLY_ACK`` is negotiated, and the + back-end sets the ``VHOST_USER_NEED_REPLY`` flag, the front-end must res= pond + with zero when operation is successfully completed, or non-zero otherwis= e. + +``VHOST_USER_BACKEND_SHARED_OBJECT_LOOKUP`` + :id: 8 + :equivalent ioctl: N/A + :request payload: ``struct VhostUserShared`` + :reply payload: dmabuf fd and ``u64`` + + When the ``VHOST_USER_PROTOCOL_F_SHARED_OBJECT`` protocol + feature has been successfully negotiated, this message can be submitted + by the backends to retrieve a given dma-buf fd from the virtio-dmabuf + shared table given a UUID. Frontend will reply passing the fd and a zero + when the operation is successful, or non-zero otherwise. Note that if the + operation fails, no fd is sent to the backend. + .. _reply_ack: =20 VHOST_USER_PROTOCOL_F_REPLY_ACK diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index 8dcf049d42..08ab256613 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/virtio-crypto.h" #include "hw/virtio/vhost-user.h" @@ -21,6 +22,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" @@ -74,6 +76,7 @@ enum VhostUserProtocolFeature { /* Feature 14 reserved for VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS.= */ VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS =3D 15, VHOST_USER_PROTOCOL_F_STATUS =3D 16, + VHOST_USER_PROTOCOL_F_SHARED_OBJECT =3D 17, VHOST_USER_PROTOCOL_F_MAX }; =20 @@ -121,6 +124,7 @@ typedef enum VhostUserRequest { VHOST_USER_REM_MEM_REG =3D 38, VHOST_USER_SET_STATUS =3D 39, VHOST_USER_GET_STATUS =3D 40, + VHOST_USER_GET_SHARED_OBJECT =3D 41, VHOST_USER_MAX } VhostUserRequest; =20 @@ -129,6 +133,9 @@ typedef enum VhostUserBackendRequest { 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_ADD =3D 6, + VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE =3D 7, + VHOST_USER_BACKEND_SHARED_OBJECT_LOOKUP =3D 8, VHOST_USER_BACKEND_MAX } VhostUserBackendRequest; =20 @@ -202,6 +209,10 @@ typedef struct VhostUserInflight { uint16_t queue_size; } VhostUserInflight; =20 +typedef struct VhostUserShared { + unsigned char uuid[16]; +} VhostUserShared; + typedef struct { VhostUserRequest request; =20 @@ -226,6 +237,7 @@ typedef union { VhostUserCryptoSession session; VhostUserVringArea area; VhostUserInflight inflight; + VhostUserShared object; } VhostUserPayload; =20 typedef struct VhostUserMsg { @@ -1601,6 +1613,139 @@ static int vhost_user_backend_handle_vring_host_not= ifier(struct vhost_dev *dev, return 0; } =20 +static int +vhost_user_backend_handle_shared_object_add(struct vhost_dev *dev, + VhostUserShared *object) +{ + QemuUUID uuid; + + memcpy(uuid.data, object->uuid, sizeof(object->uuid)); + return virtio_add_vhost_device(&uuid, dev); +} + +static int +vhost_user_backend_handle_shared_object_remove(VhostUserShared *object) +{ + QemuUUID uuid; + + memcpy(uuid.data, object->uuid, sizeof(object->uuid)); + return virtio_remove_resource(&uuid); +} + +static bool vhost_user_send_resp(QIOChannel *ioc, VhostUserHeader *hdr, + VhostUserPayload *payload, Error **errp) +{ + struct iovec iov[] =3D { + { .iov_base =3D hdr, .iov_len =3D VHOST_USER_HDR_SIZE }, + { .iov_base =3D payload, .iov_len =3D hdr->size }, + }; + + hdr->flags &=3D ~VHOST_USER_NEED_REPLY_MASK; + hdr->flags |=3D VHOST_USER_REPLY_MASK; + + return !qio_channel_writev_all(ioc, iov, ARRAY_SIZE(iov), errp); +} + +static bool +vhost_user_backend_send_dmabuf_fd(QIOChannel *ioc, VhostUserHeader *hdr, + VhostUserPayload *payload, Error **errp) +{ + hdr->size =3D sizeof(payload->u64); + return vhost_user_send_resp(ioc, hdr, payload, errp); +} + +int vhost_user_get_shared_object(struct vhost_dev *dev, unsigned char *uui= d, + int *dmabuf_fd) +{ + struct vhost_user *u =3D dev->opaque; + CharBackend *chr =3D u->user->chr; + int ret; + VhostUserMsg msg =3D { + .hdr.request =3D VHOST_USER_GET_SHARED_OBJECT, + .hdr.flags =3D VHOST_USER_VERSION, + }; + memcpy(msg.payload.object.uuid, uuid, sizeof(msg.payload.object.uuid)); + + ret =3D vhost_user_write(dev, &msg, NULL, 0); + if (ret < 0) { + return ret; + } + + ret =3D vhost_user_read(dev, &msg); + if (ret < 0) { + return ret; + } + + if (msg.hdr.request !=3D VHOST_USER_GET_SHARED_OBJECT) { + error_report("Received unexpected msg type. " + "Expected %d received %d", + VHOST_USER_GET_SHARED_OBJECT, msg.hdr.request); + return -EPROTO; + } + + *dmabuf_fd =3D qemu_chr_fe_get_msgfd(chr); + if (*dmabuf_fd < 0) { + error_report("Failed to get dmabuf fd"); + return -EIO; + } + + return 0; +} + +static int +vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u, + QIOChannel *ioc, + VhostUserHeader *hdr, + VhostUserPayload *payload) +{ + QemuUUID uuid; + CharBackend *chr =3D u->user->chr; + Error *local_err =3D NULL; + int dmabuf_fd =3D -1; + int fd_num =3D 0; + + memcpy(uuid.data, payload->object.uuid, sizeof(payload->object.uuid)); + + payload->u64 =3D 0; + switch (virtio_object_type(&uuid)) { + case TYPE_DMABUF: + dmabuf_fd =3D virtio_lookup_dmabuf(&uuid); + break; + case TYPE_VHOST_DEV: + { + struct vhost_dev *dev =3D virtio_lookup_vhost_device(&uuid); + if (dev =3D=3D NULL) { + payload->u64 =3D -EINVAL; + break; + } + int ret =3D vhost_user_get_shared_object(dev, uuid.data, &dmabuf_f= d); + if (ret < 0) { + payload->u64 =3D ret; + } + break; + } + case TYPE_INVALID: + payload->u64 =3D -EINVAL; + break; + } + + if (dmabuf_fd !=3D -1) { + fd_num++; + } + + if (qemu_chr_fe_set_msgfds(chr, &dmabuf_fd, fd_num) < 0) { + error_report("Failed to set msg fds."); + payload->u64 =3D -EINVAL; + } + + if (!vhost_user_backend_send_dmabuf_fd(ioc, hdr, payload, &local_err))= { + error_report_err(local_err); + return -EINVAL; + } + + return 0; +} + static void close_backend_channel(struct vhost_user *u) { g_source_destroy(u->backend_src); @@ -1658,6 +1803,16 @@ static gboolean backend_read(QIOChannel *ioc, GIOCon= dition condition, ret =3D vhost_user_backend_handle_vring_host_notifier(dev, &payloa= d.area, fd ? fd[0] : -1); break; + case VHOST_USER_BACKEND_SHARED_OBJECT_ADD: + ret =3D vhost_user_backend_handle_shared_object_add(dev, &payload.= object); + break; + case VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE: + ret =3D vhost_user_backend_handle_shared_object_remove(&payload.ob= ject); + break; + case VHOST_USER_BACKEND_SHARED_OBJECT_LOOKUP: + ret =3D vhost_user_backend_handle_shared_object_lookup(dev->opaque= , ioc, + &hdr, &payloa= d); + break; default: error_report("Received unexpected msg type: %d.", hdr.request); ret =3D -EINVAL; @@ -1668,21 +1823,10 @@ static gboolean backend_read(QIOChannel *ioc, GIOCo= ndition condition, * directly in their request handlers. */ if (hdr.flags & VHOST_USER_NEED_REPLY_MASK) { - struct iovec iovec[2]; - - - hdr.flags &=3D ~VHOST_USER_NEED_REPLY_MASK; - hdr.flags |=3D VHOST_USER_REPLY_MASK; - payload.u64 =3D !!ret; hdr.size =3D sizeof(payload.u64); =20 - iovec[0].iov_base =3D &hdr; - iovec[0].iov_len =3D VHOST_USER_HDR_SIZE; - iovec[1].iov_base =3D &payload; - iovec[1].iov_len =3D hdr.size; - - if (qio_channel_writev_all(ioc, iovec, ARRAY_SIZE(iovec), &local_e= rr)) { + if (!vhost_user_send_resp(ioc, &hdr, &payload, &local_err)) { error_report_err(local_err); goto err; } diff --git a/include/hw/virtio/vhost-backend.h b/include/hw/virtio/vhost-ba= ckend.h index 31a251a9f5..1860b541d8 100644 --- a/include/hw/virtio/vhost-backend.h +++ b/include/hw/virtio/vhost-backend.h @@ -196,4 +196,7 @@ int vhost_backend_handle_iotlb_msg(struct vhost_dev *de= v, =20 int vhost_user_gpu_set_socket(struct vhost_dev *dev, int fd); =20 +int vhost_user_get_shared_object(struct vhost_dev *dev, unsigned char *uui= d, + int *dmabuf_fd); + #endif /* VHOST_BACKEND_H */ --=20 2.41.0