From patchwork Wed Sep 19 06:28:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tiwei Bie X-Patchwork-Id: 10605373 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id ABE34112B for ; Wed, 19 Sep 2018 06:34:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9B48F2B649 for ; Wed, 19 Sep 2018 06:34:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8EEA22B64E; Wed, 19 Sep 2018 06:34:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 2680A2B649 for ; Wed, 19 Sep 2018 06:34:13 +0000 (UTC) Received: from localhost ([::1]:43872 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g2W3v-0001QC-VZ for patchwork-qemu-devel@patchwork.kernel.org; Wed, 19 Sep 2018 02:34:12 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45363) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g2Vzo-0006ep-S8 for qemu-devel@nongnu.org; Wed, 19 Sep 2018 02:29:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g2Vzn-0002fg-TN for qemu-devel@nongnu.org; Wed, 19 Sep 2018 02:29:56 -0400 Received: from mga11.intel.com ([192.55.52.93]:1984) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g2Vzn-0002dt-JQ for qemu-devel@nongnu.org; Wed, 19 Sep 2018 02:29:55 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 18 Sep 2018 23:29:53 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.53,392,1531810800"; d="scan'208";a="71164034" Received: from btwcube1.sh.intel.com ([10.67.104.151]) by fmsmga007.fm.intel.com with ESMTP; 18 Sep 2018 23:29:46 -0700 From: Tiwei Bie To: mst@redhat.com, alex.williamson@redhat.com, jasowang@redhat.com, qemu-devel@nongnu.org Date: Wed, 19 Sep 2018 14:28:34 +0800 Message-Id: <20180919062834.30103-5-tiwei.bie@intel.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180919062834.30103-1-tiwei.bie@intel.com> References: <20180919062834.30103-1-tiwei.bie@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 Subject: [Qemu-devel] [RFC v2 4/4] vhost-user-bridge: support VFIO container message X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: tiwei.bie@intel.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP This patch introduces the VFIO container message support in vhost-user-bridge. A new option (-G) is added to set the VFIO container for the vhost device. This is mainly used to test the VFIO container message implementation in vhost user. Signed-off-by: Tiwei Bie --- tests/vhost-user-bridge.c | 63 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/tests/vhost-user-bridge.c b/tests/vhost-user-bridge.c index 0884294141..b668462b49 100644 --- a/tests/vhost-user-bridge.c +++ b/tests/vhost-user-bridge.c @@ -35,6 +35,9 @@ #include "standard-headers/linux/virtio_net.h" #include "contrib/libvhost-user/libvhost-user.h" +#include +#include + #define VHOST_USER_BRIDGE_DEBUG 1 #define DPRINT(...) \ @@ -71,6 +74,8 @@ typedef struct VubrDev { void *addr; pthread_t thread; } notifier; + int vfio_container; + int vfio_group; } VubrDev; static void @@ -467,6 +472,12 @@ vubr_queue_set_started(VuDev *dev, int qidx, bool started) qidx * getpagesize()); } + /* We can test setting VFIO container multiple times + * by doing this in queue start */ + if (started && vubr->vfio_container >= 0) { + vu_set_vfio_container(dev, vubr->vfio_container); + } + if (qidx % 2 == 1) { vu_set_queue_handler(dev, vq, started ? vubr_handle_tx : NULL); } @@ -537,6 +548,7 @@ vubr_new(const char *path, bool client) } dev->notifier.fd = -1; + dev->vfio_container = -1; un.sun_family = AF_UNIX; strcpy(un.sun_path, path); @@ -642,6 +654,43 @@ vubr_host_notifier_setup(VubrDev *dev) dev->notifier.thread = thread; } +static void +vubr_vfio_container_setup(VubrDev *dev, const char *vfio_group) +{ + int container_fd; + int group_fd; + + container_fd = open("/dev/vfio/vfio", O_RDWR); + if (container_fd < 0) { + vubr_die("open(/dev/vfio/vfio)"); + } + + group_fd = open(vfio_group, O_RDWR); + if (group_fd < 0) { + vubr_die(vfio_group); + } + + if (ioctl(group_fd, VFIO_GROUP_SET_CONTAINER, &container_fd) < 0) { + vubr_die("ioctl(VFIO_GROUP_SET_CONTAINER)"); + } + + if (ioctl(container_fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) || + ioctl(container_fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1v2_IOMMU)) { + bool v2 = !!ioctl(container_fd, VFIO_CHECK_EXTENSION, + VFIO_TYPE1v2_IOMMU); + + if (ioctl(container_fd, VFIO_SET_IOMMU, v2 ? VFIO_TYPE1v2_IOMMU : + VFIO_TYPE1_IOMMU) < 0) { + vubr_die("ioctl(VFIO_SET_IOMMU)"); + } + } else { + vubr_die("No available IOMMU models"); + } + + dev->vfio_container = container_fd; + dev->vfio_group = group_fd; +} + static void vubr_set_host(struct sockaddr_in *saddr, const char *host) { @@ -757,8 +806,9 @@ main(int argc, char *argv[]) int opt; bool client = false; bool host_notifier = false; + char *vfio_group = NULL; - while ((opt = getopt(argc, argv, "l:r:u:cH")) != -1) { + while ((opt = getopt(argc, argv, "l:r:u:cHG:")) != -1) { switch (opt) { case 'l': @@ -780,6 +830,9 @@ main(int argc, char *argv[]) case 'H': host_notifier = true; break; + case 'G': + vfio_group = optarg; + break; default: goto out; } @@ -799,6 +852,10 @@ main(int argc, char *argv[]) vubr_host_notifier_setup(dev); } + if (vfio_group) { + vubr_vfio_container_setup(dev, vfio_group); + } + vubr_backend_udp_setup(dev, lhost, lport, rhost, rport); vubr_run(dev); @@ -808,7 +865,8 @@ main(int argc, char *argv[]) out: fprintf(stderr, "Usage: %s ", argv[0]); - fprintf(stderr, "[-c] [-H] [-u ud_socket_path] [-l lhost:lport] [-r rhost:rport]\n"); + fprintf(stderr, "[-c] [-H] [-u ud_socket_path] [-l lhost:lport]\n"); + fprintf(stderr, "\t\t[-r rhost:rport] [-G /dev/vfio/GROUP]\n"); fprintf(stderr, "\t-u path to unix doman socket. default: %s\n", DEFAULT_UD_SOCKET); fprintf(stderr, "\t-l local host and port. default: %s:%s\n", @@ -817,6 +875,7 @@ out: DEFAULT_RHOST, DEFAULT_RPORT); fprintf(stderr, "\t-c client mode\n"); fprintf(stderr, "\t-H use host notifier\n"); + fprintf(stderr, "\t-G VFIO group path.\n"); return 1; }