forked from lf-edge/eve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifnametopci.go
266 lines (245 loc) · 6.96 KB
/
ifnametopci.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
257
258
259
260
261
262
263
264
265
266
// Copyright (c) 2018 Zededa, Inc.
// SPDX-License-Identifier: Apache-2.0
// Read the symlinks in /sys/class/net/*/device to print a mapping
// from ifname to PCI-ID
package types
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"regexp"
"strings"
"github.com/lf-edge/eve/pkg/pillar/base"
"github.com/vishvananda/netlink"
)
const basePath = "/sys/class/net"
const pciPath = "/sys/bus/pci/devices"
// Returns the long PCI IDs
func ifNameToPci(log *base.LogObject, ifName string) (string, error) {
// Match for PCI IDs
re := regexp.MustCompile("([0-9a-f]){4}:([0-9a-f]){2}:([0-9a-f]){2}.[ls0-9a-f]")
ifPath := basePath + "/" + ifName
devPath := ifPath + "/device"
info, err := os.Lstat(devPath)
if err != nil {
if !strings.HasPrefix(ifName, "eth") {
if !os.IsNotExist(err) {
log.Errorln(err)
}
return "", err
}
// Try alternate since the PCI device can be kethN
// if ifName is ethN
ifName = "k" + ifName
ifPath = basePath + "/" + ifName
devPath = ifPath + "/device"
info, err = os.Lstat(devPath)
if err != nil {
if !os.IsNotExist(err) {
log.Errorln(err)
}
return "", err
}
log.Noticef("ifNameToPci using alternate %s", ifName)
}
if (info.Mode() & os.ModeSymlink) == 0 {
log.Errorf("Skipping non-symlink %s\n", devPath)
return "", fmt.Errorf("Not a symlink %s", devPath)
}
link, err := os.Readlink(devPath)
if err != nil {
return "", err
}
target := path.Base(link)
if re.MatchString(target) {
return target, nil
}
log.Noticef("Not PCI %s - try fallback for %s", target, ifName)
// Try fallback to handle nested virtualization
info, err = os.Lstat(ifPath)
if err != nil {
log.Noticef("Fallback failed: %s", err)
return target, fmt.Errorf("Not PCI %s", target)
}
if (info.Mode() & os.ModeSymlink) == 0 {
log.Noticef("Fallback not symlink")
return target, fmt.Errorf("Not PCI %s", target)
}
link, err = os.Readlink(ifPath)
if err != nil {
log.Noticef("Fallback readlink failed: %s", err)
return target, fmt.Errorf("Not PCI %s", target)
}
link = path.Clean(link)
components := strings.Split(link, "/")
for _, c := range components {
if re.MatchString(c) {
log.Noticef("Fallback found %s", c)
return c, nil
}
}
return target, fmt.Errorf("Not PCI %s", target)
}
// PCILongToShort returns the PCI ID without the domain id
func PCILongToShort(long string) string {
return strings.SplitAfterN(long, ":", 2)[1]
}
// PCISameController compares the PCI-ID without comparing the controller
func PCISameController(long1 string, long2 string) bool {
if long1 == "" || long2 == "" {
return false
}
ctrl1 := strings.SplitAfter(long1, ".")[0]
ctrl2 := strings.SplitAfter(long2, ".")[0]
return ctrl1 == ctrl2
}
// PCIGetIOMMUGroup returns IOMMU group tag as seen by the control domain
func PCIGetIOMMUGroup(long string) (string, error) {
pathDev := pciPath + "/" + long + "/iommu_group"
if iommuPath, err := os.Readlink(pathDev); err != nil {
return "", fmt.Errorf("can't determine iommu group for %s (%v)", long, err)
} else {
return path.Base(iommuPath), nil
}
}
// Check if an ID like 0000:03:00.0 exists
func pciLongExists(long string) bool {
path := pciPath + "/" + long
_, err := os.Stat(path)
return err == nil
}
// Return a string likely to be unique for the device.
// Used to make sure devices don't move around
// Returns exist bool, string
func PciLongToUnique(log *base.LogObject, long string) (bool, string) {
if !pciLongExists(long) {
return false, ""
}
devPath := pciPath + "/" + long + "/firmware_node"
info, err := os.Lstat(devPath)
if err != nil {
log.Errorln(err)
return false, ""
}
if (info.Mode() & os.ModeSymlink) == 0 {
log.Errorf("Skipping non-symlink %s\n", devPath)
return true, ""
}
link, err := os.Readlink(devPath)
if err != nil {
log.Errorln(err)
return true, ""
}
return true, link
}
// PciLongToIfname return the interface name for a network PCI device.
// This is used to make sure devices don't move around
// Returns exist bool, string
func PciLongToIfname(log *base.LogObject, long string) (bool, string) {
if !pciLongExists(long) {
return false, ""
}
devPath := pciPath + "/" + long + "/net"
locations, err := ioutil.ReadDir(devPath)
if err != nil {
log.Errorf("Dir %s is missing", devPath)
return false, ""
}
if len(locations) == 0 {
log.Errorf("Dir %s is empty", devPath)
return false, ""
}
if len(locations) != 1 {
log.Errorf("Dir %s has multiple: %d", devPath, len(locations))
for _, location := range locations {
log.Errorf("Dir %s has %s", devPath, location)
}
return false, ""
}
ifname := locations[0].Name()
log.Functionf("PciLongToIfname(%s) %s", long, ifname)
return true, ifname
}
// IoBundleToPci returns the long PCI ID if the bundle refers to a PCI controller.
// Checks if PCI ID exists on system. Returns null strings for non-PCI
// devices since we can't check if they exist.
// This can handle aliases like Ifname.
func IoBundleToPci(log *base.LogObject, ib *IoBundle) (string, error) {
var long string
if ib.PciLong != "" {
long = ib.PciLong
// Check if model matches
if ib.Ifname != "" {
l, err := ifNameToPci(log, ib.Ifname)
rename := false
if err == nil {
if long != l {
log.Warnf("Ifname and PciLong mismatch: %s vs %s for %s",
l, long, ib.Ifname)
rename = true
}
} else {
rename = true
}
if rename {
found, ifname := PciLongToIfname(log, long)
if found && ib.Ifname != ifname {
log.Warnf("%s/%s moved to %s",
ib.Ifname, long, ifname)
IfRename(log, ifname, ib.Ifname)
}
}
}
} else if ib.Ifname != "" {
var err error
long, err = ifNameToPci(log, ib.Ifname)
if err != nil {
return long, err
}
} else {
return "", nil
}
if !pciLongExists(long) {
errStr := fmt.Sprintf("PCI device %s/%s long %s does not exist",
ib.Phylabel, ib.Logicallabel, long)
return long, errors.New(errStr)
}
return long, nil
}
// IfRename brings down the interface, renames it, and brings it back up
func IfRename(log *base.LogObject, ifname string, newIfname string) error {
log.Functionf("IfRename %s to %s", ifname, newIfname)
link, err := netlink.LinkByName(ifname)
if link == nil {
log.Errorf("LinkByname on %s failed: %s", ifname, err)
return err
}
if err := netlink.LinkSetDown(link); err != nil {
log.Errorf("LinkSetDown on %s failed: %s", ifname, err)
return err
}
err = netlink.LinkSetName(link, newIfname)
if err != nil {
log.Errorf("LinkSetName failed: %s", err)
// Restore
netlink.LinkSetUp(link)
return err
}
if err := netlink.LinkSetUp(link); err != nil {
log.Errorf("LinkSetUp on %s failed: %s", ifname, err)
return err
}
return nil
}
// PCIIsBootVga return 'true' if VGA device is a console device
func PCIIsBootVga(log *base.LogObject, long string) (bool, error) {
log.Functionf("PCIIsBootVga %s", long)
bootVgaFile := pciPath + "/" + long + "/boot_vga"
if isBoot, err := ioutil.ReadFile(bootVgaFile); err != nil {
return false, err
} else {
return strings.TrimSpace(strings.TrimSuffix(string(isBoot), "\n")) == "1", err
}
}