-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguest_linux.go
256 lines (209 loc) · 5.55 KB
/
guest_linux.go
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//go:build linux
package ivshmem
import (
"fmt"
"os"
"sort"
"strconv"
"strings"
"golang.org/x/sys/unix"
)
const (
PCI_PATH = "/sys/bus/pci/devices"
IVSHMEM_VENDOR = "0x1af4" // Red Hat, Inc.
IVSHMEM_DEVICE = "0x1110" // Inter-VM shared memory
)
// ListDevices lists the available ivshmem devices by their locations. The devices are identified by their vendor and device ids.
func ListDevices() ([]PCILocation, error) {
devices, err := listIvshmemPCIRaw()
if err != nil {
return nil, fmt.Errorf("get raw devices: %w", err)
}
result := make([]PCILocation, 0)
for _, dev := range devices {
loc, err := convertLocation(dev)
if err != nil {
fmt.Println(err)
continue
}
result = append(result, *loc)
}
// Sort by bus -> device -> function
sort.Slice(result, func(a, b int) bool {
if result[a].bus < result[b].bus {
return true
} else if result[a].bus > result[b].bus {
return false
}
if result[a].device < result[b].device {
return true
} else if result[a].device > result[b].device {
return false
}
if result[a].function < result[b].function {
return true
} else if result[a].function > result[b].function {
return false
}
return true
})
return result, nil
}
// convertLocation converts the PCI folder name to a PCILocation (for example "0000:08:00.0").
func convertLocation(locationDescription string) (*PCILocation, error) {
parts := strings.Split(locationDescription, ":")
if len(parts) != 3 {
return nil, fmt.Errorf("invalid location description: %s", locationDescription)
}
bus, err := strconv.ParseUint(parts[1], 16, 8)
if err != nil {
return nil, fmt.Errorf("parse bus: %w", err)
}
devFunc := strings.Split(parts[2], ".")
if len(devFunc) != 2 {
return nil, fmt.Errorf("invalid device/function description: %s", devFunc)
}
device, err := strconv.ParseUint(devFunc[0], 16, 8)
if err != nil {
return nil, fmt.Errorf("parse device: %w", err)
}
function, err := strconv.ParseUint(devFunc[1], 16, 8)
if err != nil {
return nil, fmt.Errorf("parse function: %w", err)
}
return &PCILocation{
bus: uint8(bus),
device: uint8(device),
function: uint8(function),
}, nil
}
// Guest allows to map a shared memory region.
type Guest struct {
loc PCILocation
devPath string
mapped bool
sharedMem []byte
size uint64
}
// NewGuest returns a new Guest based on the PCI location.
func NewGuest(location PCILocation) (*Guest, error) {
devices, err := listIvshmemPCIRaw()
if err != nil {
return nil, fmt.Errorf("get raw devices: %w", err)
}
var found bool
var idx = -1
for i, dev := range devices {
loc, err := convertLocation(dev)
if err != nil {
return nil, fmt.Errorf("convert location: %w", err)
}
if *loc == location {
found = true
idx = i
}
}
if !found {
return nil, ErrCannotFindDevice
}
path := fmt.Sprintf("%s/%s/%s", PCI_PATH, devices[idx], "resource2")
return &Guest{
loc: location,
devPath: path,
}, nil
}
// Map maps the memory into the program address space.
func (g *Guest) Map() error {
if g.mapped {
return ErrAlreadyMapped
}
stat, err := os.Stat(g.devPath)
if err != nil {
return fmt.Errorf("get size: %w", err)
}
file, err := os.OpenFile(g.devPath, os.O_RDWR, 0o600)
if err != nil {
return fmt.Errorf("open device file: %w", err)
}
defer file.Close()
sharedMem, err := unix.Mmap(int(file.Fd()), 0, int(stat.Size()), unix.PROT_READ|unix.PROT_WRITE, unix.MAP_SHARED)
if err != nil {
return fmt.Errorf("mmap: %w", err)
}
g.sharedMem = sharedMem
g.size = uint64(stat.Size())
g.mapped = true
return nil
}
// Unmap unmaps the memory.
func (g Guest) Unmap() error {
if !g.mapped {
return ErrAlreadyUnmapped
}
if err := unix.Munmap(g.sharedMem); err != nil {
return fmt.Errorf("munmap: %w", err)
}
g.mapped = false
return nil
}
// System returns the guest system type.
func (g Guest) System() string {
return "Linux"
}
// Size returns the shared memory size in bytes.
func (g Guest) Size() uint64 {
return g.size
}
// DevPath returns the device path.
func (g Guest) DevPath() string {
return g.devPath
}
// SharedMem returns the shared memory region. Panics if the shared memory isn't mapped yet.
func (g Guest) SharedMem() []byte {
if !g.mapped {
panic("tried to access unmapped memory")
}
return g.sharedMem
}
// Location returns the PCI location of the device.
func (g Guest) Location() PCILocation {
return g.loc
}
// Sync makes sure the changes made to the shared memory are synced.
func (g Guest) Sync() error {
return unix.Msync(g.sharedMem, unix.MS_SYNC)
}
// listIvshmemPCIRaw returns the ivshmem PCI names as seen in PCI_PATH.
func listIvshmemPCIRaw() ([]string, error) {
entry, err := os.ReadDir(PCI_PATH)
if err != nil {
return nil, fmt.Errorf("read pci dir: %w", err)
}
devices := make([]string, 0)
for _, test := range entry {
if len(test.Name()) == 12 && len(strings.Split(test.Name(), ":")) == 3 {
devices = append(devices, test.Name())
}
}
ivshmemDevices := make([]string, 0)
for _, dev := range devices {
data, err := os.ReadFile(fmt.Sprintf("%s/%s/%s", PCI_PATH, dev, "vendor"))
if err != nil {
return nil, fmt.Errorf("vendor read: %w", err)
}
vendorName := strings.TrimSpace(string(data))
if vendorName != IVSHMEM_VENDOR {
continue
}
data, err = os.ReadFile(fmt.Sprintf("%s/%s/%s", PCI_PATH, dev, "device"))
if err != nil {
return nil, fmt.Errorf("device read: %w", err)
}
deviceName := strings.TrimSpace(string(data))
if deviceName != IVSHMEM_DEVICE {
continue
}
ivshmemDevices = append(ivshmemDevices, dev)
}
return ivshmemDevices, nil
}