|
6 | 6 | "errors"
|
7 | 7 | "os"
|
8 | 8 | "path"
|
| 9 | + "strconv" |
9 | 10 | "strings"
|
10 | 11 |
|
11 | 12 | "github.com/golang/glog"
|
@@ -43,27 +44,56 @@ func isSFCNic(devicePath string) bool {
|
43 | 44 | return vendor == solarflareVendor
|
44 | 45 | }
|
45 | 46 |
|
46 |
| -func readSysFiles() ([]string, error) { |
| 47 | +// Get numa node from sysfs files. |
| 48 | +// -1 means no specific numa node / unknown |
| 49 | +func getNumaNode(devicePath string) int64 { |
| 50 | + data, err := os.ReadFile(path.Join(devicePath, "device", "numa_node")) |
| 51 | + if errors.Is(err, os.ErrNotExist) { |
| 52 | + // File doesn't exist but that is fine, return -1 |
| 53 | + return -1 |
| 54 | + } else if err != nil { |
| 55 | + glog.Errorf("Error reading %s (%v)", |
| 56 | + path.Join(devicePath, "device", "vendor"), err) |
| 57 | + return -1 |
| 58 | + } |
| 59 | + numaString := strings.TrimSuffix(string(data), "\n") |
| 60 | + node, err := strconv.ParseInt(numaString, 10, 64) |
| 61 | + if err != nil { |
| 62 | + glog.Errorf("Error parse int from string %s (%v)", |
| 63 | + numaString, err) |
| 64 | + return -1 |
| 65 | + } |
| 66 | + return node |
| 67 | +} |
| 68 | + |
| 69 | +func readSysFiles() ([]nic, error) { |
47 | 70 | infos, err := os.ReadDir(sysClassNetPath)
|
48 | 71 | if err != nil {
|
49 | 72 | glog.Errorf("Error reading %s (%v)", sysClassNetPath, err)
|
50 |
| - return []string{}, err |
| 73 | + return []nic{}, err |
51 | 74 | }
|
52 |
| - interfaces := []string{} |
| 75 | + |
| 76 | + interfaces := []nic{} |
53 | 77 | for _, info := range infos {
|
54 |
| - if isSFCNic(path.Join(sysClassNetPath, info.Name())) { |
55 |
| - interfaces = append(interfaces, info.Name()) |
| 78 | + devicePath := path.Join(sysClassNetPath, info.Name()) |
| 79 | + if !isSFCNic(devicePath) { |
| 80 | + continue |
| 81 | + } |
| 82 | + nic := nic{ |
| 83 | + name: info.Name(), |
| 84 | + numa: getNumaNode(devicePath), |
56 | 85 | }
|
| 86 | + interfaces = append(interfaces, nic) |
57 | 87 | }
|
58 | 88 | return interfaces, nil
|
59 | 89 | }
|
60 | 90 |
|
61 | 91 | // Returns a list of the Solarflare interfaces present on the node
|
62 |
| -func queryNics() ([]string, error) { |
| 92 | +func queryNics() ([]nic, error) { |
63 | 93 | interfaces, err := readSysFiles()
|
64 | 94 | if err != nil {
|
65 | 95 | glog.Errorf("Failed to list interfaces (%v)", err)
|
66 |
| - return []string{}, err |
| 96 | + return []nic{}, err |
67 | 97 | }
|
68 | 98 | return interfaces, nil
|
69 | 99 | }
|
0 commit comments