summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/vfio/lib/sysfs.c
blob: 11415448b2e29cc9528c05ec54cf9873fba01b92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// SPDX-License-Identifier: GPL-2.0-only
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <linux/limits.h>

#include <libvfio.h>

#define readlink_safe(_path, _buf) ({					\
	int __ret;							\
									\
	_Static_assert(!__builtin_types_compatible_p(			\
		__typeof__(_buf), char *),				\
	    "readlink_safe: _buf must be an array, not a pointer");	\
									\
	__ret = readlink(_path, _buf, sizeof(_buf) - 1);		\
	if (__ret != -1)						\
		_buf[__ret] = 0;					\
	__ret;								\
})

static void readlink_base(const char *path, const char *data_fmt, void *out_data)
{
	char rl_path[PATH_MAX];
	int ret;

	ret = readlink_safe(path, rl_path);
	VFIO_ASSERT_NE(ret, -1);

	ret = sscanf(basename(rl_path), data_fmt, out_data);
	VFIO_ASSERT_EQ(ret, 1);
}

static int sysfs_val_get_int(const char *component, const char *name,
			     const char *file)
{
	char path[PATH_MAX];
	char buf[32];
	int ret;
	int fd;

	snprintf_assert(path, PATH_MAX, "/sys/bus/pci/%s/%s/%s", component, name, file);
	fd = open(path, O_RDONLY);
	if (fd < 0)
		return fd;

	VFIO_ASSERT_GT(read(fd, buf, ARRAY_SIZE(buf)), 0);
	VFIO_ASSERT_EQ(close(fd), 0);

	errno = 0;
	ret = strtol(buf, NULL, 0);
	VFIO_ASSERT_EQ(errno, 0, "sysfs path \"%s\" is not an integer: \"%s\"\n", path, buf);

	return ret;
}

static void sysfs_val_set(const char *component, const char *name,
			  const char *file, const char *val)
{
	char path[PATH_MAX];
	int fd;

	snprintf_assert(path, PATH_MAX, "/sys/bus/pci/%s/%s/%s", component, name, file);
	VFIO_ASSERT_GT(fd = open(path, O_WRONLY), 0);

	VFIO_ASSERT_EQ(write(fd, val, strlen(val)), strlen(val));
	VFIO_ASSERT_EQ(close(fd), 0);
}

static int sysfs_device_val_get(const char *bdf, const char *file)
{
	return sysfs_val_get_int("devices", bdf, file);
}

static void sysfs_device_val_set(const char *bdf, const char *file, const char *val)
{
	sysfs_val_set("devices", bdf, file, val);
}

static void sysfs_device_val_set_int(const char *bdf, const char *file, int val)
{
	char val_str[32];

	snprintf_assert(val_str, sizeof(val_str), "%d", val);
	sysfs_device_val_set(bdf, file, val_str);
}

int sysfs_sriov_totalvfs_get(const char *bdf)
{
	return sysfs_device_val_get(bdf, "sriov_totalvfs");
}

int sysfs_sriov_numvfs_get(const char *bdf)
{
	return sysfs_device_val_get(bdf, "sriov_numvfs");
}

void sysfs_sriov_numvfs_set(const char *bdf, int numvfs)
{
	sysfs_device_val_set_int(bdf, "sriov_numvfs", numvfs);
}

char *sysfs_sriov_vf_bdf_get(const char *pf_bdf, int i)
{
	char path[PATH_MAX];
	char *out_vf_bdf;

	/* Fit "0000:00:00.0" */
	out_vf_bdf = calloc(16, sizeof(char));
	VFIO_ASSERT_NOT_NULL(out_vf_bdf);

	snprintf_assert(path, PATH_MAX, "/sys/bus/pci/devices/%s/virtfn%d", pf_bdf, i);
	readlink_base(path, "%s", out_vf_bdf);

	return out_vf_bdf;
}

int sysfs_iommu_group_get(const char *bdf)
{
	char path[PATH_MAX];
	int group;

	snprintf_assert(path, PATH_MAX, "/sys/bus/pci/devices/%s/iommu_group", bdf);
	readlink_base(path, "%d", &group);

	return group;
}

char *sysfs_driver_get(const char *bdf)
{
	char driver_path[PATH_MAX];
	char path[PATH_MAX];
	char *out_driver;
	int ret;

	snprintf_assert(path, PATH_MAX, "/sys/bus/pci/devices/%s/driver", bdf);
	ret = readlink_safe(path, driver_path);
	if (ret == -1) {
		if (errno == ENOENT)
			return NULL;

		VFIO_FAIL("Failed to read %s\n", path);
	}

	out_driver = strdup(basename(driver_path));
	VFIO_ASSERT_NOT_NULL(out_driver);

	return out_driver;
}