Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

vfio: selftests: Add a helper library for VFIO selftests

Add a basic helper library to be used by VFIO selftests.

The basic unit of the library is struct vfio_pci_device, which
represents a single PCI device that is bound to the vfio-pci driver. The
library currently only supports a single device per group and container,
and VFIO IOMMU types.

The code in this library was heavily based on prior work done by
Raghavendra Rao Ananta <rananta@google.com>, and the VFIO_ASSERT*()
macros were written by Vipin Sharma <vipinsh@google.com>.

Separate that Makefile rules for building the library into a separate
script so that the library can be built by and linked into KVM selftests
in a subsequent commit.

Acked-by: Shuah Khan <skhan@linuxfoundation.org>
Signed-off-by: David Matlack <dmatlack@google.com>
Link: https://lore.kernel.org/r/20250822212518.4156428-3-dmatlack@google.com
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>

authored by

David Matlack and committed by
Alex Williamson
19faf6fd 292e9ee2

+534
+14
tools/testing/selftests/vfio/Makefile
··· 1 1 CFLAGS = $(KHDR_INCLUDES) 2 2 include ../lib.mk 3 + include lib/libvfio.mk 4 + 5 + CFLAGS += -I$(top_srcdir)/tools/include 6 + CFLAGS += -MD 7 + CFLAGS += $(EXTRA_CFLAGS) 8 + 9 + $(TEST_GEN_PROGS): %: %.o $(LIBVFIO_O) 10 + $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $< $(LIBVFIO_O) $(LDLIBS) -o $@ 11 + 12 + TEST_GEN_PROGS_O = $(patsubst %, %.o, $(TEST_GEN_PROGS)) 13 + TEST_DEP_FILES = $(patsubst %.o, %.d, $(TEST_GEN_PROGS_O) $(LIBVFIO_O)) 14 + -include $(TEST_DEP_FILES) 15 + 16 + EXTRA_CLEAN += $(TEST_GEN_PROGS_O) $(TEST_DEP_FILES)
+140
tools/testing/selftests/vfio/lib/include/vfio_util.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-only */ 2 + #ifndef SELFTESTS_VFIO_LIB_INCLUDE_VFIO_UTIL_H 3 + #define SELFTESTS_VFIO_LIB_INCLUDE_VFIO_UTIL_H 4 + 5 + #include <string.h> 6 + #include <linux/vfio.h> 7 + #include <linux/list.h> 8 + #include <linux/pci_regs.h> 9 + 10 + #include "../../../kselftest.h" 11 + 12 + #define VFIO_LOG_AND_EXIT(...) do { \ 13 + fprintf(stderr, " " __VA_ARGS__); \ 14 + fprintf(stderr, "\n"); \ 15 + exit(KSFT_FAIL); \ 16 + } while (0) 17 + 18 + #define VFIO_ASSERT_OP(_lhs, _rhs, _op, ...) do { \ 19 + typeof(_lhs) __lhs = (_lhs); \ 20 + typeof(_rhs) __rhs = (_rhs); \ 21 + \ 22 + if (__lhs _op __rhs) \ 23 + break; \ 24 + \ 25 + fprintf(stderr, "%s:%u: Assertion Failure\n\n", __FILE__, __LINE__); \ 26 + fprintf(stderr, " Expression: " #_lhs " " #_op " " #_rhs "\n"); \ 27 + fprintf(stderr, " Observed: %#lx %s %#lx\n", \ 28 + (u64)__lhs, #_op, (u64)__rhs); \ 29 + fprintf(stderr, " [errno: %d - %s]\n", errno, strerror(errno)); \ 30 + VFIO_LOG_AND_EXIT(__VA_ARGS__); \ 31 + } while (0) 32 + 33 + #define VFIO_ASSERT_EQ(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, ==, ##__VA_ARGS__) 34 + #define VFIO_ASSERT_NE(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, !=, ##__VA_ARGS__) 35 + #define VFIO_ASSERT_LT(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, <, ##__VA_ARGS__) 36 + #define VFIO_ASSERT_LE(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, <=, ##__VA_ARGS__) 37 + #define VFIO_ASSERT_GT(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, >, ##__VA_ARGS__) 38 + #define VFIO_ASSERT_GE(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, >=, ##__VA_ARGS__) 39 + #define VFIO_ASSERT_TRUE(_a, ...) VFIO_ASSERT_NE(false, (_a), ##__VA_ARGS__) 40 + #define VFIO_ASSERT_FALSE(_a, ...) VFIO_ASSERT_EQ(false, (_a), ##__VA_ARGS__) 41 + #define VFIO_ASSERT_NULL(_a, ...) VFIO_ASSERT_EQ(NULL, _a, ##__VA_ARGS__) 42 + #define VFIO_ASSERT_NOT_NULL(_a, ...) VFIO_ASSERT_NE(NULL, _a, ##__VA_ARGS__) 43 + 44 + #define VFIO_FAIL(_fmt, ...) do { \ 45 + fprintf(stderr, "%s:%u: FAIL\n\n", __FILE__, __LINE__); \ 46 + VFIO_LOG_AND_EXIT(_fmt, ##__VA_ARGS__); \ 47 + } while (0) 48 + 49 + struct vfio_pci_bar { 50 + struct vfio_region_info info; 51 + void *vaddr; 52 + }; 53 + 54 + struct vfio_pci_device { 55 + int fd; 56 + int group_fd; 57 + int container_fd; 58 + 59 + struct vfio_device_info info; 60 + struct vfio_region_info config_space; 61 + struct vfio_pci_bar bars[PCI_STD_NUM_BARS]; 62 + 63 + struct vfio_irq_info msi_info; 64 + struct vfio_irq_info msix_info; 65 + 66 + /* eventfds for MSI and MSI-x interrupts */ 67 + int msi_eventfds[PCI_MSIX_FLAGS_QSIZE + 1]; 68 + }; 69 + 70 + /* 71 + * Return the BDF string of the device that the test should use. 72 + * 73 + * If a BDF string is provided by the user on the command line (as the last 74 + * element of argv[]), then this function will return that and decrement argc 75 + * by 1. 76 + * 77 + * Otherwise this function will attempt to use the environment variable 78 + * $VFIO_SELFTESTS_BDF. 79 + * 80 + * If BDF cannot be determined then the test will exit with KSFT_SKIP. 81 + */ 82 + const char *vfio_selftests_get_bdf(int *argc, char *argv[]); 83 + 84 + struct vfio_pci_device *vfio_pci_device_init(const char *bdf, int iommu_type); 85 + void vfio_pci_device_cleanup(struct vfio_pci_device *device); 86 + 87 + void vfio_pci_dma_map(struct vfio_pci_device *device, u64 iova, u64 size, 88 + void *vaddr); 89 + void vfio_pci_dma_unmap(struct vfio_pci_device *device, u64 iova, u64 size); 90 + 91 + void vfio_pci_config_access(struct vfio_pci_device *device, bool write, 92 + size_t config, size_t size, void *data); 93 + 94 + #define vfio_pci_config_read(_device, _offset, _type) ({ \ 95 + _type __data; \ 96 + vfio_pci_config_access((_device), false, _offset, sizeof(__data), &__data); \ 97 + __data; \ 98 + }) 99 + 100 + #define vfio_pci_config_readb(_d, _o) vfio_pci_config_read(_d, _o, u8) 101 + #define vfio_pci_config_readw(_d, _o) vfio_pci_config_read(_d, _o, u16) 102 + #define vfio_pci_config_readl(_d, _o) vfio_pci_config_read(_d, _o, u32) 103 + 104 + #define vfio_pci_config_write(_device, _offset, _value, _type) do { \ 105 + _type __data = (_value); \ 106 + vfio_pci_config_access((_device), true, _offset, sizeof(_type), &__data); \ 107 + } while (0) 108 + 109 + #define vfio_pci_config_writeb(_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u8) 110 + #define vfio_pci_config_writew(_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u16) 111 + #define vfio_pci_config_writel(_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u32) 112 + 113 + void vfio_pci_irq_enable(struct vfio_pci_device *device, u32 index, 114 + u32 vector, int count); 115 + void vfio_pci_irq_disable(struct vfio_pci_device *device, u32 index); 116 + void vfio_pci_irq_trigger(struct vfio_pci_device *device, u32 index, u32 vector); 117 + 118 + static inline void vfio_pci_msi_enable(struct vfio_pci_device *device, 119 + u32 vector, int count) 120 + { 121 + vfio_pci_irq_enable(device, VFIO_PCI_MSI_IRQ_INDEX, vector, count); 122 + } 123 + 124 + static inline void vfio_pci_msi_disable(struct vfio_pci_device *device) 125 + { 126 + vfio_pci_irq_disable(device, VFIO_PCI_MSI_IRQ_INDEX); 127 + } 128 + 129 + static inline void vfio_pci_msix_enable(struct vfio_pci_device *device, 130 + u32 vector, int count) 131 + { 132 + vfio_pci_irq_enable(device, VFIO_PCI_MSIX_IRQ_INDEX, vector, count); 133 + } 134 + 135 + static inline void vfio_pci_msix_disable(struct vfio_pci_device *device) 136 + { 137 + vfio_pci_irq_disable(device, VFIO_PCI_MSIX_IRQ_INDEX); 138 + } 139 + 140 + #endif /* SELFTESTS_VFIO_LIB_INCLUDE_VFIO_UTIL_H */
+15
tools/testing/selftests/vfio/lib/libvfio.mk
··· 1 + VFIO_DIR := $(selfdir)/vfio 2 + 3 + LIBVFIO_C := lib/vfio_pci_device.c 4 + 5 + LIBVFIO_O := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBVFIO_C)) 6 + 7 + LIBVFIO_O_DIRS := $(shell dirname $(LIBVFIO_O) | uniq) 8 + $(shell mkdir -p $(LIBVFIO_O_DIRS)) 9 + 10 + CFLAGS += -I$(VFIO_DIR)/lib/include 11 + 12 + $(LIBVFIO_O): $(OUTPUT)/%.o : $(VFIO_DIR)/%.c 13 + $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@ 14 + 15 + EXTRA_CLEAN += $(LIBVFIO_O)
+365
tools/testing/selftests/vfio/lib/vfio_pci_device.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + #include <fcntl.h> 3 + #include <libgen.h> 4 + #include <stdlib.h> 5 + #include <string.h> 6 + #include <unistd.h> 7 + 8 + #include <sys/eventfd.h> 9 + #include <sys/ioctl.h> 10 + #include <sys/mman.h> 11 + 12 + #include <linux/limits.h> 13 + #include <linux/mman.h> 14 + #include <linux/types.h> 15 + #include <linux/vfio.h> 16 + 17 + #include "../../../kselftest.h" 18 + #include <vfio_util.h> 19 + 20 + #define VFIO_DEV_PATH "/dev/vfio/vfio" 21 + #define PCI_SYSFS_PATH "/sys/bus/pci/devices" 22 + 23 + #define ioctl_assert(_fd, _op, _arg) do { \ 24 + void *__arg = (_arg); \ 25 + int __ret = ioctl((_fd), (_op), (__arg)); \ 26 + VFIO_ASSERT_EQ(__ret, 0, "ioctl(%s, %s, %s) returned %d\n", #_fd, #_op, #_arg, __ret); \ 27 + } while (0) 28 + 29 + static void vfio_pci_irq_set(struct vfio_pci_device *device, 30 + u32 index, u32 vector, u32 count, int *fds) 31 + { 32 + u8 buf[sizeof(struct vfio_irq_set) + sizeof(int) * count] = {}; 33 + struct vfio_irq_set *irq = (void *)&buf; 34 + int *irq_fds = (void *)&irq->data; 35 + 36 + irq->argsz = sizeof(buf); 37 + irq->flags = VFIO_IRQ_SET_ACTION_TRIGGER; 38 + irq->index = index; 39 + irq->start = vector; 40 + irq->count = count; 41 + 42 + if (count) { 43 + irq->flags |= VFIO_IRQ_SET_DATA_EVENTFD; 44 + memcpy(irq_fds, fds, sizeof(int) * count); 45 + } else { 46 + irq->flags |= VFIO_IRQ_SET_DATA_NONE; 47 + } 48 + 49 + ioctl_assert(device->fd, VFIO_DEVICE_SET_IRQS, irq); 50 + } 51 + 52 + void vfio_pci_irq_trigger(struct vfio_pci_device *device, u32 index, u32 vector) 53 + { 54 + struct vfio_irq_set irq = { 55 + .argsz = sizeof(irq), 56 + .flags = VFIO_IRQ_SET_ACTION_TRIGGER | VFIO_IRQ_SET_DATA_NONE, 57 + .index = index, 58 + .start = vector, 59 + .count = 1, 60 + }; 61 + 62 + ioctl_assert(device->fd, VFIO_DEVICE_SET_IRQS, &irq); 63 + } 64 + 65 + static void check_supported_irq_index(u32 index) 66 + { 67 + /* VFIO selftests only supports MSI and MSI-x for now. */ 68 + VFIO_ASSERT_TRUE(index == VFIO_PCI_MSI_IRQ_INDEX || 69 + index == VFIO_PCI_MSIX_IRQ_INDEX, 70 + "Unsupported IRQ index: %u\n", index); 71 + } 72 + 73 + void vfio_pci_irq_enable(struct vfio_pci_device *device, u32 index, u32 vector, 74 + int count) 75 + { 76 + int i; 77 + 78 + check_supported_irq_index(index); 79 + 80 + for (i = vector; i < vector + count; i++) { 81 + VFIO_ASSERT_LT(device->msi_eventfds[i], 0); 82 + device->msi_eventfds[i] = eventfd(0, 0); 83 + VFIO_ASSERT_GE(device->msi_eventfds[i], 0); 84 + } 85 + 86 + vfio_pci_irq_set(device, index, vector, count, device->msi_eventfds + vector); 87 + } 88 + 89 + void vfio_pci_irq_disable(struct vfio_pci_device *device, u32 index) 90 + { 91 + int i; 92 + 93 + check_supported_irq_index(index); 94 + 95 + for (i = 0; i < ARRAY_SIZE(device->msi_eventfds); i++) { 96 + if (device->msi_eventfds[i] < 0) 97 + continue; 98 + 99 + VFIO_ASSERT_EQ(close(device->msi_eventfds[i]), 0); 100 + device->msi_eventfds[i] = -1; 101 + } 102 + 103 + vfio_pci_irq_set(device, index, 0, 0, NULL); 104 + } 105 + 106 + static void vfio_pci_irq_get(struct vfio_pci_device *device, u32 index, 107 + struct vfio_irq_info *irq_info) 108 + { 109 + irq_info->argsz = sizeof(*irq_info); 110 + irq_info->index = index; 111 + 112 + ioctl_assert(device->fd, VFIO_DEVICE_GET_IRQ_INFO, irq_info); 113 + } 114 + 115 + void vfio_pci_dma_map(struct vfio_pci_device *device, u64 iova, u64 size, void *vaddr) 116 + { 117 + struct vfio_iommu_type1_dma_map map = { 118 + .argsz = sizeof(map), 119 + .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE, 120 + .vaddr = (u64)vaddr, 121 + .iova = iova, 122 + .size = size, 123 + }; 124 + 125 + ioctl_assert(device->container_fd, VFIO_IOMMU_MAP_DMA, &map); 126 + } 127 + 128 + void vfio_pci_dma_unmap(struct vfio_pci_device *device, u64 iova, u64 size) 129 + { 130 + struct vfio_iommu_type1_dma_unmap unmap = { 131 + .argsz = sizeof(unmap), 132 + .iova = iova, 133 + .size = size, 134 + }; 135 + 136 + ioctl_assert(device->container_fd, VFIO_IOMMU_UNMAP_DMA, &unmap); 137 + } 138 + 139 + static void vfio_pci_region_get(struct vfio_pci_device *device, int index, 140 + struct vfio_region_info *info) 141 + { 142 + memset(info, 0, sizeof(*info)); 143 + 144 + info->argsz = sizeof(*info); 145 + info->index = index; 146 + 147 + ioctl_assert(device->fd, VFIO_DEVICE_GET_REGION_INFO, info); 148 + } 149 + 150 + static void vfio_pci_bar_map(struct vfio_pci_device *device, int index) 151 + { 152 + struct vfio_pci_bar *bar = &device->bars[index]; 153 + int prot = 0; 154 + 155 + VFIO_ASSERT_LT(index, PCI_STD_NUM_BARS); 156 + VFIO_ASSERT_NULL(bar->vaddr); 157 + VFIO_ASSERT_TRUE(bar->info.flags & VFIO_REGION_INFO_FLAG_MMAP); 158 + 159 + if (bar->info.flags & VFIO_REGION_INFO_FLAG_READ) 160 + prot |= PROT_READ; 161 + if (bar->info.flags & VFIO_REGION_INFO_FLAG_WRITE) 162 + prot |= PROT_WRITE; 163 + 164 + bar->vaddr = mmap(NULL, bar->info.size, prot, MAP_FILE | MAP_SHARED, 165 + device->fd, bar->info.offset); 166 + VFIO_ASSERT_NE(bar->vaddr, MAP_FAILED); 167 + } 168 + 169 + static void vfio_pci_bar_unmap(struct vfio_pci_device *device, int index) 170 + { 171 + struct vfio_pci_bar *bar = &device->bars[index]; 172 + 173 + VFIO_ASSERT_LT(index, PCI_STD_NUM_BARS); 174 + VFIO_ASSERT_NOT_NULL(bar->vaddr); 175 + 176 + VFIO_ASSERT_EQ(munmap(bar->vaddr, bar->info.size), 0); 177 + bar->vaddr = NULL; 178 + } 179 + 180 + static void vfio_pci_bar_unmap_all(struct vfio_pci_device *device) 181 + { 182 + int i; 183 + 184 + for (i = 0; i < PCI_STD_NUM_BARS; i++) { 185 + if (device->bars[i].vaddr) 186 + vfio_pci_bar_unmap(device, i); 187 + } 188 + } 189 + 190 + void vfio_pci_config_access(struct vfio_pci_device *device, bool write, 191 + size_t config, size_t size, void *data) 192 + { 193 + struct vfio_region_info *config_space = &device->config_space; 194 + int ret; 195 + 196 + if (write) 197 + ret = pwrite(device->fd, data, size, config_space->offset + config); 198 + else 199 + ret = pread(device->fd, data, size, config_space->offset + config); 200 + 201 + VFIO_ASSERT_EQ(ret, size, "Failed to %s PCI config space: 0x%lx\n", 202 + write ? "write to" : "read from", config); 203 + } 204 + 205 + static unsigned int vfio_pci_get_group_from_dev(const char *bdf) 206 + { 207 + char dev_iommu_group_path[PATH_MAX] = {0}; 208 + char sysfs_path[PATH_MAX] = {0}; 209 + unsigned int group; 210 + int ret; 211 + 212 + snprintf(sysfs_path, PATH_MAX, "%s/%s/iommu_group", PCI_SYSFS_PATH, bdf); 213 + 214 + ret = readlink(sysfs_path, dev_iommu_group_path, sizeof(dev_iommu_group_path)); 215 + VFIO_ASSERT_NE(ret, -1, "Failed to get the IOMMU group for device: %s\n", bdf); 216 + 217 + ret = sscanf(basename(dev_iommu_group_path), "%u", &group); 218 + VFIO_ASSERT_EQ(ret, 1, "Failed to get the IOMMU group for device: %s\n", bdf); 219 + 220 + return group; 221 + } 222 + 223 + static void vfio_pci_container_setup(struct vfio_pci_device *device) 224 + { 225 + int version; 226 + 227 + device->container_fd = open(VFIO_DEV_PATH, O_RDWR); 228 + VFIO_ASSERT_GE(device->container_fd, 0, "open(%s) failed\n", VFIO_DEV_PATH); 229 + 230 + version = ioctl(device->container_fd, VFIO_GET_API_VERSION); 231 + VFIO_ASSERT_EQ(version, VFIO_API_VERSION); 232 + } 233 + 234 + static void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf) 235 + { 236 + struct vfio_group_status group_status = { 237 + .argsz = sizeof(group_status), 238 + }; 239 + char group_path[32]; 240 + int group; 241 + 242 + group = vfio_pci_get_group_from_dev(bdf); 243 + snprintf(group_path, sizeof(group_path), "/dev/vfio/%d", group); 244 + 245 + device->group_fd = open(group_path, O_RDWR); 246 + VFIO_ASSERT_GE(device->group_fd, 0, "open(%s) failed\n", group_path); 247 + 248 + ioctl_assert(device->group_fd, VFIO_GROUP_GET_STATUS, &group_status); 249 + VFIO_ASSERT_TRUE(group_status.flags & VFIO_GROUP_FLAGS_VIABLE); 250 + 251 + ioctl_assert(device->group_fd, VFIO_GROUP_SET_CONTAINER, &device->container_fd); 252 + } 253 + 254 + static void vfio_pci_iommu_setup(struct vfio_pci_device *device, unsigned long iommu_type) 255 + { 256 + int ret; 257 + 258 + ret = ioctl(device->container_fd, VFIO_CHECK_EXTENSION, iommu_type); 259 + VFIO_ASSERT_GT(ret, 0, "VFIO IOMMU type %lu not supported\n", iommu_type); 260 + 261 + ioctl_assert(device->container_fd, VFIO_SET_IOMMU, (void *)iommu_type); 262 + } 263 + 264 + static void vfio_pci_device_setup(struct vfio_pci_device *device, const char *bdf) 265 + { 266 + int i; 267 + 268 + device->fd = ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, bdf); 269 + VFIO_ASSERT_GE(device->fd, 0); 270 + 271 + device->info.argsz = sizeof(device->info); 272 + ioctl_assert(device->fd, VFIO_DEVICE_GET_INFO, &device->info); 273 + 274 + vfio_pci_region_get(device, VFIO_PCI_CONFIG_REGION_INDEX, &device->config_space); 275 + 276 + /* Sanity check VFIO does not advertise mmap for config space */ 277 + VFIO_ASSERT_TRUE(!(device->config_space.flags & VFIO_REGION_INFO_FLAG_MMAP), 278 + "PCI config space should not support mmap()\n"); 279 + 280 + for (i = 0; i < PCI_STD_NUM_BARS; i++) { 281 + struct vfio_pci_bar *bar = device->bars + i; 282 + 283 + vfio_pci_region_get(device, i, &bar->info); 284 + if (bar->info.flags & VFIO_REGION_INFO_FLAG_MMAP) 285 + vfio_pci_bar_map(device, i); 286 + } 287 + 288 + vfio_pci_irq_get(device, VFIO_PCI_MSI_IRQ_INDEX, &device->msi_info); 289 + vfio_pci_irq_get(device, VFIO_PCI_MSIX_IRQ_INDEX, &device->msix_info); 290 + 291 + for (i = 0; i < ARRAY_SIZE(device->msi_eventfds); i++) 292 + device->msi_eventfds[i] = -1; 293 + } 294 + 295 + struct vfio_pci_device *vfio_pci_device_init(const char *bdf, int iommu_type) 296 + { 297 + struct vfio_pci_device *device; 298 + 299 + device = calloc(1, sizeof(*device)); 300 + VFIO_ASSERT_NOT_NULL(device); 301 + 302 + vfio_pci_container_setup(device); 303 + vfio_pci_group_setup(device, bdf); 304 + vfio_pci_iommu_setup(device, iommu_type); 305 + vfio_pci_device_setup(device, bdf); 306 + 307 + return device; 308 + } 309 + 310 + void vfio_pci_device_cleanup(struct vfio_pci_device *device) 311 + { 312 + int i; 313 + 314 + vfio_pci_bar_unmap_all(device); 315 + 316 + VFIO_ASSERT_EQ(close(device->fd), 0); 317 + 318 + for (i = 0; i < ARRAY_SIZE(device->msi_eventfds); i++) { 319 + if (device->msi_eventfds[i] < 0) 320 + continue; 321 + 322 + VFIO_ASSERT_EQ(close(device->msi_eventfds[i]), 0); 323 + } 324 + 325 + VFIO_ASSERT_EQ(close(device->group_fd), 0); 326 + VFIO_ASSERT_EQ(close(device->container_fd), 0); 327 + 328 + free(device); 329 + } 330 + 331 + static bool is_bdf(const char *str) 332 + { 333 + unsigned int s, b, d, f; 334 + int length, count; 335 + 336 + count = sscanf(str, "%4x:%2x:%2x.%2x%n", &s, &b, &d, &f, &length); 337 + return count == 4 && length == strlen(str); 338 + } 339 + 340 + const char *vfio_selftests_get_bdf(int *argc, char *argv[]) 341 + { 342 + char *bdf; 343 + 344 + if (*argc > 1 && is_bdf(argv[*argc - 1])) 345 + return argv[--(*argc)]; 346 + 347 + bdf = getenv("VFIO_SELFTESTS_BDF"); 348 + if (bdf) { 349 + VFIO_ASSERT_TRUE(is_bdf(bdf), "Invalid BDF: %s\n", bdf); 350 + return bdf; 351 + } 352 + 353 + fprintf(stderr, "Unable to determine which device to use, skipping test.\n"); 354 + fprintf(stderr, "\n"); 355 + fprintf(stderr, "To pass the device address via environment variable:\n"); 356 + fprintf(stderr, "\n"); 357 + fprintf(stderr, " export VFIO_SELFTESTS_BDF=segment:bus:device.function\n"); 358 + fprintf(stderr, " %s [options]\n", argv[0]); 359 + fprintf(stderr, "\n"); 360 + fprintf(stderr, "To pass the device address via argv:\n"); 361 + fprintf(stderr, "\n"); 362 + fprintf(stderr, " %s [options] segment:bus:device.function\n", argv[0]); 363 + fprintf(stderr, "\n"); 364 + exit(KSFT_SKIP); 365 + }