-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathiommu_group_test.go
More file actions
215 lines (173 loc) · 7.31 KB
/
iommu_group_test.go
File metadata and controls
215 lines (173 loc) · 7.31 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright (c) 2026 Zededa, Inc.
// SPDX-License-Identifier: Apache-2.0
package hypervisor
import (
"os"
"path/filepath"
"sort"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// setupFakeIOMMUSysfs creates a temporary sysfs tree for IOMMU group tests
// and returns an iommuGroupContext pointing at it.
func setupFakeIOMMUSysfs(t *testing.T) (iommuGroupContext, string) {
t.Helper()
tmpDir, err := os.MkdirTemp("", "iommu-test-*")
require.NoError(t, err)
t.Cleanup(func() { os.RemoveAll(tmpDir) })
pciDevicesDir := filepath.Join(tmpDir, "bus/pci/devices")
iommuGroupsDir := filepath.Join(tmpDir, "kernel/iommu_groups")
vfioDriverDir := filepath.Join(tmpDir, "bus/pci/drivers/vfio-pci")
driversProbe := filepath.Join(tmpDir, "bus/pci/drivers_probe")
require.NoError(t, os.MkdirAll(pciDevicesDir, 0755))
require.NoError(t, os.MkdirAll(iommuGroupsDir, 0755))
require.NoError(t, os.MkdirAll(vfioDriverDir, 0755))
require.NoError(t, os.WriteFile(driversProbe, nil, 0644))
ctx := iommuGroupContext{
pciDevicesDir: pciDevicesDir,
iommuGroupsDir: iommuGroupsDir,
vfioDriverDir: vfioDriverDir,
driversProbe: driversProbe,
}
return ctx, tmpDir
}
// addDeviceToGroup creates a fake PCI device directory with an iommu_group
// symlink and registers it in the group's devices directory.
func addDeviceToGroup(t *testing.T, ctx *iommuGroupContext, addr, group string) {
t.Helper()
devDir := filepath.Join(ctx.pciDevicesDir, addr)
require.NoError(t, os.MkdirAll(devDir, 0755))
groupDevicesDir := filepath.Join(ctx.iommuGroupsDir, group, "devices")
require.NoError(t, os.MkdirAll(groupDevicesDir, 0755))
// iommu_group symlink — os.Readlink returns the target, filepath.Base extracts group number
iommuGroupTarget := filepath.Join(ctx.iommuGroupsDir, group)
require.NoError(t, os.Symlink(iommuGroupTarget, filepath.Join(devDir, "iommu_group")))
// Register device in group's devices directory
require.NoError(t, os.WriteFile(filepath.Join(groupDevicesDir, addr), nil, 0644))
}
// bindToKernelDriver simulates a kernel driver binding by creating a driver
// directory with an unbind file and symlinking device/driver to it.
func bindToKernelDriver(t *testing.T, ctx *iommuGroupContext, tmpDir, addr, driverName string) {
t.Helper()
driverDir := filepath.Join(tmpDir, "bus/pci/drivers", driverName)
require.NoError(t, os.MkdirAll(driverDir, 0755))
unbindFile := filepath.Join(driverDir, "unbind")
if _, err := os.Stat(unbindFile); err != nil {
require.NoError(t, os.WriteFile(unbindFile, nil, 0644))
}
require.NoError(t, os.Symlink(driverDir, filepath.Join(ctx.pciDevicesDir, addr, "driver")))
}
// bindToVfioPci simulates vfio-pci driver binding.
func bindToVfioPci(t *testing.T, ctx *iommuGroupContext, addr string) {
t.Helper()
require.NoError(t, os.Symlink(ctx.vfioDriverDir, filepath.Join(ctx.pciDevicesDir, addr, "driver")))
}
func TestGetIOMMUGroup(t *testing.T) {
ctx, _ := setupFakeIOMMUSysfs(t)
addDeviceToGroup(t, &ctx, "0000:80:1f.6", "19")
group, err := ctx.getIOMMUGroup("0000:80:1f.6")
require.NoError(t, err)
assert.Equal(t, "19", group)
}
func TestGetIOMMUGroupNoSymlink(t *testing.T) {
ctx, _ := setupFakeIOMMUSysfs(t)
// Device exists but has no iommu_group symlink
require.NoError(t, os.MkdirAll(filepath.Join(ctx.pciDevicesDir, "0000:00:01.0"), 0755))
_, err := ctx.getIOMMUGroup("0000:00:01.0")
assert.Error(t, err)
}
func TestGetMembers(t *testing.T) {
ctx, _ := setupFakeIOMMUSysfs(t)
addDeviceToGroup(t, &ctx, "0000:80:1f.0", "19")
addDeviceToGroup(t, &ctx, "0000:80:1f.4", "19")
addDeviceToGroup(t, &ctx, "0000:80:1f.6", "19")
members, err := ctx.getMembers("0000:80:1f.6")
require.NoError(t, err)
sort.Strings(members)
assert.Equal(t, []string{"0000:80:1f.0", "0000:80:1f.4", "0000:80:1f.6"}, members)
}
func TestGetMembersSingleDevice(t *testing.T) {
ctx, _ := setupFakeIOMMUSysfs(t)
addDeviceToGroup(t, &ctx, "0000:01:00.0", "5")
members, err := ctx.getMembers("0000:01:00.0")
require.NoError(t, err)
assert.Equal(t, []string{"0000:01:00.0"}, members)
}
func TestIsBoundToVfioPci(t *testing.T) {
ctx, tmpDir := setupFakeIOMMUSysfs(t)
addDeviceToGroup(t, &ctx, "0000:80:1f.6", "19")
addDeviceToGroup(t, &ctx, "0000:80:1f.4", "19")
addDeviceToGroup(t, &ctx, "0000:80:1f.0", "19")
// Bound to vfio-pci
bindToVfioPci(t, &ctx, "0000:80:1f.6")
assert.True(t, ctx.isBoundToVfioPci("0000:80:1f.6"))
// Bound to a kernel driver
bindToKernelDriver(t, &ctx, tmpDir, "0000:80:1f.4", "i801_smbus")
assert.False(t, ctx.isBoundToVfioPci("0000:80:1f.4"))
// Not bound to any driver
assert.False(t, ctx.isBoundToVfioPci("0000:80:1f.0"))
}
func TestUnbindSiblings(t *testing.T) {
ctx, tmpDir := setupFakeIOMMUSysfs(t)
addDeviceToGroup(t, &ctx, "0000:80:1f.0", "19")
addDeviceToGroup(t, &ctx, "0000:80:1f.4", "19")
addDeviceToGroup(t, &ctx, "0000:80:1f.5", "19")
addDeviceToGroup(t, &ctx, "0000:80:1f.6", "19")
// Target device bound to vfio-pci (should be skipped)
bindToVfioPci(t, &ctx, "0000:80:1f.6")
// Kernel driver siblings (should be unbound)
bindToKernelDriver(t, &ctx, tmpDir, "0000:80:1f.0", "lpc_ich")
bindToKernelDriver(t, &ctx, tmpDir, "0000:80:1f.4", "i801_smbus")
// 80:1f.5 has no driver (should be skipped)
ctx.unbindSiblings("0000:80:1f.6")
// Verify unbind was written for kernel driver siblings
lpcUnbind := filepath.Join(tmpDir, "bus/pci/drivers/lpc_ich/unbind")
content, err := os.ReadFile(lpcUnbind)
require.NoError(t, err)
assert.Equal(t, "0000:80:1f.0", string(content))
i801Unbind := filepath.Join(tmpDir, "bus/pci/drivers/i801_smbus/unbind")
content, err = os.ReadFile(i801Unbind)
require.NoError(t, err)
assert.Equal(t, "0000:80:1f.4", string(content))
}
func TestUnbindSiblingsSkipsVfioPci(t *testing.T) {
ctx, _ := setupFakeIOMMUSysfs(t)
addDeviceToGroup(t, &ctx, "0000:80:1f.4", "19")
addDeviceToGroup(t, &ctx, "0000:80:1f.6", "19")
// Both bound to vfio-pci — no unbind should happen
bindToVfioPci(t, &ctx, "0000:80:1f.6")
bindToVfioPci(t, &ctx, "0000:80:1f.4")
// Should not panic or error
ctx.unbindSiblings("0000:80:1f.6")
}
func TestReprobeSiblings(t *testing.T) {
ctx, tmpDir := setupFakeIOMMUSysfs(t)
addDeviceToGroup(t, &ctx, "0000:80:1f.0", "19")
addDeviceToGroup(t, &ctx, "0000:80:1f.4", "19")
addDeviceToGroup(t, &ctx, "0000:80:1f.6", "19")
// 80:1f.6 is the target (skip)
// 80:1f.0 already has a driver rebound (skip)
someDriverDir := filepath.Join(tmpDir, "bus/pci/drivers/some_driver")
require.NoError(t, os.MkdirAll(someDriverDir, 0755))
require.NoError(t, os.Symlink(someDriverDir, filepath.Join(ctx.pciDevicesDir, "0000:80:1f.0", "driver")))
// 80:1f.4 has no driver (should be re-probed)
ctx.reprobeSiblings("0000:80:1f.6")
// Verify drivers_probe was written with the unbound device address
content, err := os.ReadFile(ctx.driversProbe)
require.NoError(t, err)
assert.Equal(t, "0000:80:1f.4", string(content))
}
func TestReprobeSiblingsAllBound(t *testing.T) {
ctx, tmpDir := setupFakeIOMMUSysfs(t)
addDeviceToGroup(t, &ctx, "0000:80:1f.4", "19")
addDeviceToGroup(t, &ctx, "0000:80:1f.6", "19")
// Both have drivers — nothing to reprobe
bindToKernelDriver(t, &ctx, tmpDir, "0000:80:1f.4", "i801_smbus")
bindToVfioPci(t, &ctx, "0000:80:1f.6")
ctx.reprobeSiblings("0000:80:1f.6")
// drivers_probe should remain empty
content, err := os.ReadFile(ctx.driversProbe)
require.NoError(t, err)
assert.Empty(t, content)
}