From: Raghavendra Rao Ananta <rananta@google.com>
To: David Matlack <dmatlack@google.com>,
Alex Williamson <alex@shazbot.org>,
Alex Williamson <alex.williamson@redhat.com>
Cc: Josh Hilke <jrhilke@google.com>,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
Raghavendra Rao Ananta <rananta@google.com>
Subject: [PATCH 1/4] vfio: selftests: Add support for passing vf_token in device init
Date: Tue, 4 Nov 2025 00:35:33 +0000 [thread overview]
Message-ID: <20251104003536.3601931-2-rananta@google.com> (raw)
In-Reply-To: <20251104003536.3601931-1-rananta@google.com>
A UUID is normally set as a vf_token to correspond the VFs with the
PFs, if they are both bound by the vfio-pci driver. This is true for
iommufd-based approach and container-based approach. The token can be
set either during device creation (VFIO_GROUP_GET_DEVICE_FD) in
container-based approach or during iommu bind (VFIO_DEVICE_BIND_IOMMUFD)
in the iommu-fd case. Hence, extend the vfio_pci_device_init() helper to
accept vf_token during device setup.
The tests depending on vfio_pci_device_init() are adjusted accordingly
and no functional changes are expected. A later patch will add tests
that passes actual token to test the UAPI.
Signed-off-by: Raghavendra Rao Ananta <rananta@google.com>
---
.../selftests/vfio/lib/include/vfio_util.h | 4 +-
tools/testing/selftests/vfio/lib/libvfio.mk | 4 +-
.../selftests/vfio/lib/vfio_pci_device.c | 60 ++++++++++++++++---
.../selftests/vfio/vfio_dma_mapping_test.c | 2 +-
.../selftests/vfio/vfio_pci_device_test.c | 4 +-
.../selftests/vfio/vfio_pci_driver_test.c | 4 +-
6 files changed, 62 insertions(+), 16 deletions(-)
diff --git a/tools/testing/selftests/vfio/lib/include/vfio_util.h b/tools/testing/selftests/vfio/lib/include/vfio_util.h
index ed31606e01b78..b01068d98fdab 100644
--- a/tools/testing/selftests/vfio/lib/include/vfio_util.h
+++ b/tools/testing/selftests/vfio/lib/include/vfio_util.h
@@ -202,7 +202,9 @@ const char *vfio_pci_get_cdev_path(const char *bdf);
extern const char *default_iommu_mode;
-struct vfio_pci_device *vfio_pci_device_init(const char *bdf, const char *iommu_mode);
+struct vfio_pci_device *vfio_pci_device_init(const char *bdf,
+ const char *iommu_mode,
+ const char *vf_token);
void vfio_pci_device_cleanup(struct vfio_pci_device *device);
void vfio_pci_device_reset(struct vfio_pci_device *device);
diff --git a/tools/testing/selftests/vfio/lib/libvfio.mk b/tools/testing/selftests/vfio/lib/libvfio.mk
index 5d11c3a89a28e..2dc85c41ffb4b 100644
--- a/tools/testing/selftests/vfio/lib/libvfio.mk
+++ b/tools/testing/selftests/vfio/lib/libvfio.mk
@@ -18,7 +18,9 @@ $(shell mkdir -p $(LIBVFIO_O_DIRS))
CFLAGS += -I$(VFIO_DIR)/lib/include
+LDLIBS += -luuid
+
$(LIBVFIO_O): $(OUTPUT)/%.o : $(VFIO_DIR)/%.c
- $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
+ $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< $(LDLIBS) -o $@
EXTRA_CLEAN += $(LIBVFIO_O)
diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
index 0921b2451ba5c..3f7be8d371d06 100644
--- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c
+++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
@@ -11,6 +11,7 @@
#include <sys/mman.h>
#include <uapi/linux/types.h>
+#include <uuid/uuid.h>
#include <linux/limits.h>
#include <linux/mman.h>
#include <linux/types.h>
@@ -22,6 +23,8 @@
#define PCI_SYSFS_PATH "/sys/bus/pci/devices"
+#define VF_TOKEN_ARG "vf_token="
+
#define ioctl_assert(_fd, _op, _arg) do { \
void *__arg = (_arg); \
int __ret = ioctl((_fd), (_op), (__arg)); \
@@ -328,7 +331,37 @@ static void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf
ioctl_assert(device->group_fd, VFIO_GROUP_SET_CONTAINER, &device->container_fd);
}
-static void vfio_pci_container_setup(struct vfio_pci_device *device, const char *bdf)
+static void vfio_pci_container_get_device_fd(struct vfio_pci_device *device,
+ const char *bdf,
+ const char *vf_token)
+{
+ char *arg = (char *) bdf;
+
+ /*
+ * If a vf_token exists, argument to VFIO_GROUP_GET_DEVICE_FD
+ * will be in the form of the following example:
+ * "0000:04:10.0 vf_token=bd8d9d2b-5a5f-4f5a-a211-f591514ba1f3"
+ */
+ if (vf_token) {
+ size_t sz = strlen(bdf) + strlen(" "VF_TOKEN_ARG) +
+ strlen(vf_token) + 1;
+
+ arg = calloc(1, sz);
+ VFIO_ASSERT_NOT_NULL(arg);
+
+ snprintf(arg, sz, "%s %s%s", bdf, VF_TOKEN_ARG, vf_token);
+ }
+
+ device->fd = ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, arg);
+
+ if (vf_token)
+ free((void *) arg);
+
+ VFIO_ASSERT_GE(device->fd, 0);
+}
+
+static void vfio_pci_container_setup(struct vfio_pci_device *device,
+ const char *bdf, const char *vf_token)
{
unsigned long iommu_type = device->iommu_mode->iommu_type;
const char *path = device->iommu_mode->container_path;
@@ -348,8 +381,7 @@ static void vfio_pci_container_setup(struct vfio_pci_device *device, const char
ioctl_assert(device->container_fd, VFIO_SET_IOMMU, (void *)iommu_type);
- device->fd = ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, bdf);
- VFIO_ASSERT_GE(device->fd, 0);
+ vfio_pci_container_get_device_fd(device, bdf, vf_token);
}
static void vfio_pci_device_setup(struct vfio_pci_device *device)
@@ -456,12 +488,19 @@ static const struct vfio_iommu_mode *lookup_iommu_mode(const char *iommu_mode)
VFIO_FAIL("Unrecognized IOMMU mode: %s\n", iommu_mode);
}
-static void vfio_device_bind_iommufd(int device_fd, int iommufd)
+static void vfio_device_bind_iommufd(int device_fd, int iommufd, const char *vf_token)
{
struct vfio_device_bind_iommufd args = {
.argsz = sizeof(args),
.iommufd = iommufd,
};
+ uuid_t token_uuid = {0};
+
+ if (vf_token) {
+ VFIO_ASSERT_EQ(uuid_parse(vf_token, token_uuid), 0);
+ args.flags = VFIO_DEVICE_BIND_FLAG_TOKEN;
+ args.token_uuid_ptr = (u64) token_uuid;
+ }
ioctl_assert(device_fd, VFIO_DEVICE_BIND_IOMMUFD, &args);
}
@@ -486,7 +525,8 @@ static void vfio_device_attach_iommufd_pt(int device_fd, u32 pt_id)
ioctl_assert(device_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &args);
}
-static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, const char *bdf)
+static void vfio_pci_iommufd_setup(struct vfio_pci_device *device,
+ const char *bdf, const char *vf_token)
{
const char *cdev_path = vfio_pci_get_cdev_path(bdf);
@@ -502,12 +542,14 @@ static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, const char *b
device->iommufd = open("/dev/iommu", O_RDWR);
VFIO_ASSERT_GT(device->iommufd, 0);
- vfio_device_bind_iommufd(device->fd, device->iommufd);
+ vfio_device_bind_iommufd(device->fd, device->iommufd, vf_token);
device->ioas_id = iommufd_ioas_alloc(device->iommufd);
vfio_device_attach_iommufd_pt(device->fd, device->ioas_id);
}
-struct vfio_pci_device *vfio_pci_device_init(const char *bdf, const char *iommu_mode)
+struct vfio_pci_device *vfio_pci_device_init(const char *bdf,
+ const char *iommu_mode,
+ const char *vf_token)
{
struct vfio_pci_device *device;
@@ -519,9 +561,9 @@ struct vfio_pci_device *vfio_pci_device_init(const char *bdf, const char *iommu_
device->iommu_mode = lookup_iommu_mode(iommu_mode);
if (device->iommu_mode->container_path)
- vfio_pci_container_setup(device, bdf);
+ vfio_pci_container_setup(device, bdf, vf_token);
else
- vfio_pci_iommufd_setup(device, bdf);
+ vfio_pci_iommufd_setup(device, bdf, vf_token);
vfio_pci_device_setup(device);
vfio_pci_driver_probe(device);
diff --git a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
index ab19c54a774da..3c53b808f7f87 100644
--- a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
+++ b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
@@ -114,7 +114,7 @@ FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous_hugetlb_1gb, SZ_1G, MAP_HUGETLB |
FIXTURE_SETUP(vfio_dma_mapping_test)
{
- self->device = vfio_pci_device_init(device_bdf, variant->iommu_mode);
+ self->device = vfio_pci_device_init(device_bdf, variant->iommu_mode, NULL);
}
FIXTURE_TEARDOWN(vfio_dma_mapping_test)
diff --git a/tools/testing/selftests/vfio/vfio_pci_device_test.c b/tools/testing/selftests/vfio/vfio_pci_device_test.c
index 7a270698e4d24..ebf7fd3d1cf70 100644
--- a/tools/testing/selftests/vfio/vfio_pci_device_test.c
+++ b/tools/testing/selftests/vfio/vfio_pci_device_test.c
@@ -28,7 +28,7 @@ FIXTURE(vfio_pci_device_test) {
FIXTURE_SETUP(vfio_pci_device_test)
{
- self->device = vfio_pci_device_init(device_bdf, default_iommu_mode);
+ self->device = vfio_pci_device_init(device_bdf, default_iommu_mode, NULL);
}
FIXTURE_TEARDOWN(vfio_pci_device_test)
@@ -116,7 +116,7 @@ FIXTURE_VARIANT_ADD(vfio_pci_irq_test, msix) {
FIXTURE_SETUP(vfio_pci_irq_test)
{
- self->device = vfio_pci_device_init(device_bdf, default_iommu_mode);
+ self->device = vfio_pci_device_init(device_bdf, default_iommu_mode, NULL);
}
FIXTURE_TEARDOWN(vfio_pci_irq_test)
diff --git a/tools/testing/selftests/vfio/vfio_pci_driver_test.c b/tools/testing/selftests/vfio/vfio_pci_driver_test.c
index 2dbd70b7db627..cfbaa05dda884 100644
--- a/tools/testing/selftests/vfio/vfio_pci_driver_test.c
+++ b/tools/testing/selftests/vfio/vfio_pci_driver_test.c
@@ -71,7 +71,7 @@ FIXTURE_SETUP(vfio_pci_driver_test)
{
struct vfio_pci_driver *driver;
- self->device = vfio_pci_device_init(device_bdf, variant->iommu_mode);
+ self->device = vfio_pci_device_init(device_bdf, variant->iommu_mode, NULL);
driver = &self->device->driver;
@@ -233,7 +233,7 @@ int main(int argc, char *argv[])
device_bdf = vfio_selftests_get_bdf(&argc, argv);
- device = vfio_pci_device_init(device_bdf, default_iommu_mode);
+ device = vfio_pci_device_init(device_bdf, default_iommu_mode, NULL);
if (!device->driver.ops) {
fprintf(stderr, "No driver found for device %s\n", device_bdf);
return KSFT_SKIP;
--
2.51.2.997.g839fc31de9-goog
next prev parent reply other threads:[~2025-11-04 0:35 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-04 0:35 [PATCH 0/4] vfio: selftest: Add SR-IOV UAPI test Raghavendra Rao Ananta
2025-11-04 0:35 ` Raghavendra Rao Ananta [this message]
2025-11-05 23:52 ` [PATCH 1/4] vfio: selftests: Add support for passing vf_token in device init David Matlack
2025-11-06 0:12 ` David Matlack
2025-11-06 16:33 ` Raghavendra Rao Ananta
2025-11-06 16:26 ` Raghavendra Rao Ananta
2025-11-06 17:17 ` David Matlack
2025-11-07 2:46 ` Raghavendra Rao Ananta
2025-11-06 0:14 ` David Matlack
2025-11-06 16:36 ` Raghavendra Rao Ananta
2025-11-06 17:10 ` David Matlack
2025-11-04 0:35 ` [PATCH 2/4] vfio: selftests: Export vfio_pci_device functions Raghavendra Rao Ananta
2025-11-06 0:41 ` David Matlack
2025-11-06 16:43 ` Raghavendra Rao Ananta
2025-11-06 17:08 ` David Matlack
2025-11-04 0:35 ` [PATCH 3/4] vfio: selftests: Add helper to set/override a vf_token Raghavendra Rao Ananta
2025-11-06 0:01 ` David Matlack
2025-11-06 16:44 ` Raghavendra Rao Ananta
2025-11-04 0:35 ` [PATCH 4/4] vfio: selftests: Add tests to validate SR-IOV UAPI Raghavendra Rao Ananta
2025-11-06 1:00 ` David Matlack
2025-11-06 17:05 ` Raghavendra Rao Ananta
2025-11-06 17:34 ` David Matlack
2025-11-07 2:56 ` Raghavendra Rao Ananta
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251104003536.3601931-2-rananta@google.com \
--to=rananta@google.com \
--cc=alex.williamson@redhat.com \
--cc=alex@shazbot.org \
--cc=dmatlack@google.com \
--cc=jrhilke@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.