From patchwork Wed Jan 19 21:41:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jag Raman X-Patchwork-Id: 12717801 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 9A640C433F5 for ; Wed, 19 Jan 2022 21:59:44 +0000 (UTC) Received: from localhost ([::1]:52050 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nAIzX-0006uY-OC for qemu-devel@archiver.kernel.org; Wed, 19 Jan 2022 16:59:43 -0500 Received: from eggs.gnu.org ([209.51.188.92]:58350) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nAIjQ-00083I-Or for qemu-devel@nongnu.org; Wed, 19 Jan 2022 16:43:07 -0500 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]:17258) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nAIjJ-0007LM-2V for qemu-devel@nongnu.org; Wed, 19 Jan 2022 16:43:02 -0500 Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 20JJPAPf032601; Wed, 19 Jan 2022 21:42:55 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=hcAm8Ou2NGwtdnTeIh2r4xjRA/UjBUC9LZPE1HxivcM=; b=p39sTncQJC6LxJnFSxtwGLtbxvQ11Dgy7RqYzxv7z3oq1QIpJLGEkZ1GSgyy/pG+3I0R FfEMEkhrtakPDccoiVUGzwq0nPQHPpQVhsg0tkoIxS51yT+gpxJcQzRSJu1XnlNLepJr 5cN6eqPo0gsIxwpyNdvVDr+XtDSON8ViQ6HQ4uWfiV5SDH1mHKrrRWUuDM1f9kqkNXML t2opeebusjghz7QV3x8+QRAPXFXpXUB8IpNur3TGF5MuWig663wmnMBG+5touZeXkGAF Cyn0eH6ZXjilf7LTZw5RKaeXWBf+CcmV2XjAPFvnbnQdBFfEJihzZaj5eUDdSnYi2oZq XA== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by mx0b-00069f02.pphosted.com with ESMTP id 3dnc4q6hxp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 19 Jan 2022 21:42:54 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 20JLautH147107; Wed, 19 Jan 2022 21:42:54 GMT Received: from nam12-dm6-obe.outbound.protection.outlook.com (mail-dm6nam12lp2177.outbound.protection.outlook.com [104.47.59.177]) by aserp3030.oracle.com with ESMTP id 3dkmaeb6v5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 19 Jan 2022 21:42:53 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lVmVLJy4fQiZx/GALP94wniXVWnIPbqkz8T9ZbfgoFMpUuhDBmeTLGzmb1dIDoFixG/QEO/St8vU8K6lejlT62w/S0a0Ufh/S2ntaiP7kG1s0BWAF+4kCXFM3jONzGeFtGmD4yemdTceN2a+jLS1vuTP1RhW19rmehAIkIO9Q9L0eEKDfnToEzuVkV3Uq2TgJXu5vH1pArHP7W8Z7bUbdbIN10aeHVfeBZ4ReBJgJgwnKPiz/uYKACiNBIsimnmiuwysIrkqka/1AgSOnZlZ4nJ2hQDeG8luJ11srVhUsqFvVHdZE5m/JwkA0vun4SCSe580wB69JYJM7N/Wldve2Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=hcAm8Ou2NGwtdnTeIh2r4xjRA/UjBUC9LZPE1HxivcM=; b=b+KSH2GzPKkKahNSCg9pc1WO+qHBF6gjLLSR7DYQl5w88xqXdGjJKCzB62hlOd7QsIdDsh6MoZlRgoScEVdna9fGdmbi7lMJr8iQ/NIk9GIrY+6DW8SPDVyChYDzW7NRY1jRncMX6RSR2GJTDblCqBmuhG5u5poiL40W2AFY6LRWbEnszK/j5yXyNjIqlYNfeicIvERjejbeH2N/kYkvDx7yGQfANlYje+8qhs/vMSRINCcSxfiqz6RnpEni7PvSZRIn/oAGY56b/dYAlyCBzvWClpxcdIm1uSuNt2jN/Up/tjc2gULcibdotg34pbrb9q7j/NFlaUB6rzy1p+PtNQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=hcAm8Ou2NGwtdnTeIh2r4xjRA/UjBUC9LZPE1HxivcM=; b=BORxLFyig4nMUUjZs1uHfScqMMR3BBgfS5MiuMDPBpI+eh51JBiW3PgPST937cMfw32UPa51W9AB0L2M7X2nKbs6EAK0wVwwJdKDhN8vImctuFT1l/mQ5UqBDWy3xH4W8DhZ+PRxdxriskerDs/9FLCIDozKn5IZHcJM4fUKw7c= Received: from MN2PR10MB4013.namprd10.prod.outlook.com (2603:10b6:208:185::25) by SA1PR10MB5887.namprd10.prod.outlook.com (2603:10b6:806:22b::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4909.7; Wed, 19 Jan 2022 21:42:52 +0000 Received: from MN2PR10MB4013.namprd10.prod.outlook.com ([fe80::98b1:4348:5117:5fc]) by MN2PR10MB4013.namprd10.prod.outlook.com ([fe80::98b1:4348:5117:5fc%7]) with mapi id 15.20.4888.014; Wed, 19 Jan 2022 21:42:52 +0000 From: Jagannathan Raman To: qemu-devel@nongnu.org Subject: [PATCH v5 10/18] vfio-user: instantiate vfio-user context Date: Wed, 19 Jan 2022 16:41:59 -0500 Message-Id: <66bb8c18bb5372f72aaa66d62f09258ec7775100.1642626515.git.jag.raman@oracle.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: References: X-ClientProxiedBy: BYAPR08CA0043.namprd08.prod.outlook.com (2603:10b6:a03:117::20) To MN2PR10MB4013.namprd10.prod.outlook.com (2603:10b6:208:185::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 9d7daa0f-1e26-4d0f-5241-08d9db94a4f0 X-MS-TrafficTypeDiagnostic: SA1PR10MB5887:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:179; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: m9Nq7x5OE8K9QcgXbJ2fxhLCO4Dm92LlpDrwWEPu7AbuUveOq+D0XE7NmjggwdXEJxLPeYtOXA3+kV7wfE24lBEwM+3nBr2yvbnJvQbDJCfuX0BV9q9rnQwisMBfb8ShmhRYzq0E/EPTBrdWCBwXY/IIWkxlQu0as+KVHmnFajXX2PHLqKFWGq03KSN+J5qzqu7TOPIX0Goy81doKkAFzm4NCVZtyieaCC8/4jXAgCSwywmSJkPW3lLCm7OhR71WjpaHev9FeJEpDB1mas5MeByJNQw+qtEUCsy5mCYG6lWfJsMLDkTM4No5ttAo2iks3Rz5y0zPFikMUsF4Vb9guLHjupxbB4KbbJLTXjuBG01MKDFofjxwB1Bs/fbJz7OeihedfmtX1iUjaiU3wBa1Yi3BF8DYFpXzugYkqW5cuX3rLRO9XW075h/B12XXqRlXt6zIQk9ffZri60AjWz3SzJlWLHZL6/qXjigSpsMf3eP8Ei5eGhlFh8l7lBVD4205bViD2jnp2z+IYWZjpucNsFhKhGMs8X0HRRxgCWen05+cmtZR3z0ZTOlr6Rc97IQ1MRPG//VxzxdmOoGFHNMfKJKWce9gwTkiK4NouwMTfhpAaDANb0kC7d6AYNMSqDKbReL2ff1o62eT0EDejh+GJqI7zrvFa1Qri8diVOUkqFaEqVpnwlzpOLCAYd/po2+5qQTry1OeQvDt5ZN8CSKJXQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:MN2PR10MB4013.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(366004)(5660300002)(186003)(6916009)(508600001)(66946007)(66556008)(26005)(7416002)(4326008)(66476007)(86362001)(107886003)(8936002)(38100700002)(38350700002)(8676002)(36756003)(83380400001)(6486002)(2616005)(6512007)(2906002)(6506007)(316002)(52116002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: v9vwbkLHFUMexE61toApt/NvM7SkHhJMF7slK/NwoUJsp565uN3yJkSRk4xDgMXcrxQSfr0aVIXOghNagpgTfnca0syq/6ycBiIXneqzn8n1qTlFDr7QwzhSBSIL5cQ8uKHbvIcTQrJgSUUU76mLnmmIngHY4VkweDN8xa2RyssxVO7SLZ+8iv7EosBo6GgbNq1BH+rB7+skkkvdYZdWMYtj+2zlCQ5makb8m54Vo6mN+wUKWC+v+HYrxnkuv72+Sez0E2bP/q5QiJgEFKzIRpD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k5oUusl77dJ7ACZZZBXgscoPPfwQDtDgkPuyCzSPx+ty4Lq4qOT4oMabHqUPT5LVSX6zHhThXzJFRYAAkXQwIKJ62R1HETsiY++DZ0QSw7gMmGJOEOXuEu29U51wuNj66QBfNjTzHvb3yQUuITahfCuMTif2aAMOn3GFUqmEqsy6X++Jpl4wab0AVYVlf+NWgK+sDoSlqNI9tUnr3/6+oNp0/mJuskhdP1O2JrLQiWNT0G3xCw+T60J/OyiuUy5szU+2MohB2xDIbD7l3tEspB546FHHG4OuJkg3x2U= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9d7daa0f-1e26-4d0f-5241-08d9db94a4f0 X-MS-Exchange-CrossTenant-AuthSource: MN2PR10MB4013.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Jan 2022 21:42:51.9897 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: xXzQiDx9QYJ5Y1kG4Qb/Bu1oF6F3bpSn5HOEEZ1IrLzwh08L3n0Or3eJ1Cum2VWqeetYJo5hPNcb6f0TKA8hlg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR10MB5887 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10232 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 mlxscore=0 suspectscore=0 phishscore=0 malwarescore=0 mlxlogscore=999 spamscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2201190117 X-Proofpoint-GUID: _LP2CEu6Z8rrBLVbx2JLdQhpo8Sjw1X1 X-Proofpoint-ORIG-GUID: _LP2CEu6Z8rrBLVbx2JLdQhpo8Sjw1X1 Received-SPF: pass client-ip=205.220.177.32; envelope-from=jag.raman@oracle.com; helo=mx0b-00069f02.pphosted.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, 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, 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: , Cc: eduardo@habkost.net, elena.ufimtseva@oracle.com, john.g.johnson@oracle.com, berrange@redhat.com, bleal@redhat.com, john.levon@nutanix.com, mst@redhat.com, armbru@redhat.com, quintela@redhat.com, f4bug@amsat.org, marcandre.lureau@gmail.com, stefanha@redhat.com, thanos.makatos@nutanix.com, pbonzini@redhat.com, jag.raman@oracle.com, eblake@redhat.com, dgilbert@redhat.com Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" create a context with the vfio-user library to run a PCI device Signed-off-by: Elena Ufimtseva Signed-off-by: John G Johnson Signed-off-by: Jagannathan Raman --- hw/remote/vfio-user-obj.c | 78 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/hw/remote/vfio-user-obj.c b/hw/remote/vfio-user-obj.c index 80757b0029..810a7c3943 100644 --- a/hw/remote/vfio-user-obj.c +++ b/hw/remote/vfio-user-obj.c @@ -41,6 +41,9 @@ #include "hw/remote/machine.h" #include "qapi/error.h" #include "qapi/qapi-visit-sockets.h" +#include "qemu/notify.h" +#include "sysemu/sysemu.h" +#include "libvfio-user.h" #define TYPE_VFU_OBJECT "x-vfio-user-server" OBJECT_DECLARE_TYPE(VfuObject, VfuObjectClass, VFU_OBJECT) @@ -82,13 +85,23 @@ struct VfuObject { char *device; Error *err; + + Notifier machine_done; + + vfu_ctx_t *vfu_ctx; }; +static void vfu_object_init_ctx(VfuObject *o, Error **errp); + static void vfu_object_set_socket(Object *obj, Visitor *v, const char *name, void *opaque, Error **errp) { VfuObject *o = VFU_OBJECT(obj); + if (o->vfu_ctx) { + return; + } + qapi_free_SocketAddress(o->socket); o->socket = NULL; @@ -104,17 +117,68 @@ static void vfu_object_set_socket(Object *obj, Visitor *v, const char *name, } trace_vfu_prop("socket", o->socket->u.q_unix.path); + + vfu_object_init_ctx(o, errp); } static void vfu_object_set_device(Object *obj, const char *str, Error **errp) { VfuObject *o = VFU_OBJECT(obj); + if (o->vfu_ctx) { + return; + } + g_free(o->device); o->device = g_strdup(str); trace_vfu_prop("device", str); + + vfu_object_init_ctx(o, errp); +} + +/* + * TYPE_VFU_OBJECT depends on the availability of the 'socket' and 'device' + * properties. It also depends on devices instantiated in QEMU. These + * dependencies are not available during the instance_init phase of this + * object's life-cycle. As such, the server is initialized after the + * machine is setup. machine_init_done_notifier notifies TYPE_VFU_OBJECT + * when the machine is setup, and the dependencies are available. + */ +static void vfu_object_machine_done(Notifier *notifier, void *data) +{ + VfuObject *o = container_of(notifier, VfuObject, machine_done); + Error *err = NULL; + + vfu_object_init_ctx(o, &err); + + if (err) { + error_propagate(&error_abort, err); + } +} + +static void vfu_object_init_ctx(VfuObject *o, Error **errp) +{ + ERRP_GUARD(); + + if (o->vfu_ctx || !o->socket || !o->device || + !phase_check(PHASE_MACHINE_READY)) { + return; + } + + if (o->err) { + error_propagate(errp, o->err); + o->err = NULL; + return; + } + + o->vfu_ctx = vfu_create_ctx(VFU_TRANS_SOCK, o->socket->u.q_unix.path, 0, + o, VFU_DEV_TYPE_PCI); + if (o->vfu_ctx == NULL) { + error_setg(errp, "vfu: Failed to create context - %s", strerror(errno)); + return; + } } static void vfu_object_init(Object *obj) @@ -124,6 +188,11 @@ static void vfu_object_init(Object *obj) k->nr_devs++; + if (!phase_check(PHASE_MACHINE_READY)) { + o->machine_done.notify = vfu_object_machine_done; + qemu_add_machine_init_done_notifier(&o->machine_done); + } + 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); @@ -142,6 +211,10 @@ static void vfu_object_finalize(Object *obj) o->socket = NULL; + if (o->vfu_ctx) { + vfu_destroy_ctx(o->vfu_ctx); + } + g_free(o->device); o->device = NULL; @@ -149,6 +222,11 @@ static void vfu_object_finalize(Object *obj) if (!k->nr_devs && k->auto_shutdown) { qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN); } + + if (o->machine_done.notify) { + qemu_remove_machine_init_done_notifier(&o->machine_done); + o->machine_done.notify = NULL; + } } static void vfu_object_class_init(ObjectClass *klass, void *data)