File: utils.go

package info (click to toggle)
incus 6.0.5-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,788 kB
  • sloc: sh: 16,313; ansic: 3,121; python: 457; makefile: 337; ruby: 51; sql: 50; lisp: 6
file content (237 lines) | stat: -rw-r--r-- 5,371 bytes parent folder | download | duplicates (3)
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
//go:build linux

package resources

import (
	"encoding/hex"
	"errors"
	"fmt"
	"io/fs"
	"os"
	"path/filepath"
	"slices"
	"strconv"
	"strings"
)

var sysBusPci = "/sys/bus/pci/devices"

func isDir(name string) bool {
	stat, err := os.Stat(name)
	if err != nil {
		return false
	}

	return stat.IsDir()
}

func readUint(path string) (uint64, error) {
	content, err := os.ReadFile(path)
	if err != nil {
		return 0, err
	}

	value, err := strconv.ParseUint(strings.TrimSpace(string(content)), 10, 64)
	if err != nil {
		return 0, err
	}

	return value, nil
}

func readInt(path string) (int64, error) {
	content, err := os.ReadFile(path)
	if err != nil {
		return -1, err
	}

	value, err := strconv.ParseInt(strings.TrimSpace(string(content)), 10, 64)
	if err != nil {
		return -1, err
	}

	return value, nil
}

func sysfsExists(path string) bool {
	_, err := os.Lstat(path)
	return err == nil
}

func sysfsNumaNode(path string) (uint64, error) {
	// List all the directory entries
	entries, err := os.ReadDir(path)
	if err != nil {
		return 0, err
	}

	// Iterate and look for NUMA
	for _, entry := range entries {
		entryName := entry.Name()

		if strings.HasPrefix(entryName, "node") && sysfsExists(filepath.Join(path, entryName, "numastat")) {
			node := strings.TrimPrefix(entryName, "node")

			nodeNumber, err := strconv.ParseUint(node, 10, 64)
			if err != nil {
				return 0, err
			}

			// Return the node we found
			return nodeNumber, nil
		}
	}

	// Didn't find a NUMA node for the device, assume single-node
	return 0, nil
}

func hasBit(n uint32, pos uint) bool {
	val := n & (1 << pos)
	return (val > 0)
}

func hasBitField(n []uint32, bit uint) bool {
	return (n[bit/32] & (1 << (bit % 32))) != 0
}

func udevDecode(s string) (string, error) {
	// Inverse of https://github.com/systemd/systemd/blob/main/src/shared/device-nodes.c#L19
	ret := ""
	for i := 0; i < len(s); i++ {
		// udev converts non-devnode supported chars to four byte encode hex strings.
		if s[i] == '\\' && i+4 <= len(s) && s[i+1] == 'x' {
			hexValue := s[i+2 : i+4]

			strValue, err := hex.DecodeString(hexValue)
			if err != nil {
				return ret, err
			}

			ret += string(strValue)
			i += 3
		} else {
			ret += s[i : i+1]
		}
	}

	return ret, nil
}

func pciAddress(devicePath string) (string, error) {
	deviceDeviceDir, err := getDeviceDir(devicePath)
	if err != nil {
		return "", err
	}

	// Check if we have a subsystem listed at all.
	if !sysfsExists(filepath.Join(deviceDeviceDir, "subsystem")) {
		return "", nil
	}

	// Track down the device.
	linkTarget, err := filepath.EvalSymlinks(deviceDeviceDir)
	if err != nil {
		return "", fmt.Errorf("Failed to find %q: %w", deviceDeviceDir, err)
	}

	// Extract the subsystem.
	subsystemTarget, err := filepath.EvalSymlinks(filepath.Join(linkTarget, "subsystem"))
	if err != nil {
		return "", fmt.Errorf("Failed to find %q: %w", filepath.Join(deviceDeviceDir, "subsystem"), err)
	}

	subsystem := filepath.Base(subsystemTarget)

	if subsystem == "virtio" {
		// If virtio, consider the parent.
		linkTarget = filepath.Dir(linkTarget)
		subsystemTarget, err := filepath.EvalSymlinks(filepath.Join(linkTarget, "subsystem"))
		if err != nil {
			return "", fmt.Errorf("Failed to find %q: %w", filepath.Join(deviceDeviceDir, "subsystem"), err)
		}

		subsystem = filepath.Base(subsystemTarget)
	}

	if subsystem != "pci" {
		return "", nil
	}

	// Address is the last entry.
	return filepath.Base(linkTarget), nil
}

// usbAddress returns the suspected USB address (bus:dev) for the device.
func usbAddress(devicePath string) (string, error) {
	// Resolve symlink.
	devicePath, err := filepath.EvalSymlinks(devicePath)
	if err != nil {
		return "", fmt.Errorf("Failed to resolve device symlink: %w", err)
	}

	// Check if it looks like a USB device.
	if !strings.Contains(devicePath, "/usb") {
		return "", nil
	}

	path := devicePath
	for {
		// Avoid infinite loops.
		if path == "" || path == "/" {
			return "", nil
		}

		// Check if we found a usb device path.
		if !sysfsExists(filepath.Join(path, "busnum")) || !sysfsExists(filepath.Join(path, "devnum")) {
			path = filepath.Dir(path)
			continue
		}

		// Bus address.
		bus, err := readUint(filepath.Join(path, "busnum"))
		if err != nil {
			return "", fmt.Errorf("Unable to parse USB bus addr: %w", err)
		}

		// Device address.
		dev, err := readUint(filepath.Join(path, "devnum"))
		if err != nil {
			return "", fmt.Errorf("Unable to parse USB device addr: %w", err)
		}

		return fmt.Sprintf("%d:%d", bus, dev), nil
	}
}

// getDeviceDir returns the directory which contains device information needed for a device.
// It appends /device to the path until it ends up to a child /device which is a regular file.
// This function is needed for devices which include sub-devices like wwan.
func getDeviceDir(devicePath string) (string, error) {
	for {
		deviceDir := filepath.Join(devicePath, "device")
		fileInfo, err := os.Stat(deviceDir)
		if errors.Is(err, fs.ErrNotExist) {
			break
		} else if err != nil {
			return "", fmt.Errorf("Unable to get file info for %q: %w", deviceDir, err)
		} else if fileInfo.Mode().IsRegular() {
			break
		}

		devicePath = deviceDir
	}

	return devicePath, nil
}

func sortedMapKeys[M ~map[int64]V, V any](m M) []int64 {
	r := make([]int64, 0, len(m))
	for k := range m {
		r = append(r, k)
	}

	slices.Sort(r)

	return r
}