-
Notifications
You must be signed in to change notification settings - Fork 5
ON-15256: Report topology info from device plugin #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tcrawley-xilinx
wants to merge
2
commits into
master
Choose a base branch
from
reviews/tcrawley/ON-15256
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,69 +3,97 @@ | |
package deviceplugin | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"os/exec" | ||
"regexp" | ||
"path" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/golang/glog" | ||
) | ||
|
||
// Takes the output from lshw and returns the device name for each solarflare | ||
// device. | ||
func parseOutput(output string) []string { | ||
// "lshw -short -class network" sample output: | ||
// H/W path Device Class Description | ||
// ========================================================= | ||
// /0/100/1b/0 enp2s0f0 network XtremeScale SFC9250 10/25/40/50/100G Ethernet Controller | ||
// /0/100/1b/0.1 enp2s0f1 network XtremeScale SFC9250 10/25/40/50/100G Ethernet Controller | ||
// /0/100/1c.1/0 eno1 network NetXtreme BCM5720 Gigabit Ethernet PCIe | ||
// /0/100/1c.1/0.1 eno2 network NetXtreme BCM5720 Gigabit Ethernet PCIe | ||
const ( | ||
sysClassNetPath = "/sys/class/net/" | ||
solarflareVendor = "0x1924" | ||
) | ||
|
||
func isSFCNic(devicePath string) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be worth using isOnloadableNic or isSupportedNic? Just in case we are asked to support another vendor sometime in the future. |
||
deviceDir, err := os.Stat(path.Join(devicePath, "device")) | ||
if errors.Is(err, os.ErrNotExist) { | ||
// Not a physical device, so won't have the "vendor" file | ||
return false | ||
} else if err != nil { | ||
glog.Errorf("Failed to stat %s (%v)", devicePath, err) | ||
return false | ||
} | ||
if !deviceDir.IsDir() { | ||
return false | ||
} | ||
|
||
lines := strings.Split(output, "\n") | ||
data, err := os.ReadFile(path.Join(devicePath, "device", "vendor")) | ||
if errors.Is(err, os.ErrNotExist) { | ||
// File doesn't exist but that is fine | ||
return false | ||
} else if err != nil { | ||
glog.Errorf("Error reading %s (%v)", | ||
path.Join(devicePath, "device", "vendor"), err) | ||
return false | ||
} | ||
|
||
vendor := strings.TrimSuffix(string(data), "\n") | ||
return vendor == solarflareVendor | ||
} | ||
|
||
var interfaces []string | ||
// Get numa node from sysfs files. | ||
// -1 means no specific numa node / unknown | ||
ivatet-amd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func getNumaNode(devicePath string) int64 { | ||
data, err := os.ReadFile(path.Join(devicePath, "device", "numa_node")) | ||
if errors.Is(err, os.ErrNotExist) { | ||
// File doesn't exist but that is fine, return -1 | ||
return -1 | ||
} else if err != nil { | ||
glog.Errorf("Error reading %s (%v)", | ||
path.Join(devicePath, "device", "vendor"), err) | ||
return -1 | ||
} | ||
numaString := strings.TrimSuffix(string(data), "\n") | ||
node, err := strconv.ParseInt(numaString, 10, 64) | ||
if err != nil { | ||
glog.Errorf("Error parse int from string %s (%v)", | ||
numaString, err) | ||
return -1 | ||
} | ||
return node | ||
} | ||
|
||
// Assume that we are running as root, if not then we would have to skip | ||
// an additional line at the start of the output | ||
skip_lines := 2 | ||
end_lines := 1 | ||
if os.Geteuid() != 0 { | ||
skip_lines = 3 | ||
end_lines = 2 | ||
func readSysFiles() ([]nic, error) { | ||
infos, err := os.ReadDir(sysClassNetPath) | ||
if err != nil { | ||
glog.Errorf("Error reading %s (%v)", sysClassNetPath, err) | ||
return []nic{}, err | ||
} | ||
|
||
for _, line := range lines[skip_lines : len(lines)-end_lines] { | ||
// This regex makes the assumption that all interface names only | ||
// contain either lowercase letters or numbers. If that is not true, | ||
// then this should be updated to reflect that. | ||
r := regexp.MustCompile("([a-z0-9]+) *network *.*SFC") | ||
out := r.FindStringSubmatch(line) | ||
if out != nil { | ||
// It is safe to access out[1] here since the return value of | ||
// FindStringSubmatch is an array where the first value is the | ||
// whole string and any subsequent values are the submatches. | ||
// In this case since there is a submatch that should match the | ||
// device name if FindStringSubmatch returns non-nil then there | ||
// will be at least 2 elements in the return array. | ||
interfaces = append(interfaces, out[1]) | ||
interfaces := []nic{} | ||
for _, info := range infos { | ||
devicePath := path.Join(sysClassNetPath, info.Name()) | ||
if !isSFCNic(devicePath) { | ||
continue | ||
} | ||
nic := nic{ | ||
name: info.Name(), | ||
numa: getNumaNode(devicePath), | ||
} | ||
interfaces = append(interfaces, nic) | ||
} | ||
return interfaces | ||
return interfaces, nil | ||
} | ||
|
||
// Returns a list of the Solarflare interfaces present on the node | ||
func queryNics() ([]string, error) { | ||
// Depending on what information we are looking for in the output I think it | ||
// is quite tempting to retrieve the information in a json format, then | ||
// parse this using golang's built-in features. | ||
bytes, err := exec.Command("lshw", "-short", "-class", "network").CombinedOutput() | ||
output := string(bytes) | ||
func queryNics() ([]nic, error) { | ||
interfaces, err := readSysFiles() | ||
if err != nil { | ||
glog.Error(output) | ||
glog.Errorf("error while listing sfc devices : %v", err) | ||
return nil, err | ||
glog.Errorf("Failed to list interfaces (%v)", err) | ||
return []nic{}, err | ||
} | ||
interfaces := parseOutput(output) | ||
return interfaces, nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.