From patchwork Wed Jan 8 11:50:17 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: John Levon X-Patchwork-Id: 13930702 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B6286E7719A for ; Wed, 8 Jan 2025 11:54:47 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tVUdM-0006S5-8y; Wed, 08 Jan 2025 06:54:01 -0500 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 1tVUdH-0006RE-FM for qemu-devel@nongnu.org; Wed, 08 Jan 2025 06:53:55 -0500 Received: from mx0b-002c1b01.pphosted.com ([148.163.155.12]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tVUdB-0002Ew-Ds for qemu-devel@nongnu.org; Wed, 08 Jan 2025 06:53:53 -0500 Received: from pps.filterd (m0127842.ppops.net [127.0.0.1]) by mx0b-002c1b01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 5087vts7007169; Wed, 8 Jan 2025 03:53:48 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nutanix.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s= proofpoint20171006; bh=e2pH8ec9fPJ/yQmaQBOhWEct2hE2OKTew5a0CgjBL gk=; b=C9CEPmplPdtT/CYBrXJRxZs/zSrB7pSGkEit71eUd2Wf8APww7KkOPeQX Ncr/hp565aFmP3suXeGEL2Ojqb1gVES4w1XH66Zffj75hiwyB438xcObSrAs6JZ+ yqVHr9jJo/UHVBToWbXYeEUlN5L5FYMu/ol6cLsdbKGs5b3G8RUlUHpuT/MU2VlK o5KOgYc1P7hJQNg+bHkz9fNkZmAH+1JT0oPzDM5aQoAHlzGuHFq3+SMCnmHaSijm coCEJsepVNJa858mJv9MHMhrSD2JyMmrlxb3/HaCJ35UQShyJZcmO9fSVN7FR0X/ DztL9PyE8Jo+G43s5gvq+dTNt/8Wg== Received: from nam04-dm6-obe.outbound.protection.outlook.com (mail-dm6nam04lp2048.outbound.protection.outlook.com [104.47.73.48]) by mx0b-002c1b01.pphosted.com (PPS) with ESMTPS id 43y56eryxp-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 08 Jan 2025 03:53:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=S3J9EV4ZwmCxb9jQQ/uVs8eM9MSZJ/MIv0QrV9bSW9/eoaBOxchNxKSY9n5NymvlLW80av+7Us4tMbYlQ4u50wv21ooyXkoChhestxtbU+kN9aOtemqBhMQcbnNRyQJ2wckezhdEm1rWWZQg7ST1jT444CqGNIHBSNskLleDs8wY2RuB7gRtARiMzDnmgrbq1Kq9dZtSnN4ZnPRbXUXAJ/al7rHffnXgQhnzShvyojAHKUotdr3D0zUETEZWPQqUfD5o5wAjnBxQb8pHXd5zS0tWBDZNvgz/YOVR6Xv2hl3u5loShyHijP5juokI8IxyxmwVSn7FcwOu2ayNULkhJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=e2pH8ec9fPJ/yQmaQBOhWEct2hE2OKTew5a0CgjBLgk=; b=yiMOlUQGzqJ9w6Bcz4Z7My9wxLcnYx6RF+y+pPWHA9dmI92zzsjg7bRXgr4e208dWRqqXvTs3ZnA/UK4UrDUX7BawvfLoo+FskVmvXC18zJ84x9YsfHWDxHLeUPhLa063wkq7GKHrc9DVxOKKnusJnBhqUZcQUnZZqS+fSxP0tKzl92waUyA0zISFRI3hbpREy5y88sZ4CnoAgQVZB6PXzM8gb8beMEKPsApAohY6C8/fuOSwW08A6iW48HvGKLro4HF9UJBmPK8uwf4e1tlS4LjPl6RNuZ57j5d2JeClr/dSLmHi6sVAuT5Os/F5zGTqPzRHyB44eAcvbTR0Y5vqQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nutanix.com; dmarc=pass action=none header.from=nutanix.com; dkim=pass header.d=nutanix.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nutanix.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=e2pH8ec9fPJ/yQmaQBOhWEct2hE2OKTew5a0CgjBLgk=; b=lQemC1YamFuAY/b/2GERcmuRtJaGNE5Uq61IAHoYtESvRl66qncRFGAZ1tshjqMMuK6dqUPn4kx3Ys+TTUY8AdYN4ir2jaRGLtXk2yr4etCU/QbcSHsPw2v/WqJobxkFds02IbDejXWNy9fcPzElW+hBJ+WPWBL1fYqHqw/JBMQBxwLfOjXLHB5FXBNHVKDh+wggY6jk7GdPfSYr//8Dn9M1hL99S6yUjqd1jfZpAwSfiydDMYUMRNjB0YVGjprHqvLZTvcT3yXLGwKQkzsq/mIlIn/EKKB6lzaJaIJetQcbRaIBr8dii2fbn3mGFRaU0onvhrnjzez4nF/zU5K2nQ== Received: from CH2PR02MB6760.namprd02.prod.outlook.com (2603:10b6:610:7f::9) by SJ0PR02MB7502.namprd02.prod.outlook.com (2603:10b6:a03:2a2::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8335.11; Wed, 8 Jan 2025 11:53:43 +0000 Received: from CH2PR02MB6760.namprd02.prod.outlook.com ([fe80::fd77:ea65:a159:ef51]) by CH2PR02MB6760.namprd02.prod.outlook.com ([fe80::fd77:ea65:a159:ef51%4]) with mapi id 15.20.8335.010; Wed, 8 Jan 2025 11:53:43 +0000 From: John Levon To: qemu-devel@nongnu.org Cc: elena.ufimtseva@oracle.com, alex.williamson@redhat.com, clg@redhat.com, jag.raman@oracle.com, thanos.makatos@nutanix.com Subject: [PATCH 11/26] vfio-user: connect vfio proxy to remote server Date: Wed, 8 Jan 2025 11:50:17 +0000 Message-Id: <20250108115032.1677686-12-john.levon@nutanix.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250108115032.1677686-1-john.levon@nutanix.com> References: <20250108115032.1677686-1-john.levon@nutanix.com> X-ClientProxiedBy: AS4P189CA0066.EURP189.PROD.OUTLOOK.COM (2603:10a6:20b:659::14) To CH2PR02MB6760.namprd02.prod.outlook.com (2603:10b6:610:7f::9) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR02MB6760:EE_|SJ0PR02MB7502:EE_ X-MS-Office365-Filtering-Correlation-Id: 8cb719b7-4503-4865-e9c8-08dd2fdb1a03 x-proofpoint-crosstenant: true X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|1800799024|366016; X-Microsoft-Antispam-Message-Info: =?utf-8?q?cLxWChhd9ydjkYF2+8WdGASK86vp3ik?= =?utf-8?q?ht5Y0nwgAhKcgZCpWpGDiAQy2JRjzeHQGs6PXTr7sm2XFcCpal0hmzU5H/SKT3Ew3?= =?utf-8?q?+j2EYSMcp5Y5VVT8MSMuJq9TsXMkwwk+XvXTstiMSpPfG0D/fzCt8fCcor0tLcXp2?= =?utf-8?q?ajUgd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8eRNb6pqeeVDUwOS+6Vpvbo7Kd9lC27RA4zZzOwHN6QxiHRjKcKss?= =?utf-8?q?5wO+FnsQiaLfXeAHDS4CSKHCdvEljMi5Z7NuLfxABSSOVTHLWFPGbHJs0gct+NpSV?= =?utf-8?q?7SjQM3xrLT/VcQKz7YtOfvlnhRCrV74b7CupLksHZNg8Qm2WZ1oGdV7xoSj5T2gny?= =?utf-8?q?8QOwbQIVrvpXzFFKENLteeHHKJkhneYAPjbi/dtrj7/lk2OkOuIs++Pl5t0UC80W7?= =?utf-8?q?tT1I0fJ/a1rj9cT/LHGhHV795EW8yzgZkMN/VbNh9SvZIUuPAKQko8mMFO+HaDFwi?= =?utf-8?q?xISVpkKCpt3w?= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CH2PR02MB6760.namprd02.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(376014)(1800799024)(366016); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?clmnlOd6w2fDLte6zYlBVWlsL0n+?= =?utf-8?q?ZZn8ookDrsL/5kvNz3sd5gOsVmLrNt+zJwg9JnGhh7/mNQdmI1CrhFC7blNfGn0TH?= =?utf-8?q?LedM2UVDUuFnOxBRbnlMENUkgMxWwft8m+ta4PIPCX+4FWND0A/PgF1XIn6LAN7eg?= =?utf-8?q?JVPYkpomDCNbtzRTdpX5r1Advsg12xnsWkE+E6d8bIJDCbREJxObWrSJYL80S1bbp?= =?utf-8?q?Uko+rV+ZlZs2r99M8jFiR/w2/LxNzhy3Scv4kEtKr8zdYekOYfcVYjWT/oQMTMt2D?= =?utf-8?q?blxU2pSmN2L0HsjRpkCQ5N5cJb0XGmAWwGT0Uec8p2bVkLIUk2WjiFgtoohRxSNsf?= =?utf-8?q?vj37oQfdS1ZjaseeoFbCFumcNSBqayJ9vbHObw7GrY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aLDTkkwa04wwvdilP43OD5TRKRuSe?= =?utf-8?q?z8ehZPCTOGP3TMx1pzez/W7JFYJwlWD7LOFg/zIuqx3ks9bz7dOoWuo3OGLxsmPIy?= =?utf-8?q?9cQzQ4eWWLyVY9SONfpdznjzF+lEn41vkpONbBdK5bV8hGGbfy5/tFnbw+3dMHZfA?= =?utf-8?q?DDyAI8cClrlshabVU1J9wkYiFq+R197Z+WJpVktAHxbWveT1t6BKWRPXHSOzBdWgB?= =?utf-8?q?KLnXAkEG+0MXcFxvnpsbNd0WU/SgNoNt/RE9MMNxoSd0TLjxmgpnnDvjnH9p367r3?= =?utf-8?q?v6R4hwY5H4s5?= X-OriginatorOrg: nutanix.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8cb719b7-4503-4865-e9c8-08dd2fdb1a03 X-MS-Exchange-CrossTenant-AuthSource: CH2PR02MB6760.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Jan 2025 11:53:43.8832 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: bb047546-786f-4de1-bd75-24e5b6f79043 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: QUE/CwdwABaM0P2HrxmnKpv1t4bKfkYCWgl0HBmUulrbW5UZedS9I9aE2RlGbpb9PNXQehpgnJLsJtoIwpvHcA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR02MB7502 X-Authority-Analysis: v=2.4 cv=A6aWP7WG c=1 sm=1 tr=0 ts=677e674c cx=c_pps a=MHkl0I0wjNeC5ak5fNlPUA==:117 a=wKuvFiaSGQ0qltdbU6+NXLB8nM8=:19 a=Ol13hO9ccFRV9qXi2t6ftBPywas=:19 a=xqWC_Br6kY4A:10 a=IkcTkHD0fZMA:10 a=VdSt8ZQiCzkA:10 a=0034W8JfsZAA:10 a=0kUYKlekyDsA:10 a=yPCof4ZbAAAA:8 a=64Cc0HZtAAAA:8 a=vhHEnE-nI2wpHcbFAcgA:9 a=3ZKOabzyN94A:10 a=QEXdDO2ut3YA:10 a=14NRyaPF5x3gF6G45PvQ:22 X-Proofpoint-GUID: A276CHxlHgv9nT6e407yzMQKnP_ikcKU X-Proofpoint-ORIG-GUID: A276CHxlHgv9nT6e407yzMQKnP_ikcKU X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2025-01-08_02,2025-01-08_01,2024-11-22_01 X-Proofpoint-Spam-Reason: safe Received-SPF: pass client-ip=148.163.155.12; envelope-from=john.levon@nutanix.com; helo=mx0b-002c1b01.pphosted.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.432, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Jagannathan Raman add user.c & user.h files for vfio-user code add proxy struct to handle comms with remote server Originally-by: John Johnson Signed-off-by: Elena Ufimtseva Signed-off-by: Jagannathan Raman Signed-off-by: John Levon --- MAINTAINERS | 1 + hw/vfio/meson.build | 2 +- hw/vfio/user-pci.c | 17 ++++ hw/vfio/user.c | 171 ++++++++++++++++++++++++++++++++++ hw/vfio/user.h | 78 ++++++++++++++++ include/hw/vfio/vfio-common.h | 2 + 6 files changed, 270 insertions(+), 1 deletion(-) create mode 100644 hw/vfio/user.c create mode 100644 hw/vfio/user.h diff --git a/MAINTAINERS b/MAINTAINERS index b0f9b54500..153825b463 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -4141,6 +4141,7 @@ S: Supported F: docs/devel/vfio-user.rst F: hw/vfio/user-container.c F: hw/vfio/user-pci.c +F: hw/vfio/user.* F: subprojects/libvfio-user EBPF: diff --git a/hw/vfio/meson.build b/hw/vfio/meson.build index f897c5b81a..32ad5ca6b7 100644 --- a/hw/vfio/meson.build +++ b/hw/vfio/meson.build @@ -18,7 +18,7 @@ vfio_ss.add(when: 'CONFIG_VFIO_PCI', if_true: files( )) if get_option('vfio_user_client').enabled() - vfio_ss.add(files('user-container.c', 'user-pci.c')) + vfio_ss.add(files('user.c', 'user-container.c', 'user-pci.c')) endif vfio_ss.add(when: 'CONFIG_VFIO_CCW', if_true: files('ccw.c')) diff --git a/hw/vfio/user-pci.c b/hw/vfio/user-pci.c index a06115cd55..7610b47163 100644 --- a/hw/vfio/user-pci.c +++ b/hw/vfio/user-pci.c @@ -18,6 +18,7 @@ #include "hw/pci/pci_bridge.h" #include "hw/qdev-properties.h" #include "hw/qdev-properties-system.h" +#include "hw/vfio/user.h" #include "migration/vmstate.h" #include "qapi/qmp/qdict.h" #include "qemu/error-report.h" @@ -63,6 +64,8 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp) VFIOPCIDevice *vdev = VFIO_PCI_BASE(pdev); VFIODevice *vbasedev = &vdev->vbasedev; AddressSpace *as; + SocketAddress addr; + VFIOUserProxy *proxy; /* * TODO: make option parser understand SocketAddress @@ -75,6 +78,15 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp) return; } + memset(&addr, 0, sizeof(addr)); + addr.type = SOCKET_ADDRESS_TYPE_UNIX; + addr.u.q_unix.path = udev->sock_name; + proxy = vfio_user_connect_dev(&addr, errp); + if (!proxy) { + return; + } + vbasedev->proxy = proxy; + vbasedev->name = g_strdup_printf("VFIO user <%s>", udev->sock_name); vbasedev->ops = &vfio_user_pci_ops; vbasedev->type = VFIO_DEVICE_TYPE_PCI; @@ -123,8 +135,13 @@ static void vfio_user_instance_init(Object *obj) static void vfio_user_instance_finalize(Object *obj) { VFIOPCIDevice *vdev = VFIO_PCI_BASE(obj); + VFIODevice *vbasedev = &vdev->vbasedev; vfio_pci_put_device(vdev); + + if (vbasedev->proxy != NULL) { + vfio_user_disconnect(vbasedev->proxy); + } } static const Property vfio_user_pci_dev_properties[] = { diff --git a/hw/vfio/user.c b/hw/vfio/user.c new file mode 100644 index 0000000000..1c79fb1cb9 --- /dev/null +++ b/hw/vfio/user.c @@ -0,0 +1,171 @@ +/* + * vfio protocol over a UNIX socket. + * + * Copyright © 2018, 2021 Oracle and/or its affiliates. + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + */ + +#include "qemu/osdep.h" +#include +#include + +#include "qemu/error-report.h" +#include "qapi/error.h" +#include "qemu/main-loop.h" +#include "qemu/lockable.h" +#include "hw/hw.h" +#include "hw/vfio/vfio-common.h" +#include "qemu/sockets.h" +#include "io/channel.h" +#include "io/channel-socket.h" +#include "io/channel-util.h" +#include "system/iothread.h" +#include "user.h" + +static IOThread *vfio_user_iothread; + +static void vfio_user_shutdown(VFIOUserProxy *proxy); + + +/* + * Functions called by main, CPU, or iothread threads + */ + +static void vfio_user_shutdown(VFIOUserProxy *proxy) +{ + qio_channel_shutdown(proxy->ioc, QIO_CHANNEL_SHUTDOWN_READ, NULL); + qio_channel_set_aio_fd_handler(proxy->ioc, proxy->ctx, NULL, + proxy->ctx, NULL, NULL); +} + +/* + * Functions only called by iothread + */ + +static void vfio_user_cb(void *opaque) +{ + VFIOUserProxy *proxy = opaque; + + QEMU_LOCK_GUARD(&proxy->lock); + + proxy->state = VFIO_PROXY_CLOSED; + qemu_cond_signal(&proxy->close_cv); +} + + +/* + * Functions called by main or CPU threads + */ + +static QLIST_HEAD(, VFIOUserProxy) vfio_user_sockets = + QLIST_HEAD_INITIALIZER(vfio_user_sockets); + +VFIOUserProxy *vfio_user_connect_dev(SocketAddress *addr, Error **errp) +{ + VFIOUserProxy *proxy; + QIOChannelSocket *sioc; + QIOChannel *ioc; + char *sockname; + + if (addr->type != SOCKET_ADDRESS_TYPE_UNIX) { + error_setg(errp, "vfio_user_connect - bad address family"); + return NULL; + } + sockname = addr->u.q_unix.path; + + sioc = qio_channel_socket_new(); + ioc = QIO_CHANNEL(sioc); + if (qio_channel_socket_connect_sync(sioc, addr, errp)) { + object_unref(OBJECT(ioc)); + return NULL; + } + qio_channel_set_blocking(ioc, false, NULL); + + proxy = g_malloc0(sizeof(VFIOUserProxy)); + proxy->sockname = g_strdup_printf("unix:%s", sockname); + proxy->ioc = ioc; + proxy->flags = VFIO_PROXY_CLIENT; + proxy->state = VFIO_PROXY_CONNECTED; + + qemu_mutex_init(&proxy->lock); + qemu_cond_init(&proxy->close_cv); + + if (vfio_user_iothread == NULL) { + vfio_user_iothread = iothread_create("VFIO user", errp); + } + + proxy->ctx = iothread_get_aio_context(vfio_user_iothread); + + QTAILQ_INIT(&proxy->outgoing); + QTAILQ_INIT(&proxy->incoming); + QTAILQ_INIT(&proxy->free); + QTAILQ_INIT(&proxy->pending); + QLIST_INSERT_HEAD(&vfio_user_sockets, proxy, next); + + return proxy; +} + +void vfio_user_disconnect(VFIOUserProxy *proxy) +{ + VFIOUserMsg *r1, *r2; + + qemu_mutex_lock(&proxy->lock); + + /* our side is quitting */ + if (proxy->state == VFIO_PROXY_CONNECTED) { + vfio_user_shutdown(proxy); + if (!QTAILQ_EMPTY(&proxy->pending)) { + error_printf("vfio_user_disconnect: outstanding requests\n"); + } + } + object_unref(OBJECT(proxy->ioc)); + proxy->ioc = NULL; + + proxy->state = VFIO_PROXY_CLOSING; + QTAILQ_FOREACH_SAFE(r1, &proxy->outgoing, next, r2) { + qemu_cond_destroy(&r1->cv); + QTAILQ_REMOVE(&proxy->outgoing, r1, next); + g_free(r1); + } + QTAILQ_FOREACH_SAFE(r1, &proxy->incoming, next, r2) { + qemu_cond_destroy(&r1->cv); + QTAILQ_REMOVE(&proxy->incoming, r1, next); + g_free(r1); + } + QTAILQ_FOREACH_SAFE(r1, &proxy->pending, next, r2) { + qemu_cond_destroy(&r1->cv); + QTAILQ_REMOVE(&proxy->pending, r1, next); + g_free(r1); + } + QTAILQ_FOREACH_SAFE(r1, &proxy->free, next, r2) { + qemu_cond_destroy(&r1->cv); + QTAILQ_REMOVE(&proxy->free, r1, next); + g_free(r1); + } + + /* + * Make sure the iothread isn't blocking anywhere + * with a ref to this proxy by waiting for a BH + * handler to run after the proxy fd handlers were + * deleted above. + */ + aio_bh_schedule_oneshot(proxy->ctx, vfio_user_cb, proxy); + qemu_cond_wait(&proxy->close_cv, &proxy->lock); + + /* we now hold the only ref to proxy */ + qemu_mutex_unlock(&proxy->lock); + qemu_cond_destroy(&proxy->close_cv); + qemu_mutex_destroy(&proxy->lock); + + QLIST_REMOVE(proxy, next); + if (QLIST_EMPTY(&vfio_user_sockets)) { + iothread_destroy(vfio_user_iothread); + vfio_user_iothread = NULL; + } + + g_free(proxy->sockname); + g_free(proxy); +} diff --git a/hw/vfio/user.h b/hw/vfio/user.h new file mode 100644 index 0000000000..ac7d15dfa8 --- /dev/null +++ b/hw/vfio/user.h @@ -0,0 +1,78 @@ +#ifndef VFIO_USER_H +#define VFIO_USER_H + +/* + * vfio protocol over a UNIX socket. + * + * Copyright © 2018, 2021 Oracle and/or its affiliates. + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the COPYING file in the top-level directory. + * + */ + +typedef struct { + int send_fds; + int recv_fds; + int *fds; +} VFIOUserFDs; + +enum msg_type { + VFIO_MSG_NONE, + VFIO_MSG_ASYNC, + VFIO_MSG_WAIT, + VFIO_MSG_NOWAIT, + VFIO_MSG_REQ, +}; + +typedef struct VFIOUserMsg { + QTAILQ_ENTRY(VFIOUserMsg) next; + VFIOUserFDs *fds; + uint32_t rsize; + uint32_t id; + QemuCond cv; + bool complete; + enum msg_type type; +} VFIOUserMsg; + + +enum proxy_state { + VFIO_PROXY_CONNECTED = 1, + VFIO_PROXY_ERROR = 2, + VFIO_PROXY_CLOSING = 3, + VFIO_PROXY_CLOSED = 4, +}; + +typedef QTAILQ_HEAD(VFIOUserMsgQ, VFIOUserMsg) VFIOUserMsgQ; + +typedef struct VFIOUserProxy { + QLIST_ENTRY(VFIOUserProxy) next; + char *sockname; + struct QIOChannel *ioc; + void (*request)(void *opaque, VFIOUserMsg *msg); + void *req_arg; + int flags; + QemuCond close_cv; + AioContext *ctx; + QEMUBH *req_bh; + + /* + * above only changed when BQL is held + * below are protected by per-proxy lock + */ + QemuMutex lock; + VFIOUserMsgQ free; + VFIOUserMsgQ pending; + VFIOUserMsgQ incoming; + VFIOUserMsgQ outgoing; + VFIOUserMsg *last_nowait; + enum proxy_state state; +} VFIOUserProxy; + +/* VFIOProxy flags */ +#define VFIO_PROXY_CLIENT 0x1 + +VFIOUserProxy *vfio_user_connect_dev(SocketAddress *addr, Error **errp); +void vfio_user_disconnect(VFIOUserProxy *proxy); + +#endif /* VFIO_USER_H */ diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index 50afa944ae..afc67a3a77 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -79,6 +79,7 @@ typedef struct VFIOMigration { } VFIOMigration; struct VFIOGroup; +typedef struct VFIOUserProxy VFIOUserProxy; /* MMU container sub-class for legacy vfio implementation. */ typedef struct VFIOContainer { @@ -162,6 +163,7 @@ typedef struct VFIODevice { IOMMUFDBackend *iommufd; VFIOIOASHwpt *hwpt; QLIST_ENTRY(VFIODevice) hwpt_next; + VFIOUserProxy *proxy; struct vfio_region_info **regions; } VFIODevice;