diff mbox series

[RFC,25/33] vfio: selftests: Add [-i iommu_mode] option to all tests

Message ID 20250523233018.1702151-26-dmatlack@google.com
State New
Headers show
Series [RFC,01/33] selftests: Create tools/testing/selftests/vfio | expand

Commit Message

David Matlack May 23, 2025, 11:30 p.m. UTC
Add a new command line option to all tests that allow them to select
which iommu_mode to use. This makes it possible to replicate tests with
different modes and therefore get test coverage of different parts of
the kernel without having to change the tests themselves.

Signed-off-by: David Matlack <dmatlack@google.com>
---
 .../selftests/vfio/lib/include/vfio_util.h    |  1 +
 .../selftests/vfio/lib/vfio_pci_device.c      | 15 ++++++++
 .../selftests/vfio/vfio_dma_mapping_test.c    | 14 +++++---
 .../selftests/vfio/vfio_pci_device_test.c     | 33 ++++++++++++-----
 .../selftests/vfio/vfio_pci_driver_test.c     | 35 ++++++++++++++-----
 5 files changed, 77 insertions(+), 21 deletions(-)
diff mbox series

Patch

diff --git a/tools/testing/selftests/vfio/lib/include/vfio_util.h b/tools/testing/selftests/vfio/lib/include/vfio_util.h
index bff8f550274b..9989528c21b9 100644
--- a/tools/testing/selftests/vfio/lib/include/vfio_util.h
+++ b/tools/testing/selftests/vfio/lib/include/vfio_util.h
@@ -173,6 +173,7 @@  struct vfio_pci_device {
 const char *vfio_pci_get_cdev_path(const char *bdf);
 
 extern const char *default_iommu_mode;
+void iommu_mode_help(const char *flag);
 
 struct vfio_pci_device *vfio_pci_device_init(const char *bdf, const char *iommu_mode);
 void vfio_pci_device_cleanup(struct vfio_pci_device *device);
diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
index 4a95cf0c1b1c..950c83fee3ed 100644
--- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c
+++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
@@ -370,6 +370,21 @@  static const struct vfio_iommu_mode iommu_modes[] = {
 
 const char *default_iommu_mode = "vfio_type1_iommu";
 
+void iommu_mode_help(const char *flag)
+{
+	int i;
+
+	printf("  %s: The iommu mode to use for the test (default: %s)\n"
+	       "\n"
+	       "      Available modes:\n",
+	       flag, default_iommu_mode);
+
+	for (i = 0; i < ARRAY_SIZE(iommu_modes); i++)
+		printf("        %s\n", iommu_modes[i].name);
+
+	printf("\n");
+}
+
 static const struct vfio_iommu_mode *lookup_iommu_mode(const char *iommu_mode)
 {
 	int i;
diff --git a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
index 2ecf2514f30c..e2f636838130 100644
--- a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
+++ b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
@@ -18,6 +18,7 @@  static struct {
 	u64 iova;
 	int mmap_flags;
 	const char *bdf;
+	const char *iommu_mode;
 } test_config;
 
 struct iommu_mapping {
@@ -104,7 +105,7 @@  FIXTURE(vfio_dma_mapping_test)
 
 FIXTURE_SETUP(vfio_dma_mapping_test)
 {
-	self->device = vfio_pci_device_init(test_config.bdf, default_iommu_mode);
+	self->device = vfio_pci_device_init(test_config.bdf, test_config.iommu_mode);
 }
 
 FIXTURE_TEARDOWN(vfio_dma_mapping_test)
@@ -172,13 +173,15 @@  TEST_F(vfio_dma_mapping_test, dma_map_unmap)
 
 static void help(const char *name)
 {
-	printf("Usage: %s [-b backing_src] segment:bus:device.function\n"
+	printf("Usage: %s [-b backing_src] [-i iommu_mode] segment:bus:device.function\n"
 	       "  -b: Which backing memory to use (default: anonymous)\n"
 	       "\n"
 	       "      anonymous\n"
 	       "      anonymous_hugetlb_2mb\n"
-	       "      anonymous_hugetlb_1gb\n",
+	       "      anonymous_hugetlb_1gb\n"
+	       "\n",
 	       name);
+	iommu_mode_help("-i");
 	exit(1);
 }
 
@@ -211,11 +214,14 @@  int main(int argc, char *argv[])
 	const char *backing_src = "anonymous";
 	int c;
 
-	while ((c = getopt(argc, argv, "b:")) != -1) {
+	while ((c = getopt(argc, argv, "b:i:")) != -1) {
 		switch (c) {
 		case 'b':
 			backing_src = optarg;
 			break;
+		case 'i':
+			test_config.iommu_mode = optarg;
+			break;
 		default:
 			help(argv[0]);
 		}
diff --git a/tools/testing/selftests/vfio/vfio_pci_device_test.c b/tools/testing/selftests/vfio/vfio_pci_device_test.c
index 45707c6d1c09..842f0f2f3523 100644
--- a/tools/testing/selftests/vfio/vfio_pci_device_test.c
+++ b/tools/testing/selftests/vfio/vfio_pci_device_test.c
@@ -14,7 +14,8 @@ 
 
 #include "../kselftest_harness.h"
 
-const char *device_bdf;
+static const char *device_bdf;
+static const char *iommu_mode;
 
 /*
  * Limit the number of MSIs enabled/disabled by the test regardless of the
@@ -27,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, iommu_mode);
 }
 
 FIXTURE_TEARDOWN(vfio_pci_device_test) {
@@ -113,7 +114,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, iommu_mode);
 }
 
 FIXTURE_TEARDOWN(vfio_pci_irq_test) {
@@ -165,14 +166,30 @@  TEST_F(vfio_pci_device_test, reset)
 	vfio_pci_device_reset(self->device);
 }
 
+static void help(const char *name)
+{
+	printf("Usage: %s [-i iommu_mode] segment:bus:device.function\n", name);
+	iommu_mode_help("-i");
+	exit(1);
+}
+
 int main(int argc, char *argv[])
 {
-	if (argc != 2) {
-		fprintf(stderr, "usage: %s segment:bus:device.function\n", argv[0]);
-		return KSFT_FAIL;
+	int c;
+
+	while ((c = getopt(argc, argv, "i:")) != -1) {
+		switch (c) {
+		case 'i':
+			iommu_mode = optarg;
+			break;
+		default:
+			help(argv[0]);
+		}
 	}
 
-	device_bdf = argv[1];
+	if (optind >= argc)
+		help(argv[0]);
 
-	return test_harness_run(1, argv);
+	device_bdf = argv[optind];
+	return test_harness_run(0, NULL);
 }
diff --git a/tools/testing/selftests/vfio/vfio_pci_driver_test.c b/tools/testing/selftests/vfio/vfio_pci_driver_test.c
index 0f05cc3774a6..e94b77125e5e 100644
--- a/tools/testing/selftests/vfio/vfio_pci_driver_test.c
+++ b/tools/testing/selftests/vfio/vfio_pci_driver_test.c
@@ -11,7 +11,8 @@ 
 
 #include "../kselftest_harness.h"
 
-const char *device_bdf;
+static const char *device_bdf;
+static const char *iommu_mode;
 
 #define ASSERT_NO_MSI(_eventfd) do {			\
 	u64 __value;					\
@@ -62,7 +63,7 @@  FIXTURE(vfio_pci_driver_test) {
 FIXTURE_SETUP(vfio_pci_driver_test) {
 	struct vfio_pci_driver *driver;
 
-	self->device = vfio_pci_device_init(device_bdf, default_iommu_mode);
+	self->device = vfio_pci_device_init(device_bdf, iommu_mode);
 
 	driver = &self->device->driver;
 
@@ -217,23 +218,39 @@  TEST_F_TIMEOUT(vfio_pci_driver_test, memcpy_storm, 60)
 	ASSERT_NO_MSI(self->msi_fd);
 }
 
+static void help(const char *name)
+{
+	printf("Usage: %s [-i iommu_mode] segment:bus:device.function\n", name);
+	iommu_mode_help("-i");
+	exit(1);
+}
+
 int main(int argc, char *argv[])
 {
 	struct vfio_pci_device *device;
-
-	if (argc != 2) {
-		fprintf(stderr, "usage: %s segment:bus:device.function\n", argv[0]);
-		return KSFT_FAIL;
+	int c;
+
+	while ((c = getopt(argc, argv, "i:")) != -1) {
+		switch (c) {
+		case 'i':
+			iommu_mode = optarg;
+			break;
+		default:
+			help(argv[0]);
+		}
 	}
 
-	device_bdf = argv[1];
+	if (optind >= argc)
+		help(argv[0]);
+
+	device_bdf = argv[optind];
 
-	device = vfio_pci_device_init(device_bdf, default_iommu_mode);
+	device = vfio_pci_device_init(device_bdf, iommu_mode);
 	if (!device->driver.ops) {
 		fprintf(stderr, "No driver found for device %s\n", device_bdf);
 		return KSFT_SKIP;
 	}
 	vfio_pci_device_cleanup(device);
 
-	return test_harness_run(1, argv);
+	return test_harness_run(0, NULL);
 }