From nobody Sun Feb 8 01:33:10 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1561577947; cv=none; d=zoho.com; s=zohoarc; b=hMUlrtJ4GHH4FcYoFKn2dB3mjVJ2pltXhsD2aSZSp7Z/8p7LGn9LikNzlNPXupe+jOm6+h18cplG6f1DVG8b3qdgBxEexIvMB2vuXuxGej0QzsHzMTZG0GyVJZq65BjmfKWCwCgIGd5g2JUr+or50mmXsk35o3ETl1H9byZ/rP4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1561577947; 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:ARC-Authentication-Results; bh=Q6gO1azatnxhbSTOerXiZw9LzcaftBrek9dybp81hx8=; b=AdOynLrQUHmxJ872lWwRDSwTGi+BJKnQQ94tucl6uUwMBFYdbtQU/plBpJ/SuzmrU1RpQSbzYtphmZXJA96/u7K+TWH6DhzgO3nua1Mi/f9Hb9i/cSuM54VVWOpynzmLKpddJeaw7TqB8wH85/fqGmtbYPVOg8r4vn4FrCRa0mY= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1561577947095383.10298193945357; Wed, 26 Jun 2019 12:39:07 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 25D6C81F1B; Wed, 26 Jun 2019 19:39:00 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id F1D026012E; Wed, 26 Jun 2019 19:38:59 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id B1CF31806B16; Wed, 26 Jun 2019 19:38:59 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x5QJaVjG012954 for ; Wed, 26 Jun 2019 15:36:31 -0400 Received: by smtp.corp.redhat.com (Postfix) id F17865D71B; Wed, 26 Jun 2019 19:36:30 +0000 (UTC) Received: from mx1.redhat.com (ext-mx13.extmail.prod.ext.phx2.redhat.com [10.5.110.42]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E9B555D717; Wed, 26 Jun 2019 19:36:28 +0000 (UTC) Received: from mail-qt1-f196.google.com (mail-qt1-f196.google.com [209.85.160.196]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DEDE7308213C; Wed, 26 Jun 2019 19:36:19 +0000 (UTC) Received: by mail-qt1-f196.google.com with SMTP id m29so3748526qtu.1; Wed, 26 Jun 2019 12:36:19 -0700 (PDT) Received: from rekt.ibmuc.com ([2804:18:805:41b2:608e:c97b:9c84:d48f]) by smtp.gmail.com with ESMTPSA id k40sm10706658qta.50.2019.06.26.12.36.17 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 26 Jun 2019 12:36:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=flBjUAzqYIc+GrwWM/2w8/+tBktmyWpnBIB9U6Im+U4=; b=L1WeyUcWt0LuPl22ufDPEsddWZhSlO3FFxv5JBiApnFWWqMI/yUwUIkbpgkS31jhgX j0REAdZMqqfQj4kGBv0J5hsXqGB8hGmlAFv4br8PQ8LmJH9vd2pwQDIHIquU/V8zpNWd CKPX5yGeZlYPeLGUtPxYPxH4WSbvvZkfVqzHHZCaox7c4LuY/GUaG7KKYFXG9em1nCQC UCJQTzumQ2PalEUqv2SgmSdV78rS5GNSNhCv/HLFRsN8zONKjv1EPLkFrtHr7J1ptA0Q l5VAQdW6wo/tLoPguyy1YVwdt08GfSVl6DoSFZBjLEF7EOJsR+unix0k/0+eM+YY/PRY BFcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=flBjUAzqYIc+GrwWM/2w8/+tBktmyWpnBIB9U6Im+U4=; b=jTTJHx1s5sN2qlgiqEVm1I2NbaStEwpHqPh0vOwkWQU8bca6mTxWleBnf0dQUmN3ME UNQukYANjwHLLxcghRYBHKBwY6RAhlty7ZtK8bUPdUaNJCBU02p+XFMCK1fsW3/Nt58E EqTGVjJ69SO5Q54Esahf8Eq40zMQIA2J4+RQKjwon3MfBpnEoC9Z2HL0acSb8SU8+9Xk gAHTbE3DsyOapqPdNbyZrZ9yZviAkBORAIbi+gkdqioxIIFMqQQxhZyPbE7v8/SpFiET 1r1VSiW2se6yTUn9CdN5vHAx9ktSpYepcB4rVxbdr1xJKlUTZ0wnX6pJTcpF/FaN+3lU rTzg== X-Gm-Message-State: APjAAAXH04svt8AwpTVKWyJZJVuEF+xNaaH3evOoPsYsMFYednn5KfwO HSyfK9BinNvc8ftNPTdzZBqE+DEBWvw= X-Google-Smtp-Source: APXvYqzp3B53X5aotaY3UI2IJKpNL2zs+YT88fncqDCOSztP7qUFDL798H0fXWoAbQrz1XHjbnftvQ== X-Received: by 2002:a0c:d94e:: with SMTP id t14mr4961583qvj.18.1561577778882; Wed, 26 Jun 2019 12:36:18 -0700 (PDT) From: Daniel Henrique Barboza To: libvir-list@redhat.com Date: Wed, 26 Jun 2019 16:35:54 -0300 Message-Id: <20190626193559.24431-6-danielhb413@gmail.com> In-Reply-To: <20190626193559.24431-1-danielhb413@gmail.com> References: <20190626193559.24431-1-danielhb413@gmail.com> MIME-Version: 1.0 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Wed, 26 Jun 2019 19:36:20 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Wed, 26 Jun 2019 19:36:20 +0000 (UTC) for IP:'209.85.160.196' DOMAIN:'mail-qt1-f196.google.com' HELO:'mail-qt1-f196.google.com' FROM:'danielhb413@gmail.com' RCPT:'' X-RedHat-Spam-Score: 0.143 (DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS) 209.85.160.196 mail-qt1-f196.google.com 209.85.160.196 mail-qt1-f196.google.com X-Scanned-By: MIMEDefang 2.84 on 10.5.110.42 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Cc: mprivozn@redhat.com, Daniel Henrique Barboza , laine@laine.org Subject: [libvirt] [PATCH v1 05/10] tests: pci: Mock the iommu groups and vfio X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Wed, 26 Jun 2019 19:39:05 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Shivaprasad G Bhat The iommu group, /dev/vfio/ behaviors of the host are mocked. This patch implements support for multifunction/multiple devices per iommu groups and emulates the /dev/vfio/ file correctly. This code helps adding necessary testcases for pci-hotplug code. Signed-off-by: Shivaprasad G Bhat Signed-off-by: Daniel Henrique Barboza --- tests/virpcimock.c | 177 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 162 insertions(+), 15 deletions(-) diff --git a/tests/virpcimock.c b/tests/virpcimock.c index acd7560f8b..fc8cb92ebd 100644 --- a/tests/virpcimock.c +++ b/tests/virpcimock.c @@ -44,6 +44,8 @@ char *fakerootdir; char *fakesysfspcidir; =20 # define SYSFS_PCI_PREFIX "/sys/bus/pci/" +# define SYSFS_KERNEL_PREFIX "/sys/kernel/" + =20 # define STDERR(...) \ fprintf(stderr, "%s %zu: ", __FUNCTION__, (size_t) __LINE__); \ @@ -128,6 +130,11 @@ struct pciDevice { struct pciDriver *driver; /* Driver attached. NULL if attached to no= driver */ }; =20 +struct pciIommuGroup { + int iommu; + size_t nDevicesBoundToVFIO; /* Indicates the devices in the gro= up */ +}; + struct fdCallback { int fd; char *path; @@ -139,6 +146,9 @@ size_t nPCIDevices =3D 0; struct pciDriver **pciDrivers =3D NULL; size_t nPCIDrivers =3D 0; =20 +struct pciIommuGroup **pciIommuGroups =3D NULL; +size_t npciIommuGroups =3D 0; + struct fdCallback *callbacks =3D NULL; size_t nCallbacks =3D 0; =20 @@ -255,6 +265,13 @@ getrealpath(char **newpath, errno =3D ENOMEM; return -1; } + } else if (STRPREFIX(path, SYSFS_KERNEL_PREFIX)) { + if (virAsprintfQuiet(newpath, "%s/%s", + fakerootdir, + path) < 0) { + errno =3D ENOMEM; + return -1; + } } else { if (VIR_STRDUP_QUIET(*newpath, path) < 0) return -1; @@ -473,6 +490,101 @@ pci_device_autobind(struct pciDevice *dev) return pci_driver_bind(driver, dev); } =20 +static void +pci_iommu_new(int num) +{ + char *iommupath, *kerneldir; + struct pciIommuGroup *iommuGroup; + + if (VIR_ALLOC_QUIET(iommuGroup) < 0) + ABORT_OOM(); + + iommuGroup->iommu =3D num; + iommuGroup->nDevicesBoundToVFIO =3D 0; /* No device bound to vfio by d= efault */ + + if (virAsprintfQuiet(&kerneldir, "%s%s", + fakerootdir, SYSFS_KERNEL_PREFIX) < 0) + ABORT_OOM(); + + if (virAsprintfQuiet(&iommupath, "%s/iommu_groups/%d/devices", kerneld= ir, num) < 0) + ABORT_OOM(); + VIR_FREE(kerneldir); + + if (virFileMakePath(iommupath) < 0) + ABORT("Unable to create: %s", iommupath); + VIR_FREE(iommupath); + + if (VIR_APPEND_ELEMENT_QUIET(pciIommuGroups, npciIommuGroups, iommuGro= up) < 0) + ABORT_OOM(); +} + +static int +pci_vfio_release_iommu(struct pciDevice *device) +{ + char *vfiopath =3D NULL; + int ret =3D -1; + size_t i =3D 0; + + for (i =3D 0; i < npciIommuGroups; i++) { + if (pciIommuGroups[i]->iommu =3D=3D device->iommuGroup) + break; + } + + if (i !=3D npciIommuGroups) { + if (pciIommuGroups[i]->nDevicesBoundToVFIO =3D=3D 0) { + ret =3D 0; + goto cleanup; + } + pciIommuGroups[i]->nDevicesBoundToVFIO--; + if (!pciIommuGroups[i]->nDevicesBoundToVFIO) { + if (virAsprintfQuiet(&vfiopath, "%s/dev/vfio/%d", + fakesysfspcidir, device->iommuGroup) < 0) { + errno =3D ENOMEM; + goto cleanup; + } + if (unlink(vfiopath) < 0) + goto cleanup; + } + } + + ret =3D 0; + cleanup: + VIR_FREE(vfiopath); + return ret; +} + +static int +pci_vfio_lock_iommu(struct pciDevice *device) +{ + char *vfiopath =3D NULL; + int ret =3D -1; + size_t i =3D 0; + int fd =3D -1; + + for (i =3D 0; i < npciIommuGroups; i++) { + if (pciIommuGroups[i]->iommu =3D=3D device->iommuGroup) + break; + } + + if (i !=3D npciIommuGroups) { + if (!pciIommuGroups[i]->nDevicesBoundToVFIO) { + if (virAsprintfQuiet(&vfiopath, "%s/dev/vfio/%d", + fakesysfspcidir, device->iommuGroup) < 0) { + errno =3D ENOMEM; + goto cleanup; + } + if ((fd =3D real_open(vfiopath, O_CREAT)) < 0) + goto cleanup; + } + pciIommuGroups[i]->nDevicesBoundToVFIO++; + } + + ret =3D 0; + cleanup: + real_close(fd); + VIR_FREE(vfiopath); + return ret; +} =20 /* * PCI Driver functions @@ -626,6 +738,10 @@ pci_driver_bind(struct pciDriver *driver, if (symlink(devpath, driverpath) < 0) goto cleanup; =20 + if (STREQ(driver->name, "vfio-pci")) + if (pci_vfio_lock_iommu(dev) < 0) + goto cleanup; + dev->driver =3D driver; ret =3D 0; cleanup: @@ -660,6 +776,10 @@ pci_driver_unbind(struct pciDriver *driver, unlink(driverpath) < 0) goto cleanup; =20 + if (STREQ(driver->name, "vfio-pci")) + if (pci_vfio_release_iommu(dev) < 0) + goto cleanup; + dev->driver =3D NULL; ret =3D 0; cleanup: @@ -858,6 +978,8 @@ init_syms(void) static void init_env(void) { + char *devVFIO; + if (fakerootdir && fakesysfspcidir) return; =20 @@ -873,6 +995,29 @@ init_env(void) =20 make_file(fakesysfspcidir, "drivers_probe", NULL, -1); =20 + if (virAsprintfQuiet(&devVFIO, "%s/dev/vfio", fakesysfspcidir) < 0) + ABORT_OOM(); + + if (virFileMakePath(devVFIO) < 0) + ABORT("Unable to create: %s", devVFIO); + + /* Create /dev/vfio/vfio file */ + make_file(devVFIO, "vfio", NULL, -1); + VIR_FREE(devVFIO); + + pci_iommu_new(0); + pci_iommu_new(1); + pci_iommu_new(2); + pci_iommu_new(3); + pci_iommu_new(4); + pci_iommu_new(5); + pci_iommu_new(6); + pci_iommu_new(7); + pci_iommu_new(8); + pci_iommu_new(9); + pci_iommu_new(10); + pci_iommu_new(11); + # define MAKE_PCI_DRIVER(name, ...) \ pci_driver_new(name, 0, __VA_ARGS__, -1, -1) =20 @@ -888,20 +1033,21 @@ init_env(void) pci_device_new_from_stub(&dev); \ } while (0) =20 - MAKE_PCI_DEVICE("0000:00:00.0", 0x8086, 0x0044); - MAKE_PCI_DEVICE("0000:00:01.0", 0x8086, 0x0044); - MAKE_PCI_DEVICE("0000:00:02.0", 0x8086, 0x0046); - MAKE_PCI_DEVICE("0000:00:03.0", 0x8086, 0x0048); - MAKE_PCI_DEVICE("0001:00:00.0", 0x1014, 0x03b9, .klass =3D 0x060400); - MAKE_PCI_DEVICE("0001:01:00.0", 0x8086, 0x105e, .iommuGroup =3D 0); - MAKE_PCI_DEVICE("0001:01:00.1", 0x8086, 0x105e, .iommuGroup =3D 0); - MAKE_PCI_DEVICE("0005:80:00.0", 0x10b5, 0x8112, .klass =3D 0x060400); - MAKE_PCI_DEVICE("0005:90:01.0", 0x1033, 0x0035, .iommuGroup =3D 1); - MAKE_PCI_DEVICE("0005:90:01.1", 0x1033, 0x0035, .iommuGroup =3D 1); - MAKE_PCI_DEVICE("0005:90:01.2", 0x1033, 0x00e0, .iommuGroup =3D 1); - MAKE_PCI_DEVICE("0000:0a:01.0", 0x8086, 0x0047); - MAKE_PCI_DEVICE("0000:0a:02.0", 0x8286, 0x0048); - MAKE_PCI_DEVICE("0000:0a:03.0", 0x8386, 0x0048); + MAKE_PCI_DEVICE("0000:00:00.0", 0x8086, 0x0044, .iommuGroup =3D 0); + MAKE_PCI_DEVICE("0000:00:01.0", 0x8086, 0x0044, .iommuGroup =3D 1); + MAKE_PCI_DEVICE("0000:00:02.0", 0x8086, 0x0046, .iommuGroup =3D 2); + MAKE_PCI_DEVICE("0000:00:03.0", 0x8086, 0x0048, .iommuGroup =3D 3); + MAKE_PCI_DEVICE("0001:00:00.0", 0x1014, 0x03b9, .klass =3D 0x060400, .= iommuGroup =3D 4); + MAKE_PCI_DEVICE("0001:01:00.0", 0x8086, 0x105e, .iommuGroup =3D 5); + MAKE_PCI_DEVICE("0001:01:00.1", 0x8086, 0x105e, .iommuGroup =3D 5); + MAKE_PCI_DEVICE("0005:80:00.0", 0x10b5, 0x8112, .klass =3D 0x060400, .= iommuGroup =3D 6); + MAKE_PCI_DEVICE("0005:90:01.0", 0x1033, 0x0035, .iommuGroup =3D 7); + MAKE_PCI_DEVICE("0005:90:01.1", 0x1033, 0x0035, .iommuGroup =3D 7); + MAKE_PCI_DEVICE("0005:90:01.2", 0x1033, 0x00e0, .iommuGroup =3D 7); + MAKE_PCI_DEVICE("0005:90:01.3", 0x1033, 0x00e0, .iommuGroup =3D 7); + MAKE_PCI_DEVICE("0000:0a:01.0", 0x8086, 0x0047, .iommuGroup =3D 8); + MAKE_PCI_DEVICE("0000:0a:02.0", 0x8286, 0x0048, .iommuGroup =3D 8); + MAKE_PCI_DEVICE("0000:0a:03.0", 0x8386, 0x0048, .iommuGroup =3D 8); } =20 =20 @@ -985,7 +1131,8 @@ opendir(const char *path) =20 init_syms(); =20 - if (STRPREFIX(path, SYSFS_PCI_PREFIX) && + if ((STRPREFIX(path, SYSFS_PCI_PREFIX) || + STRPREFIX(path, SYSFS_KERNEL_PREFIX)) && getrealpath(&newpath, path) < 0) return NULL; =20 --=20 2.20.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list