From nobody Wed Dec 17 05:38:41 2025 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=1655309547; cv=none; d=zohomail.com; s=zohoarc; b=LOtOqz4y7ME3D35Dqt/GhIUGvfTtQXZLkQ1AuYkpE1ekncs6mT5VLf4hFXHrQV3nNF7NJgCxLLmE4A4bSOcMyh33y+Jo5/NY0CEK7xdDdbOLQF2Zxwc+03F+ytSy1rFZ/4TVkIDgIM4vRf9iQfgBCBIlZGBxQgJWiGCRp2FGJx0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1655309547; h=Content-Type: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=1f6Yq0XPUOYY4LLbBP/yFWgZRwaHkFgJLCFl4RDmBlY=; b=KD3QwYtCGB/ZtPQkDOT39hFydTUJV7a2gragkikq9FzAkVDulz4MN4MkOJcJBF658weVKjeapgXrzu3bek9xuQI6Emv5toGkBCxUrKDouF6rOWw8EJDYZPFt5oeRVGjLZjey8SI8FRp+JhVPwseq8c/XHGVhYH4lqX57PpXHtrA= 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 1655309547967328.78468229570706; Wed, 15 Jun 2022 09:12:27 -0700 (PDT) Received: from localhost ([::1]:39024 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o1Vd4-0007R5-Qi for importer@patchew.org; Wed, 15 Jun 2022 12:12:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43254) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o1VLp-00048C-Fa for qemu-devel@nongnu.org; Wed, 15 Jun 2022 11:54:37 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:46765) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o1VLj-0007KT-9M for qemu-devel@nongnu.org; Wed, 15 Jun 2022 11:54:33 -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-154-Co6vVlrCOke_H4Kf8SnYfw-1; Wed, 15 Jun 2022 11:54:25 -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 4BCFB29AB443; Wed, 15 Jun 2022 15:54:24 +0000 (UTC) Received: from localhost (unknown [10.39.192.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8BA6140334F; Wed, 15 Jun 2022 15:54:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1655308469; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1f6Yq0XPUOYY4LLbBP/yFWgZRwaHkFgJLCFl4RDmBlY=; b=NiquS+efkHiCPaj438v1/qrTnh3XKgJGYVq/7w23gf8N1M2BKWw/l6IWOzC0NL4R5JnSQ2 lc8qhaQPHQfL4WUkehiwhGUJfw92Ckq6/nf7UtdQmD0afaGW7Pb+BX5vqFmEdktc9LnxgP C4Cx697DPF+CyaVdARsy1la2OHopG0I= X-MC-Unique: Co6vVlrCOke_H4Kf8SnYfw-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Thomas Huth , Jagannathan Raman , Aarushi Mehta , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Elena Ufimtseva , Marcel Apfelbaum , Stefan Hajnoczi , Laurent Vivier , , "Dr. David Alan Gilbert" , Richard Henderson , virtio-fs@redhat.com, Hanna Reitz , David Hildenbrand , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Eric Blake , Kevin Wolf , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Beraldo Leal , Peter Xu , Eduardo Habkost , Qiuhao Li , Paolo Bonzini , Markus Armbruster , Bandan Das , "Michael S. Tsirkin" , Stefano Garzarella , Alexander Bulekov , Julia Suvorova , Darren Kenny , Wainer dos Santos Moschetta , John G Johnson Subject: [PULL 07/18] vfio-user: define vfio-user-server object Date: Wed, 15 Jun 2022 16:51:18 +0100 Message-Id: <20220615155129.1025811-8-stefanha@redhat.com> In-Reply-To: <20220615155129.1025811-1-stefanha@redhat.com> References: <20220615155129.1025811-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 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=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable 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" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1655309548659100001 From: Jagannathan Raman Define vfio-user object which is remote process server for QEMU. Setup object initialization functions and properties necessary to instantiate the object Signed-off-by: Elena Ufimtseva Signed-off-by: John G Johnson Signed-off-by: Jagannathan Raman Reviewed-by: Stefan Hajnoczi Message-id: e45a17001e9b38f451543a664ababdf860e5f2f2.1655151679.git.jag.ram= an@oracle.com Signed-off-by: Stefan Hajnoczi --- MAINTAINERS | 1 + qapi/qom.json | 20 +++- include/hw/remote/machine.h | 2 + hw/remote/machine.c | 27 +++++ hw/remote/vfio-user-obj.c | 210 ++++++++++++++++++++++++++++++++++++ hw/remote/meson.build | 1 + hw/remote/trace-events | 3 + 7 files changed, 262 insertions(+), 2 deletions(-) create mode 100644 hw/remote/vfio-user-obj.c diff --git a/MAINTAINERS b/MAINTAINERS index d0fcaf0edb..cbac72e239 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -3643,6 +3643,7 @@ F: include/hw/remote/proxy-memory-listener.h F: hw/remote/iohub.c F: include/hw/remote/iohub.h F: subprojects/libvfio-user +F: hw/remote/vfio-user-obj.c =20 EBPF: M: Jason Wang diff --git a/qapi/qom.json b/qapi/qom.json index 6a653c6636..80dd419b39 100644 --- a/qapi/qom.json +++ b/qapi/qom.json @@ -734,6 +734,20 @@ { 'struct': 'RemoteObjectProperties', 'data': { 'fd': 'str', 'devid': 'str' } } =20 +## +# @VfioUserServerProperties: +# +# Properties for x-vfio-user-server objects. +# +# @socket: socket to be used by the libvfio-user library +# +# @device: the ID of the device to be emulated at the server +# +# Since: 7.1 +## +{ 'struct': 'VfioUserServerProperties', + 'data': { 'socket': 'SocketAddress', 'device': 'str' } } + ## # @RngProperties: # @@ -874,7 +888,8 @@ 'tls-creds-psk', 'tls-creds-x509', 'tls-cipher-suites', - { 'name': 'x-remote-object', 'features': [ 'unstable' ] } + { 'name': 'x-remote-object', 'features': [ 'unstable' ] }, + { 'name': 'x-vfio-user-server', 'features': [ 'unstable' ] } ] } =20 ## @@ -938,7 +953,8 @@ 'tls-creds-psk': 'TlsCredsPskProperties', 'tls-creds-x509': 'TlsCredsX509Properties', 'tls-cipher-suites': 'TlsCredsProperties', - 'x-remote-object': 'RemoteObjectProperties' + 'x-remote-object': 'RemoteObjectProperties', + 'x-vfio-user-server': 'VfioUserServerProperties' } } =20 ## diff --git a/include/hw/remote/machine.h b/include/hw/remote/machine.h index 8d0fa98d33..ac32fda387 100644 --- a/include/hw/remote/machine.h +++ b/include/hw/remote/machine.h @@ -24,6 +24,8 @@ struct RemoteMachineState { RemoteIOHubState iohub; =20 bool vfio_user; + + bool auto_shutdown; }; =20 /* Used to pass to co-routine device and ioc. */ diff --git a/hw/remote/machine.c b/hw/remote/machine.c index 9f3cdc55c3..4d008ed721 100644 --- a/hw/remote/machine.c +++ b/hw/remote/machine.c @@ -77,6 +77,28 @@ static void remote_machine_set_vfio_user(Object *obj, bo= ol value, Error **errp) s->vfio_user =3D value; } =20 +static bool remote_machine_get_auto_shutdown(Object *obj, Error **errp) +{ + RemoteMachineState *s =3D REMOTE_MACHINE(obj); + + return s->auto_shutdown; +} + +static void remote_machine_set_auto_shutdown(Object *obj, bool value, + Error **errp) +{ + RemoteMachineState *s =3D REMOTE_MACHINE(obj); + + s->auto_shutdown =3D value; +} + +static void remote_machine_instance_init(Object *obj) +{ + RemoteMachineState *s =3D REMOTE_MACHINE(obj); + + s->auto_shutdown =3D true; +} + static void remote_machine_class_init(ObjectClass *oc, void *data) { MachineClass *mc =3D MACHINE_CLASS(oc); @@ -90,12 +112,17 @@ static void remote_machine_class_init(ObjectClass *oc,= void *data) object_class_property_add_bool(oc, "vfio-user", remote_machine_get_vfio_user, remote_machine_set_vfio_user); + + object_class_property_add_bool(oc, "auto-shutdown", + remote_machine_get_auto_shutdown, + remote_machine_set_auto_shutdown); } =20 static const TypeInfo remote_machine =3D { .name =3D TYPE_REMOTE_MACHINE, .parent =3D TYPE_MACHINE, .instance_size =3D sizeof(RemoteMachineState), + .instance_init =3D remote_machine_instance_init, .class_init =3D remote_machine_class_init, .interfaces =3D (InterfaceInfo[]) { { TYPE_HOTPLUG_HANDLER }, diff --git a/hw/remote/vfio-user-obj.c b/hw/remote/vfio-user-obj.c new file mode 100644 index 0000000000..bc49adcc27 --- /dev/null +++ b/hw/remote/vfio-user-obj.c @@ -0,0 +1,210 @@ +/** + * QEMU vfio-user-server server object + * + * Copyright =C2=A9 2022 Oracle and/or its affiliates. + * + * This work is licensed under the terms of the GNU GPL-v2, version 2 or l= ater. + * + * See the COPYING file in the top-level directory. + * + */ + +/** + * Usage: add options: + * -machine x-remote,vfio-user=3Don,auto-shutdown=3Don + * -device ,id=3D + * -object x-vfio-user-server,id=3D,type=3Dunix,path=3D, + * device=3D + * + * Note that x-vfio-user-server object must be used with x-remote machine = only. + * This server could only support PCI devices for now. + * + * type - SocketAddress type - presently "unix" alone is supported. Requir= ed + * option + * + * path - named unix socket, it will be created by the server. It is + * a required option + * + * device - id of a device on the server, a required option. PCI devices + * alone are supported presently. + */ + +#include "qemu/osdep.h" + +#include "qom/object.h" +#include "qom/object_interfaces.h" +#include "qemu/error-report.h" +#include "trace.h" +#include "sysemu/runstate.h" +#include "hw/boards.h" +#include "hw/remote/machine.h" +#include "qapi/error.h" +#include "qapi/qapi-visit-sockets.h" + +#define TYPE_VFU_OBJECT "x-vfio-user-server" +OBJECT_DECLARE_TYPE(VfuObject, VfuObjectClass, VFU_OBJECT) + +/** + * VFU_OBJECT_ERROR - reports an error message. If auto_shutdown + * is set, it aborts the machine on error. Otherwise, it logs an + * error message without aborting. + */ +#define VFU_OBJECT_ERROR(o, fmt, ...) \ + { \ + if (vfu_object_auto_shutdown()) { \ + error_setg(&error_abort, (fmt), ## __VA_ARGS__); \ + } else { \ + error_report((fmt), ## __VA_ARGS__); \ + } \ + } \ + +struct VfuObjectClass { + ObjectClass parent_class; + + unsigned int nr_devs; +}; + +struct VfuObject { + /* private */ + Object parent; + + SocketAddress *socket; + + char *device; + + Error *err; +}; + +static bool vfu_object_auto_shutdown(void) +{ + bool auto_shutdown =3D true; + Error *local_err =3D NULL; + + if (!current_machine) { + return auto_shutdown; + } + + auto_shutdown =3D object_property_get_bool(OBJECT(current_machine), + "auto-shutdown", + &local_err); + + /* + * local_err would be set if no such property exists - safe to ignore. + * Unlikely scenario as auto-shutdown is always defined for + * TYPE_REMOTE_MACHINE, and TYPE_VFU_OBJECT only works with + * TYPE_REMOTE_MACHINE + */ + if (local_err) { + auto_shutdown =3D true; + error_free(local_err); + } + + return auto_shutdown; +} + +static void vfu_object_set_socket(Object *obj, Visitor *v, const char *nam= e, + void *opaque, Error **errp) +{ + VfuObject *o =3D VFU_OBJECT(obj); + + qapi_free_SocketAddress(o->socket); + + o->socket =3D NULL; + + visit_type_SocketAddress(v, name, &o->socket, errp); + + if (o->socket->type !=3D SOCKET_ADDRESS_TYPE_UNIX) { + error_setg(errp, "vfu: Unsupported socket type - %s", + SocketAddressType_str(o->socket->type)); + qapi_free_SocketAddress(o->socket); + o->socket =3D NULL; + return; + } + + trace_vfu_prop("socket", o->socket->u.q_unix.path); +} + +static void vfu_object_set_device(Object *obj, const char *str, Error **er= rp) +{ + VfuObject *o =3D VFU_OBJECT(obj); + + g_free(o->device); + + o->device =3D g_strdup(str); + + trace_vfu_prop("device", str); +} + +static void vfu_object_init(Object *obj) +{ + VfuObjectClass *k =3D VFU_OBJECT_GET_CLASS(obj); + VfuObject *o =3D VFU_OBJECT(obj); + + k->nr_devs++; + + if (!object_dynamic_cast(OBJECT(current_machine), TYPE_REMOTE_MACHINE)= ) { + error_setg(&o->err, "vfu: %s only compatible with %s machine", + TYPE_VFU_OBJECT, TYPE_REMOTE_MACHINE); + return; + } +} + +static void vfu_object_finalize(Object *obj) +{ + VfuObjectClass *k =3D VFU_OBJECT_GET_CLASS(obj); + VfuObject *o =3D VFU_OBJECT(obj); + + k->nr_devs--; + + qapi_free_SocketAddress(o->socket); + + o->socket =3D NULL; + + g_free(o->device); + + o->device =3D NULL; + + if (!k->nr_devs && vfu_object_auto_shutdown()) { + qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN); + } +} + +static void vfu_object_class_init(ObjectClass *klass, void *data) +{ + VfuObjectClass *k =3D VFU_OBJECT_CLASS(klass); + + k->nr_devs =3D 0; + + object_class_property_add(klass, "socket", "SocketAddress", NULL, + vfu_object_set_socket, NULL, NULL); + object_class_property_set_description(klass, "socket", + "SocketAddress " + "(ex: type=3Dunix,path=3D/tmp/so= ck). " + "Only UNIX is presently supporte= d"); + object_class_property_add_str(klass, "device", NULL, + vfu_object_set_device); + object_class_property_set_description(klass, "device", + "device ID - only PCI devices " + "are presently supported"); +} + +static const TypeInfo vfu_object_info =3D { + .name =3D TYPE_VFU_OBJECT, + .parent =3D TYPE_OBJECT, + .instance_size =3D sizeof(VfuObject), + .instance_init =3D vfu_object_init, + .instance_finalize =3D vfu_object_finalize, + .class_size =3D sizeof(VfuObjectClass), + .class_init =3D vfu_object_class_init, + .interfaces =3D (InterfaceInfo[]) { + { TYPE_USER_CREATABLE }, + { } + } +}; + +static void vfu_register_types(void) +{ + type_register_static(&vfu_object_info); +} + +type_init(vfu_register_types); diff --git a/hw/remote/meson.build b/hw/remote/meson.build index 7da83350c8..0eb5a0f375 100644 --- a/hw/remote/meson.build +++ b/hw/remote/meson.build @@ -6,6 +6,7 @@ remote_ss.add(when: 'CONFIG_MULTIPROCESS', if_true: files('= message.c')) remote_ss.add(when: 'CONFIG_MULTIPROCESS', if_true: files('remote-obj.c')) remote_ss.add(when: 'CONFIG_MULTIPROCESS', if_true: files('proxy.c')) remote_ss.add(when: 'CONFIG_MULTIPROCESS', if_true: files('iohub.c')) +remote_ss.add(when: 'CONFIG_VFIO_USER_SERVER', if_true: files('vfio-user-o= bj.c')) =20 remote_ss.add(when: 'CONFIG_VFIO_USER_SERVER', if_true: libvfio_user_dep) =20 diff --git a/hw/remote/trace-events b/hw/remote/trace-events index 0b23974f90..7da12f0d96 100644 --- a/hw/remote/trace-events +++ b/hw/remote/trace-events @@ -2,3 +2,6 @@ =20 mpqemu_send_io_error(int cmd, int size, int nfds) "send command %d size %d= , %d file descriptors to remote process" mpqemu_recv_io_error(int cmd, int size, int nfds) "failed to receive %d si= ze %d, %d file descriptors to remote process" + +# vfio-user-obj.c +vfu_prop(const char *prop, const char *val) "vfu: setting %s as %s" --=20 2.36.1