Skip to content

Commit 19cf492

Browse files
dmatlackawilliam
authored andcommitted
vfio: selftests: Move vfio_selftests_*() helpers into libvfio.c
Move the vfio_selftests_*() helpers into their own file libvfio.c. These helpers have nothing to do with struct vfio_pci_device, so they don't make sense in vfio_pci_device.c. No functional change intended. Reviewed-by: Alex Mastro <amastro@fb.com> Tested-by: Alex Mastro <amastro@fb.com> Reviewed-by: Raghavendra Rao Ananta <rananta@google.com> Signed-off-by: David Matlack <dmatlack@google.com> Link: https://lore.kernel.org/r/20251126231733.3302983-16-dmatlack@google.com Signed-off-by: Alex Williamson <alex@shazbot.org>
1 parent 657d241 commit 19cf492

3 files changed

Lines changed: 79 additions & 71 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
6+
#include "../../../kselftest.h"
7+
#include <libvfio.h>
8+
9+
static bool is_bdf(const char *str)
10+
{
11+
unsigned int s, b, d, f;
12+
int length, count;
13+
14+
count = sscanf(str, "%4x:%2x:%2x.%2x%n", &s, &b, &d, &f, &length);
15+
return count == 4 && length == strlen(str);
16+
}
17+
18+
static char **get_bdfs_cmdline(int *argc, char *argv[], int *nr_bdfs)
19+
{
20+
int i;
21+
22+
for (i = *argc - 1; i > 0 && is_bdf(argv[i]); i--)
23+
continue;
24+
25+
i++;
26+
*nr_bdfs = *argc - i;
27+
*argc -= *nr_bdfs;
28+
29+
return *nr_bdfs ? &argv[i] : NULL;
30+
}
31+
32+
static char *get_bdf_env(void)
33+
{
34+
char *bdf;
35+
36+
bdf = getenv("VFIO_SELFTESTS_BDF");
37+
if (!bdf)
38+
return NULL;
39+
40+
VFIO_ASSERT_TRUE(is_bdf(bdf), "Invalid BDF: %s\n", bdf);
41+
return bdf;
42+
}
43+
44+
char **vfio_selftests_get_bdfs(int *argc, char *argv[], int *nr_bdfs)
45+
{
46+
static char *env_bdf;
47+
char **bdfs;
48+
49+
bdfs = get_bdfs_cmdline(argc, argv, nr_bdfs);
50+
if (bdfs)
51+
return bdfs;
52+
53+
env_bdf = get_bdf_env();
54+
if (env_bdf) {
55+
*nr_bdfs = 1;
56+
return &env_bdf;
57+
}
58+
59+
fprintf(stderr, "Unable to determine which device(s) to use, skipping test.\n");
60+
fprintf(stderr, "\n");
61+
fprintf(stderr, "To pass the device address via environment variable:\n");
62+
fprintf(stderr, "\n");
63+
fprintf(stderr, " export VFIO_SELFTESTS_BDF=\"segment:bus:device.function\"\n");
64+
fprintf(stderr, " %s [options]\n", argv[0]);
65+
fprintf(stderr, "\n");
66+
fprintf(stderr, "To pass the device address(es) via argv:\n");
67+
fprintf(stderr, "\n");
68+
fprintf(stderr, " %s [options] segment:bus:device.function ...\n", argv[0]);
69+
fprintf(stderr, "\n");
70+
exit(KSFT_SKIP);
71+
}
72+
73+
const char *vfio_selftests_get_bdf(int *argc, char *argv[])
74+
{
75+
int nr_bdfs;
76+
77+
return vfio_selftests_get_bdfs(argc, argv, &nr_bdfs)[0];
78+
}

tools/testing/selftests/vfio/lib/libvfio.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LIBVFIO_SRCDIR := $(selfdir)/vfio/lib
55

66
LIBVFIO_C := iommu.c
77
LIBVFIO_C += iova_allocator.c
8+
LIBVFIO_C += libvfio.c
89
LIBVFIO_C += vfio_pci_device.c
910
LIBVFIO_C += vfio_pci_driver.c
1011

tools/testing/selftests/vfio/lib/vfio_pci_device.c

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -376,74 +376,3 @@ void vfio_pci_device_cleanup(struct vfio_pci_device *device)
376376

377377
free(device);
378378
}
379-
380-
static bool is_bdf(const char *str)
381-
{
382-
unsigned int s, b, d, f;
383-
int length, count;
384-
385-
count = sscanf(str, "%4x:%2x:%2x.%2x%n", &s, &b, &d, &f, &length);
386-
return count == 4 && length == strlen(str);
387-
}
388-
389-
static char **get_bdfs_cmdline(int *argc, char *argv[], int *nr_bdfs)
390-
{
391-
int i;
392-
393-
for (i = *argc - 1; i > 0 && is_bdf(argv[i]); i--)
394-
continue;
395-
396-
i++;
397-
*nr_bdfs = *argc - i;
398-
*argc -= *nr_bdfs;
399-
400-
return *nr_bdfs ? &argv[i] : NULL;
401-
}
402-
403-
static char *get_bdf_env(void)
404-
{
405-
char *bdf;
406-
407-
bdf = getenv("VFIO_SELFTESTS_BDF");
408-
if (!bdf)
409-
return NULL;
410-
411-
VFIO_ASSERT_TRUE(is_bdf(bdf), "Invalid BDF: %s\n", bdf);
412-
return bdf;
413-
}
414-
415-
char **vfio_selftests_get_bdfs(int *argc, char *argv[], int *nr_bdfs)
416-
{
417-
static char *env_bdf;
418-
char **bdfs;
419-
420-
bdfs = get_bdfs_cmdline(argc, argv, nr_bdfs);
421-
if (bdfs)
422-
return bdfs;
423-
424-
env_bdf = get_bdf_env();
425-
if (env_bdf) {
426-
*nr_bdfs = 1;
427-
return &env_bdf;
428-
}
429-
430-
fprintf(stderr, "Unable to determine which device(s) to use, skipping test.\n");
431-
fprintf(stderr, "\n");
432-
fprintf(stderr, "To pass the device address via environment variable:\n");
433-
fprintf(stderr, "\n");
434-
fprintf(stderr, " export VFIO_SELFTESTS_BDF=\"segment:bus:device.function\"\n");
435-
fprintf(stderr, " %s [options]\n", argv[0]);
436-
fprintf(stderr, "\n");
437-
fprintf(stderr, "To pass the device address(es) via argv:\n");
438-
fprintf(stderr, "\n");
439-
fprintf(stderr, " %s [options] segment:bus:device.function ...\n", argv[0]);
440-
fprintf(stderr, "\n");
441-
exit(KSFT_SKIP);
442-
}
443-
444-
const char *vfio_selftests_get_bdf(int *argc, char *argv[])
445-
{
446-
int nr_bdfs;
447-
448-
return vfio_selftests_get_bdfs(argc, argv, &nr_bdfs)[0];
449-
}

0 commit comments

Comments
 (0)