-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
koordlet: supply rdma device
Signed-off-by: wangjianyu.wjy <[email protected]>
wangjianyu.wjy
committed
Dec 5, 2024
1 parent
b36d230
commit c1168ab
Showing
15 changed files
with
492 additions
and
25 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
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
77 changes: 77 additions & 0 deletions
77
pkg/koordlet/metricsadvisor/devices/rdma/collector_rdma.go
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,77 @@ | ||
/* | ||
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 rdma | ||
|
||
import ( | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/klog/v2" | ||
|
||
"github.com/koordinator-sh/koordinator/pkg/features" | ||
"github.com/koordinator-sh/koordinator/pkg/koordlet/metriccache" | ||
"github.com/koordinator-sh/koordinator/pkg/koordlet/metricsadvisor/framework" | ||
) | ||
|
||
const ( | ||
DeviceCollectorName = "RDMA" | ||
) | ||
|
||
type rdmaCollector struct { | ||
enabled bool | ||
} | ||
|
||
func New(opt *framework.Options) framework.DeviceCollector { | ||
return &rdmaCollector{ | ||
enabled: features.DefaultKoordletFeatureGate.Enabled(features.RDMADevices), | ||
} | ||
} | ||
|
||
func (g *rdmaCollector) Shutdown() { | ||
} | ||
|
||
func (g *rdmaCollector) Enabled() bool { | ||
return g.enabled | ||
} | ||
|
||
func (g *rdmaCollector) Setup(fra *framework.Context) { | ||
} | ||
|
||
func (g *rdmaCollector) Run(stopCh <-chan struct{}) { | ||
} | ||
|
||
func (g *rdmaCollector) Started() bool { | ||
return true | ||
} | ||
|
||
func (g *rdmaCollector) Infos() metriccache.Devices { | ||
netDevices, err := GetNetDevice() | ||
if err != nil { | ||
klog.Errorf("failed to get net device: %v", err) | ||
} | ||
return netDevices | ||
} | ||
|
||
func (g *rdmaCollector) GetNodeMetric() ([]metriccache.MetricSample, error) { | ||
return nil, nil | ||
} | ||
|
||
func (g *rdmaCollector) GetPodMetric(uid, podParentDir string, cs []corev1.ContainerStatus) ([]metriccache.MetricSample, error) { | ||
return nil, nil | ||
} | ||
|
||
func (g *rdmaCollector) GetContainerMetric(containerID, podParentDir string, c *corev1.ContainerStatus) ([]metriccache.MetricSample, error) { | ||
return nil, nil | ||
} |
109 changes: 109 additions & 0 deletions
109
pkg/koordlet/metricsadvisor/devices/rdma/collector_rdma_linux.go
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,109 @@ | ||
/* | ||
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 rdma | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/Mellanox/rdmamap" | ||
"github.com/jaypipes/ghw" | ||
"k8s.io/klog/v2" | ||
|
||
"github.com/koordinator-sh/koordinator/pkg/koordlet/metriccache" | ||
"github.com/koordinator-sh/koordinator/pkg/koordlet/metricsadvisor/devices/helper" | ||
"github.com/koordinator-sh/koordinator/pkg/koordlet/util" | ||
"github.com/koordinator-sh/koordinator/pkg/koordlet/util/system" | ||
) | ||
|
||
func GetNetDevice() (metriccache.Devices, error) { | ||
pci, err := ghw.PCI() | ||
if err != nil { | ||
return nil, fmt.Errorf("getNetDevice(): new PCI instance error, %v", err) | ||
} | ||
devices := pci.ListDevices() | ||
if len(devices) == 0 { | ||
klog.Warningf("getNetDevice(): no pci devices") | ||
return nil, nil | ||
} | ||
var netDevices util.RDMADevices | ||
for _, device := range devices { | ||
if !isNetDevice(device.Class.ID) || system.IsSriovVF(device.Address) { | ||
continue | ||
} | ||
netDevice := util.RDMADeviceInfo{ | ||
ID: device.Address, | ||
RDMAResources: rdmamap.GetRdmaDevicesForPcidev(device.Address), | ||
VFEnabled: system.SriovConfigured(device.Address), | ||
VFMap: nil, | ||
Minor: 0, | ||
Labels: nil, | ||
VendorCode: device.Vendor.ID, | ||
DeviceCode: device.Product.ID, | ||
BusID: device.Address, | ||
} | ||
if len(netDevice.RDMAResources) == 0 { | ||
klog.Warningf("getNetDevice(): no rdma device for pci device %s", device.Address) | ||
continue | ||
} | ||
nodeID, pcie, _, err := helper.ParsePCIInfo(device.Address) | ||
if err != nil { | ||
klog.Errorf("getNetDevice(): parse pci device %s error, %v", device.Address, err) | ||
return nil, err | ||
} | ||
netDevice.NodeID = nodeID | ||
netDevice.PCIE = pcie | ||
if netDevice.VFEnabled { | ||
vfList, err := system.GetVFList(netDevice.ID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, vfBDF := range vfList { | ||
vf := util.VirtualFunction{ | ||
ID: vfBDF, | ||
} | ||
if netDevice.VFMap == nil { | ||
netDevice.VFMap = map[string]*util.VirtualFunction{} | ||
} | ||
netDevice.VFMap[vf.ID] = &vf | ||
} | ||
} | ||
netDevices = append(netDevices, netDevice) | ||
} | ||
klog.Infof("rdma netDevices: %+v", netDevices) | ||
return netDevices, nil | ||
} | ||
|
||
const ( | ||
classIDBaseInt = 16 | ||
classIDBitSize = 64 | ||
netDevClassID = 0x02 | ||
) | ||
|
||
func isNetDevice(devClassID string) bool { | ||
devClass, err := parseDeviceClassID(devClassID) | ||
if err != nil { | ||
klog.Warningf("getNetDevice(): unable to parse device class for device %+v %q", devClassID, err) | ||
return false | ||
} | ||
return devClass == netDevClassID | ||
} | ||
|
||
// parseDeviceClassID returns device ID parsed from the string as 64bit integer | ||
func parseDeviceClassID(deviceID string) (int64, error) { | ||
return strconv.ParseInt(deviceID, classIDBaseInt, classIDBitSize) | ||
} |
29 changes: 29 additions & 0 deletions
29
pkg/koordlet/metricsadvisor/devices/rdma/collector_rdma_unsupported.go
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,29 @@ | ||
//go:build !linux | ||
// +build !linux | ||
|
||
/* | ||
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 rdma | ||
|
||
import ( | ||
"github.com/koordinator-sh/koordinator/pkg/koordlet/metriccache" | ||
) | ||
|
||
func GetNetDevice() (metriccache.Devices, error) { | ||
// TODO: support rdma devices on non-linux | ||
return nil, nil | ||
} |
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 system | ||
|
||
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(GetPCIDeviceDir(), 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(GetPCIDeviceDir(), 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(GetPCIDeviceDir(), pciAddr, "physfn") | ||
if _, err := os.Stat(totalVfFilePath); err != nil { | ||
return false | ||
} | ||
return true | ||
} |