]> www.infradead.org Git - users/hch/misc.git/commitdiff
vfio: selftests: Move helper to get cdev path to libvfio
authorDavid Matlack <dmatlack@google.com>
Fri, 22 Aug 2025 21:25:10 +0000 (21:25 +0000)
committerAlex Williamson <alex.williamson@redhat.com>
Wed, 27 Aug 2025 18:14:09 +0000 (12:14 -0600)
Move the helper function to get the VFIO cdev path to libvfio so that it
can be used in libvfio in a subsequent commit.

No functional change intended.

Acked-by: Shuah Khan <skhan@linuxfoundation.org>
Signed-off-by: David Matlack <dmatlack@google.com>
Link: https://lore.kernel.org/r/20250822212518.4156428-24-dmatlack@google.com
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
tools/testing/selftests/vfio/lib/include/vfio_util.h
tools/testing/selftests/vfio/lib/vfio_pci_device.c
tools/testing/selftests/vfio/vfio_iommufd_setup_test.c

index a7d05a4299a1da25a963c002d231fcaa82caabb9..05a10417e0d7197f0c9c0ff7b2bf2c4052edc783 100644 (file)
@@ -175,6 +175,7 @@ struct vfio_pci_device {
  * If BDF cannot be determined then the test will exit with KSFT_SKIP.
  */
 const char *vfio_selftests_get_bdf(int *argc, char *argv[]);
+const char *vfio_pci_get_cdev_path(const char *bdf);
 
 struct vfio_pci_device *vfio_pci_device_init(const char *bdf, int iommu_type);
 void vfio_pci_device_cleanup(struct vfio_pci_device *device);
index d8bb227e869dc4637ed8dab5d108c46d806aa944..d53e2d682c7edf47761435622621bfcdca8fc73f 100644 (file)
@@ -1,4 +1,5 @@
 // SPDX-License-Identifier: GPL-2.0-only
+#include <dirent.h>
 #include <fcntl.h>
 #include <libgen.h>
 #include <stdlib.h>
@@ -332,6 +333,36 @@ static void vfio_pci_device_setup(struct vfio_pci_device *device, const char *bd
                device->msi_eventfds[i] = -1;
 }
 
+const char *vfio_pci_get_cdev_path(const char *bdf)
+{
+       char dir_path[PATH_MAX];
+       struct dirent *entry;
+       char *cdev_path;
+       DIR *dir;
+
+       cdev_path = calloc(PATH_MAX, 1);
+       VFIO_ASSERT_NOT_NULL(cdev_path);
+
+       snprintf(dir_path, sizeof(dir_path), "/sys/bus/pci/devices/%s/vfio-dev/", bdf);
+
+       dir = opendir(dir_path);
+       VFIO_ASSERT_NOT_NULL(dir, "Failed to open directory %s\n", dir_path);
+
+       while ((entry = readdir(dir)) != NULL) {
+               /* Find the file that starts with "vfio" */
+               if (strncmp("vfio", entry->d_name, 4))
+                       continue;
+
+               snprintf(cdev_path, PATH_MAX, "/dev/vfio/devices/%s", entry->d_name);
+               break;
+       }
+
+       VFIO_ASSERT_NE(cdev_path[0], 0, "Failed to find vfio cdev file.\n");
+       VFIO_ASSERT_EQ(closedir(dir), 0);
+
+       return cdev_path;
+}
+
 struct vfio_pci_device *vfio_pci_device_init(const char *bdf, int iommu_type)
 {
        struct vfio_pci_device *device;
index f45335d9260fe40ca0bf942a915e3cbfb0964461..3655106b912d1012fad6be9d6ab384beed26cb86 100644 (file)
@@ -1,8 +1,4 @@
 // SPDX-License-Identifier: GPL-2.0
-#include <assert.h>
-#include <dirent.h>
-#include <fcntl.h>
-
 #include <uapi/linux/types.h>
 #include <linux/limits.h>
 #include <linux/sizes.h>
@@ -11,7 +7,6 @@
 
 #include <stdint.h>
 #include <stdio.h>
-#include <string.h>
 #include <sys/ioctl.h>
 #include <unistd.h>
 
 #include "../kselftest_harness.h"
 
 static const char iommu_dev_path[] = "/dev/iommu";
-static char cdev_path[PATH_MAX] = { '\0' };
-
-static void set_cdev_path(const char *bdf)
-{
-       char dir_path[PATH_MAX];
-       DIR *dir;
-       struct dirent *entry;
-
-       snprintf(dir_path, sizeof(dir_path), "/sys/bus/pci/devices/%s/vfio-dev/", bdf);
-
-       dir = opendir(dir_path);
-       assert(dir);
-
-       /* Find the file named "vfio<number>" */
-       while ((entry = readdir(dir)) != NULL) {
-               if (!strncmp("vfio", entry->d_name, 4)) {
-                       snprintf(cdev_path, sizeof(cdev_path), "/dev/vfio/devices/%s",
-                                entry->d_name);
-                       break;
-               }
-       }
-
-       assert(strlen(cdev_path) > 0);
-
-       closedir(dir);
-}
+static const char *cdev_path;
 
 static int vfio_device_bind_iommufd_ioctl(int cdev_fd, int iommufd)
 {
@@ -150,7 +120,7 @@ int main(int argc, char *argv[])
 {
        const char *device_bdf = vfio_selftests_get_bdf(&argc, argv);
 
-       set_cdev_path(device_bdf);
+       cdev_path = vfio_pci_get_cdev_path(device_bdf);
        printf("Using cdev device %s\n", cdev_path);
 
        return test_harness_run(argc, argv);