-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: wangjianyu.wjy <[email protected]>
- Loading branch information
wangjianyu.wjy
committed
Dec 4, 2024
1 parent
b36d230
commit d0ad0cb
Showing
13 changed files
with
604 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
Copyright 2022 The Koordinator Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package helper | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
const ( | ||
configuredVfFile = "sriov_numvfs" | ||
) | ||
|
||
// SriovConfigured returns true if sriov_numvfs reads > 0 else false | ||
func SriovConfigured(addr string) bool { | ||
return GetVConfigured(addr) > 0 | ||
} | ||
|
||
func extractNumber(pfDir string, s string) int { | ||
num, _ := strconv.Atoi(strings.TrimPrefix(s, fmt.Sprintf("%s/virtfn", pfDir))) | ||
return num | ||
} | ||
|
||
// GetVFList returns a List containing PCI addr for all VF discovered in a given PF | ||
func GetVFList(pf string) (vfList []string, err error) { | ||
vfList = make([]string, 0) | ||
pfDir := filepath.Join(SysBusPci, pf) | ||
_, err = os.Lstat(pfDir) | ||
if err != nil { | ||
err = fmt.Errorf("error. Could not get PF directory information for device: %s, Err: %v", pf, err) | ||
return | ||
} | ||
|
||
vfDirs, err := filepath.Glob(filepath.Join(pfDir, "virtfn*")) | ||
|
||
if err != nil { | ||
err = fmt.Errorf("error reading VF directories %v", err) | ||
return | ||
} | ||
//TODO 排序 | ||
sort.Slice(vfDirs, func(i, j int) bool { | ||
return extractNumber(pfDir, vfDirs[i]) < extractNumber(pfDir, vfDirs[j]) | ||
}) | ||
|
||
// Read all VF directory and get add VF PCI addr to the vfList | ||
for _, dir := range vfDirs { | ||
dirInfo, err := os.Lstat(dir) | ||
if err == nil && (dirInfo.Mode()&os.ModeSymlink != 0) { | ||
linkName, err := filepath.EvalSymlinks(dir) | ||
if err == nil { | ||
vfLink := filepath.Base(linkName) | ||
vfList = append(vfList, vfLink) | ||
} | ||
} | ||
} | ||
return | ||
} | ||
|
||
// GetVConfigured returns number of VF configured for a PF | ||
func GetVConfigured(pf string) int { | ||
configuredVfPath := filepath.Join(SysBusPci, pf, configuredVfFile) | ||
vfs, err := os.ReadFile(configuredVfPath) | ||
if err != nil { | ||
return 0 | ||
} | ||
configuredVFs := bytes.TrimSpace(vfs) | ||
numConfiguredVFs, err := strconv.Atoi(string(configuredVFs)) | ||
if err != nil { | ||
return 0 | ||
} | ||
return numConfiguredVFs | ||
} | ||
|
||
// IsSriovVF check if a pci device has link to a PF | ||
func IsSriovVF(pciAddr string) bool { | ||
totalVfFilePath := filepath.Join(SysBusPci, pciAddr, "physfn") | ||
if _, err := os.Stat(totalVfFilePath); err != nil { | ||
return false | ||
} | ||
return true | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
Copyright 2022 The Koordinator Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package helper | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strconv" | ||
|
||
"k8s.io/klog/v2" | ||
|
||
"github.com/koordinator-sh/koordinator/pkg/koordlet/util/system" | ||
) | ||
|
||
var ( | ||
SysBusPci = "/sys/bus/pci/devices" | ||
DevDir = "/dev" | ||
|
||
pcieRegexp = regexp.MustCompile(`pci\d{4}:[0-9a-fA-F]{2}`) | ||
) | ||
|
||
func ParsePCIInfo(busID string) (int32, string, string, error) { | ||
//klog.V(4).Infof("ParsePCIInfo: busID=%s", busID) | ||
nodeID, err := getNUMANodeID(busID) | ||
if err != nil { | ||
//klog.V(4).Infof("ParsePCIInfo: return err:%v busid:", err, busID) | ||
return 0, "", "", fmt.Errorf("failed to parse NUMA Node ID, err: %w", err) | ||
} | ||
//klog.V(4).Infof("ParsePCIInfo: go on") | ||
pcie, err := getPCIERootComplexID(busID) | ||
if err != nil { | ||
//klog.V(4).Infof("failed to parse PCIE ID, err: %v", err) | ||
return 0, "", "", fmt.Errorf("failed to parse PCIE ID, err: %w", err) | ||
} | ||
//klog.V(4).Infof("ParsePCIInfo: nodeID=%s pcie=%s busID=%s", nodeID, pcie, busID) | ||
return nodeID, pcie, busID, nil | ||
} | ||
|
||
func getPCIERootComplexID(bdf string) (string, error) { | ||
path, err := filepath.EvalSymlinks(filepath.Join(system.GetPCIDeviceDir(), bdf)) | ||
if err != nil { | ||
return "", err | ||
} | ||
return parsePCIEID(path), err | ||
} | ||
|
||
func parsePCIEID(path string) string { | ||
result := pcieRegexp.FindAllStringSubmatch(path, -1) | ||
if len(result) == 0 || len(result[0]) == 0 { | ||
return "" | ||
} | ||
return result[0][0] | ||
} | ||
|
||
func getNUMANodeID(bdf string) (int32, error) { | ||
path := filepath.Join(system.GetPCIDeviceDir(), bdf, "numa_node") | ||
klog.V(4).Infof("ParsePCIInfo: path=%s", path) | ||
data, err := os.ReadFile(filepath.Join(system.GetPCIDeviceDir(), bdf, "numa_node")) | ||
if err != nil { | ||
return -1, err | ||
} | ||
nodeID, err := strconv.Atoi(string(bytes.TrimSpace(data))) | ||
if err != nil { | ||
return 0, err | ||
} | ||
if nodeID == -1 { | ||
nodeID = 0 | ||
} | ||
return int32(nodeID), nil | ||
} |
Oops, something went wrong.