-
Notifications
You must be signed in to change notification settings - Fork 24
fix: Properly determine PCI address for hierarchial paths #210
fix: Properly determine PCI address for hierarchial paths #210
Conversation
|
will this not be handled by the new library https://github.com/jaypipes/ghw ? |
|
Sadly no. It will also fail to interpret something like |
should we report this to Jay so we do not have to keep the workaround? |
|
This is what gemini tells me
it seems the basename is the key and AFAIK we are already using |
aojea
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like to hold until we have more real evidance that using readlink to get the path is not valid
I'm not sure what you mean by "real evidence", but the following is based on actual (different) machines:
...which means you get the PCI address if you follow the link
...which means the link takes you back to virtio3 and NOT it's PCI address.
Sure I'll report it. But ghw is also not optimal (or atleast will require additional changes) since it can only list all NICs with their data and then we have to build a map on top of that. It does not (yet) expose a function where we simply pass in a single NIC and get back it's equivalent info. We can work towards improving in ghw (assuming that is seen acceptable to ghw maintainers) but it's going to take time. |
|
|
||
| // pciAddressRegex is used to identify a PCI address within a string. | ||
| // It matches patterns like "0000:00:04.0" or "00:04.0". | ||
| var pciAddressRegex = regexp.MustCompile(`^(?:([0-9a-fA-F]{4}):)?([0-9a-fA-F]{2}):([0-9a-fA-F]{2})\.([0-9a-fA-F])$`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, this always returns 5 elements in a match package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(?:([0-9a-fA-F]{4}):)?([0-9a-fA-F]{2}):([0-9a-fA-F]{2})\.([0-9a-fA-F])$`)
fmt.Printf("%q\n", re.FindStringSubmatch("0000:00:04.0"))
fmt.Printf("%q\n", re.FindStringSubmatch("00:04.0"))
}
["0000:00:04.0" "0000" "00" "04" "0"]
["00:04.0" "" "00" "04" "0"]
| f := strings.Split(pci[2], ".") | ||
| if len(f) != 2 { | ||
| return nil, fmt.Errorf("could not find corresponding PCI device and function: %v", pci) | ||
| if len(matches) == 5 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was the part that needs a comment explaining that the regex always return 5 , it is very magic https://github.com/google/dranet/pull/210/files#r2304914785
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense to document it. Added a comment.
aojea
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
we need to document the magic number 5 only, the rest LGTM
19f6572 to
9769ad5
Compare
9769ad5 to
448f4d0
Compare
448f4d0 to
0a8cb8d
Compare
The previous PCI address discovery assumed a fixed depth in the sysfs path gives the correct PCI address. That is correct for paths like
/sys/devices/pci0000:00/0000:00:04.0/virtio1which do not have a PCI hierarchy and we returne the 4th part (0000:00:04.0as the PCI address)This doesn't work when the device's sysfs path has a deeper hierarchy, like
/sys/devices/pci0000:8c/0000:8c:00.0/0000:8d:00.0/0000:8e:02.0/0000:91:00.0/net/eth3, where the PCI address is not the 4th part and is instead0000:91:00.0