2017-11-28 14:47:11 +00:00
|
|
|
/*
|
2021-02-23 08:05:13 +00:00
|
|
|
Copyright 2017-2021 The Kubernetes Authors.
|
2017-11-28 14:47:11 +00:00
|
|
|
|
|
|
|
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 network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2021-09-10 13:18:33 +00:00
|
|
|
"os"
|
2021-06-24 19:57:46 +00:00
|
|
|
"path/filepath"
|
2017-11-28 14:47:11 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2018-06-21 16:02:30 +00:00
|
|
|
|
2021-02-23 08:05:13 +00:00
|
|
|
"k8s.io/klog/v2"
|
|
|
|
|
2021-06-24 19:57:46 +00:00
|
|
|
"sigs.k8s.io/node-feature-discovery/pkg/api/feature"
|
|
|
|
"sigs.k8s.io/node-feature-discovery/pkg/utils"
|
2018-11-28 12:30:03 +00:00
|
|
|
"sigs.k8s.io/node-feature-discovery/source"
|
2017-11-28 14:47:11 +00:00
|
|
|
)
|
|
|
|
|
2022-01-19 07:12:06 +00:00
|
|
|
// Name of this feature source
|
2021-05-12 13:27:29 +00:00
|
|
|
const Name = "network"
|
|
|
|
|
2021-06-24 19:57:46 +00:00
|
|
|
const DeviceFeature = "device"
|
2020-05-19 18:07:23 +00:00
|
|
|
|
|
|
|
const sysfsBaseDir = "class/net"
|
|
|
|
|
2021-06-24 19:57:46 +00:00
|
|
|
// networkSource implements the FeatureSource and LabelSource interfaces.
|
|
|
|
type networkSource struct {
|
|
|
|
features *feature.DomainFeatures
|
|
|
|
}
|
2017-11-28 14:47:11 +00:00
|
|
|
|
2021-03-01 07:02:22 +00:00
|
|
|
// Singleton source instance
|
|
|
|
var (
|
|
|
|
src networkSource
|
2021-06-24 19:57:46 +00:00
|
|
|
_ source.FeatureSource = &src
|
|
|
|
_ source.LabelSource = &src
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ifaceAttrs is the list of files under /sys/class/net/<iface> that we're trying to read
|
|
|
|
ifaceAttrs = []string{"operstate", "speed"}
|
|
|
|
// devAttrs is the list of files under /sys/class/net/<iface>/device that we're trying to read
|
|
|
|
devAttrs = []string{"sriov_numvfs", "sriov_totalvfs"}
|
2021-03-01 07:02:22 +00:00
|
|
|
)
|
2020-04-21 19:03:37 +00:00
|
|
|
|
2021-03-01 07:02:22 +00:00
|
|
|
// Name returns an identifier string for this feature source.
|
|
|
|
func (s *networkSource) Name() string { return Name }
|
2020-04-21 19:03:37 +00:00
|
|
|
|
2021-03-01 07:02:22 +00:00
|
|
|
// Priority method of the LabelSource interface
|
|
|
|
func (s *networkSource) Priority() int { return 0 }
|
2020-04-21 19:03:37 +00:00
|
|
|
|
2021-03-01 16:39:49 +00:00
|
|
|
// GetLabels method of the LabelSource interface
|
|
|
|
func (s *networkSource) GetLabels() (source.FeatureLabels, error) {
|
2021-06-24 19:57:46 +00:00
|
|
|
labels := source.FeatureLabels{}
|
|
|
|
features := s.GetFeatures()
|
2020-05-19 18:07:23 +00:00
|
|
|
|
2021-06-24 19:57:46 +00:00
|
|
|
for _, dev := range features.Instances[DeviceFeature].Elements {
|
|
|
|
attrs := dev.Attributes
|
|
|
|
for attr, feature := range map[string]string{
|
|
|
|
"sriov_totalvfs": "sriov.capable",
|
|
|
|
"sriov_numvfs": "sriov.configured"} {
|
2020-05-19 18:07:23 +00:00
|
|
|
|
2021-06-24 19:57:46 +00:00
|
|
|
if v, ok := attrs[attr]; ok {
|
|
|
|
t, err := strconv.Atoi(v)
|
2017-11-28 14:47:11 +00:00
|
|
|
if err != nil {
|
2021-06-24 19:57:46 +00:00
|
|
|
klog.Errorf("failed to parse %s of %s: %v", attr, attrs["name"])
|
2017-11-28 14:47:11 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-06-24 19:57:46 +00:00
|
|
|
if t > 0 {
|
|
|
|
labels[feature] = true
|
2017-11-28 14:47:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-24 19:57:46 +00:00
|
|
|
return labels, nil
|
2017-11-28 14:47:11 +00:00
|
|
|
}
|
2020-05-19 18:07:23 +00:00
|
|
|
|
2021-06-24 19:57:46 +00:00
|
|
|
// Discover method of the FeatureSource interface.
|
|
|
|
func (s *networkSource) Discover() error {
|
|
|
|
s.features = feature.NewDomainFeatures()
|
|
|
|
|
|
|
|
devs, err := detectNetDevices()
|
2020-05-19 18:07:23 +00:00
|
|
|
if err != nil {
|
2021-06-24 19:57:46 +00:00
|
|
|
return fmt.Errorf("failed to detect network devices: %w", err)
|
|
|
|
}
|
|
|
|
s.features.Instances[DeviceFeature] = feature.InstanceFeatureSet{Elements: devs}
|
|
|
|
|
|
|
|
utils.KlogDump(3, "discovered network features:", " ", s.features)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFeatures method of the FeatureSource Interface.
|
|
|
|
func (s *networkSource) GetFeatures() *feature.DomainFeatures {
|
|
|
|
if s.features == nil {
|
|
|
|
s.features = feature.NewDomainFeatures()
|
2020-05-19 18:07:23 +00:00
|
|
|
}
|
2021-06-24 19:57:46 +00:00
|
|
|
return s.features
|
|
|
|
}
|
|
|
|
|
|
|
|
func detectNetDevices() ([]feature.InstanceFeature, error) {
|
|
|
|
sysfsBasePath := source.SysfsDir.Path(sysfsBaseDir)
|
|
|
|
|
|
|
|
ifaces, err := ioutil.ReadDir(sysfsBasePath)
|
2020-05-19 18:07:23 +00:00
|
|
|
if err != nil {
|
2021-06-24 19:57:46 +00:00
|
|
|
return nil, fmt.Errorf("failed to list network interfaces: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over devices
|
|
|
|
info := make([]feature.InstanceFeature, 0, len(ifaces))
|
|
|
|
for _, iface := range ifaces {
|
|
|
|
name := iface.Name()
|
|
|
|
if _, err := os.Stat(filepath.Join(sysfsBasePath, name, "device")); err == nil {
|
|
|
|
info = append(info, readIfaceInfo(filepath.Join(sysfsBasePath, name)))
|
|
|
|
} else if klog.V(3).Enabled() {
|
|
|
|
klog.Infof("skipping non-device iface %q", name)
|
|
|
|
}
|
2020-05-19 18:07:23 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 19:57:46 +00:00
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readIfaceInfo(path string) feature.InstanceFeature {
|
|
|
|
attrs := map[string]string{"name": filepath.Base(path)}
|
|
|
|
for _, attrName := range ifaceAttrs {
|
|
|
|
data, err := ioutil.ReadFile(filepath.Join(path, attrName))
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
klog.Errorf("failed to read net iface attribute %s: %v", attrName, err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
attrs[attrName] = strings.TrimSpace(string(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, attrName := range devAttrs {
|
|
|
|
data, err := ioutil.ReadFile(filepath.Join(path, "device", attrName))
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
klog.Errorf("failed to read net device attribute %s: %v", attrName, err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
attrs[attrName] = strings.TrimSpace(string(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
return *feature.NewInstanceFeature(attrs)
|
|
|
|
|
2020-05-19 18:07:23 +00:00
|
|
|
}
|
2021-03-01 07:02:22 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
source.Register(&src)
|
|
|
|
}
|