2018-07-17 09:26:11 +00:00
|
|
|
/*
|
2021-02-23 08:05:13 +00:00
|
|
|
Copyright 2018-2021 The Kubernetes Authors.
|
2018-07-17 09:26: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 cpu
|
|
|
|
|
|
|
|
import (
|
2022-09-08 10:23:49 +00:00
|
|
|
"os"
|
2021-03-02 13:17:33 +00:00
|
|
|
"strconv"
|
2021-02-23 08:05:13 +00:00
|
|
|
|
|
|
|
"k8s.io/klog/v2"
|
2018-07-17 09:26:11 +00:00
|
|
|
|
2022-03-28 09:51:23 +00:00
|
|
|
"github.com/klauspost/cpuid/v2"
|
|
|
|
|
2022-07-04 07:56:35 +00:00
|
|
|
nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/pkg/apis/nfd/v1alpha1"
|
2021-03-02 13:17:33 +00:00
|
|
|
"sigs.k8s.io/node-feature-discovery/pkg/utils"
|
2022-10-06 11:05:11 +00:00
|
|
|
"sigs.k8s.io/node-feature-discovery/pkg/utils/hostpath"
|
2018-07-17 09:26:11 +00:00
|
|
|
"sigs.k8s.io/node-feature-discovery/source"
|
|
|
|
)
|
|
|
|
|
2022-01-19 07:12:06 +00:00
|
|
|
// Name of this feature source
|
2021-05-12 13:27:29 +00:00
|
|
|
const Name = "cpu"
|
|
|
|
|
2021-03-02 13:17:33 +00:00
|
|
|
const (
|
2023-01-13 09:05:28 +00:00
|
|
|
CpuidFeature = "cpuid"
|
|
|
|
Cpumodel = "model"
|
|
|
|
CstateFeature = "cstate"
|
|
|
|
PstateFeature = "pstate"
|
|
|
|
RdtFeature = "rdt"
|
|
|
|
SeFeature = "se" // DEPRECATED in v0.12: will be removed in the future
|
|
|
|
SecurityFeature = "security"
|
|
|
|
SgxFeature = "sgx" // DEPRECATED in v0.12: will be removed in the future
|
|
|
|
SstFeature = "sst"
|
|
|
|
TopologyFeature = "topology"
|
|
|
|
CoprocessorFeature = "coprocessor"
|
2021-03-02 13:17:33 +00:00
|
|
|
)
|
|
|
|
|
2019-05-13 14:10:55 +00:00
|
|
|
// Configuration file options
|
|
|
|
type cpuidConfig struct {
|
|
|
|
AttributeBlacklist []string `json:"attributeBlacklist,omitempty"`
|
|
|
|
AttributeWhitelist []string `json:"attributeWhitelist,omitempty"`
|
|
|
|
}
|
|
|
|
|
2022-03-03 01:12:46 +00:00
|
|
|
// Config holds configuration for the cpu source.
|
2020-04-21 19:03:37 +00:00
|
|
|
type Config struct {
|
2019-05-13 14:10:55 +00:00
|
|
|
Cpuid cpuidConfig `json:"cpuid,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-04-21 19:03:37 +00:00
|
|
|
// newDefaultConfig returns a new config with pre-populated defaults
|
|
|
|
func newDefaultConfig() *Config {
|
|
|
|
return &Config{
|
|
|
|
cpuidConfig{
|
|
|
|
AttributeBlacklist: []string{
|
|
|
|
"BMI1",
|
|
|
|
"BMI2",
|
|
|
|
"CLMUL",
|
|
|
|
"CMOV",
|
|
|
|
"CX16",
|
|
|
|
"ERMS",
|
|
|
|
"F16C",
|
|
|
|
"HTT",
|
|
|
|
"LZCNT",
|
|
|
|
"MMX",
|
|
|
|
"MMXEXT",
|
|
|
|
"NX",
|
|
|
|
"POPCNT",
|
|
|
|
"RDRAND",
|
|
|
|
"RDSEED",
|
|
|
|
"RDTSCP",
|
|
|
|
"SGX",
|
|
|
|
"SGXLC",
|
|
|
|
"SSE",
|
|
|
|
"SSE2",
|
|
|
|
"SSE3",
|
2021-07-06 08:54:55 +00:00
|
|
|
"SSE4",
|
|
|
|
"SSE42",
|
2020-04-21 19:03:37 +00:00
|
|
|
"SSSE3",
|
|
|
|
},
|
|
|
|
AttributeWhitelist: []string{},
|
2019-05-13 14:10:55 +00:00
|
|
|
},
|
2020-04-21 19:03:37 +00:00
|
|
|
}
|
2019-05-13 14:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filter for cpuid labels
|
|
|
|
type keyFilter struct {
|
|
|
|
keys map[string]struct{}
|
|
|
|
whitelist bool
|
|
|
|
}
|
|
|
|
|
2021-03-02 13:17:33 +00:00
|
|
|
// cpuSource implements the FeatureSource, LabelSource and ConfigurableSource interfaces.
|
2021-03-01 07:02:22 +00:00
|
|
|
type cpuSource struct {
|
2020-04-21 19:03:37 +00:00
|
|
|
config *Config
|
|
|
|
cpuidFilter *keyFilter
|
2022-07-04 11:05:58 +00:00
|
|
|
features *nfdv1alpha1.Features
|
2020-04-21 19:03:37 +00:00
|
|
|
}
|
2018-07-17 09:26:11 +00:00
|
|
|
|
2021-03-01 07:02:22 +00:00
|
|
|
// Singleton source instance
|
|
|
|
var (
|
2021-03-02 13:17:33 +00:00
|
|
|
src = cpuSource{config: newDefaultConfig(), cpuidFilter: &keyFilter{}}
|
|
|
|
_ source.FeatureSource = &src
|
2021-03-01 07:02:22 +00:00
|
|
|
_ source.LabelSource = &src
|
|
|
|
_ source.ConfigurableSource = &src
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *cpuSource) Name() string { return Name }
|
2018-07-17 09:26:11 +00:00
|
|
|
|
2021-03-01 05:45:32 +00:00
|
|
|
// NewConfig method of the LabelSource interface
|
2021-03-01 07:02:22 +00:00
|
|
|
func (s *cpuSource) NewConfig() source.Config { return newDefaultConfig() }
|
2018-07-17 09:26:11 +00:00
|
|
|
|
2021-03-01 05:45:32 +00:00
|
|
|
// GetConfig method of the LabelSource interface
|
2021-03-01 07:02:22 +00:00
|
|
|
func (s *cpuSource) GetConfig() source.Config { return s.config }
|
2020-04-21 19:03:37 +00:00
|
|
|
|
2021-03-01 05:45:32 +00:00
|
|
|
// SetConfig method of the LabelSource interface
|
2021-03-01 07:02:22 +00:00
|
|
|
func (s *cpuSource) SetConfig(conf source.Config) {
|
2020-04-21 19:03:37 +00:00
|
|
|
switch v := conf.(type) {
|
|
|
|
case *Config:
|
|
|
|
s.config = v
|
|
|
|
s.initCpuidFilter()
|
|
|
|
default:
|
2021-02-23 08:05:13 +00:00
|
|
|
klog.Fatalf("invalid config type: %T", conf)
|
2019-05-13 14:10:55 +00:00
|
|
|
}
|
2020-04-21 19:03:37 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 07:02:22 +00:00
|
|
|
// Priority method of the LabelSource interface
|
|
|
|
func (s *cpuSource) Priority() int { return 0 }
|
|
|
|
|
2021-03-01 16:39:49 +00:00
|
|
|
// GetLabels method of the LabelSource interface
|
|
|
|
func (s *cpuSource) GetLabels() (source.FeatureLabels, error) {
|
2021-03-02 13:17:33 +00:00
|
|
|
labels := source.FeatureLabels{}
|
|
|
|
features := s.GetFeatures()
|
2019-05-13 14:10:55 +00:00
|
|
|
|
2021-03-02 13:17:33 +00:00
|
|
|
// CPUID
|
2022-06-14 14:02:49 +00:00
|
|
|
for f := range features.Flags[CpuidFeature].Elements {
|
2021-03-02 13:17:33 +00:00
|
|
|
if s.cpuidFilter.unmask(f) {
|
|
|
|
labels["cpuid."+f] = true
|
|
|
|
}
|
2018-07-17 09:26:11 +00:00
|
|
|
}
|
2019-04-12 11:30:53 +00:00
|
|
|
|
2022-03-28 09:51:23 +00:00
|
|
|
// CPU model
|
2022-06-14 14:02:49 +00:00
|
|
|
for k, v := range features.Attributes[Cpumodel].Elements {
|
2022-03-28 09:51:23 +00:00
|
|
|
labels["model."+k] = v
|
|
|
|
}
|
|
|
|
|
2021-03-02 13:17:33 +00:00
|
|
|
// Cstate
|
2022-06-14 14:02:49 +00:00
|
|
|
for k, v := range features.Attributes[CstateFeature].Elements {
|
2021-03-02 13:17:33 +00:00
|
|
|
labels["cstate."+k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pstate
|
2022-06-14 14:02:49 +00:00
|
|
|
for k, v := range features.Attributes[PstateFeature].Elements {
|
2021-03-02 13:17:33 +00:00
|
|
|
labels["pstate."+k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// RDT
|
2022-06-14 14:02:49 +00:00
|
|
|
for k := range features.Flags[RdtFeature].Elements {
|
2021-03-02 13:17:33 +00:00
|
|
|
labels["rdt."+k] = true
|
|
|
|
}
|
|
|
|
|
2022-06-28 08:44:21 +00:00
|
|
|
// Security
|
2022-06-14 14:02:49 +00:00
|
|
|
for k, v := range features.Attributes[SecurityFeature].Elements {
|
2022-06-28 08:44:21 +00:00
|
|
|
labels["security."+k] = v
|
|
|
|
}
|
|
|
|
|
2021-11-12 11:25:19 +00:00
|
|
|
// SGX
|
2022-06-14 14:02:49 +00:00
|
|
|
for k, v := range features.Attributes[SgxFeature].Elements {
|
2021-11-12 11:25:19 +00:00
|
|
|
labels["sgx."+k] = v
|
|
|
|
}
|
|
|
|
|
2022-03-18 13:40:03 +00:00
|
|
|
// Secure Execution
|
2022-06-14 14:02:49 +00:00
|
|
|
for k, v := range features.Attributes[SeFeature].Elements {
|
2022-03-18 13:40:03 +00:00
|
|
|
labels["se."+k] = v
|
|
|
|
}
|
|
|
|
|
2021-03-02 13:17:33 +00:00
|
|
|
// SST
|
2022-06-14 14:02:49 +00:00
|
|
|
for k, v := range features.Attributes[SstFeature].Elements {
|
2021-03-02 13:17:33 +00:00
|
|
|
labels["power.sst_"+k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hyperthreading
|
2022-06-14 14:02:49 +00:00
|
|
|
if v, ok := features.Attributes[TopologyFeature].Elements["hardware_multithreading"]; ok {
|
2021-03-02 13:17:33 +00:00
|
|
|
labels["hardware_multithreading"] = v
|
2019-04-12 11:30:53 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 09:05:28 +00:00
|
|
|
// NX
|
|
|
|
if v, ok := features.Attributes[CoprocessorFeature].Elements["nx_gzip"]; ok {
|
|
|
|
labels["coprocessor.nx_gzip"] = v
|
|
|
|
}
|
|
|
|
|
2021-03-02 13:17:33 +00:00
|
|
|
return labels, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discover method of the FeatureSource Interface
|
|
|
|
func (s *cpuSource) Discover() error {
|
2022-07-04 11:05:58 +00:00
|
|
|
s.features = nfdv1alpha1.NewFeatures()
|
2021-03-02 13:17:33 +00:00
|
|
|
|
2019-02-19 08:52:46 +00:00
|
|
|
// Detect CPUID
|
2022-07-04 07:56:35 +00:00
|
|
|
s.features.Flags[CpuidFeature] = nfdv1alpha1.NewFlagFeatures(getCpuidFlags()...)
|
2021-03-02 13:17:33 +00:00
|
|
|
|
2022-03-28 09:51:23 +00:00
|
|
|
// Detect CPU model
|
2022-07-04 07:56:35 +00:00
|
|
|
s.features.Attributes[Cpumodel] = nfdv1alpha1.NewAttributeFeatures(getCPUModel())
|
2022-03-28 09:51:23 +00:00
|
|
|
|
2021-03-02 13:17:33 +00:00
|
|
|
// Detect cstate configuration
|
|
|
|
cstate, err := detectCstate()
|
|
|
|
if err != nil {
|
|
|
|
klog.Errorf("failed to detect cstate: %v", err)
|
|
|
|
} else {
|
2022-07-04 07:56:35 +00:00
|
|
|
s.features.Attributes[CstateFeature] = nfdv1alpha1.NewAttributeFeatures(cstate)
|
2019-02-19 08:52:46 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 13:11:27 +00:00
|
|
|
// Detect pstate features
|
|
|
|
pstate, err := detectPstate()
|
2019-02-19 08:52:46 +00:00
|
|
|
if err != nil {
|
2021-02-23 08:05:13 +00:00
|
|
|
klog.Error(err)
|
2019-02-19 08:52:46 +00:00
|
|
|
}
|
2022-07-04 07:56:35 +00:00
|
|
|
s.features.Attributes[PstateFeature] = nfdv1alpha1.NewAttributeFeatures(pstate)
|
2019-02-19 08:52:46 +00:00
|
|
|
|
|
|
|
// Detect RDT features
|
2022-07-04 07:56:35 +00:00
|
|
|
s.features.Flags[RdtFeature] = nfdv1alpha1.NewFlagFeatures(discoverRDT()...)
|
2021-03-02 13:17:33 +00:00
|
|
|
|
2023-03-27 11:24:44 +00:00
|
|
|
// Detect available guest protection(SGX,TDX,SEV) features
|
2022-07-04 07:56:35 +00:00
|
|
|
s.features.Attributes[SecurityFeature] = nfdv1alpha1.NewAttributeFeatures(discoverSecurity())
|
2022-06-28 08:44:21 +00:00
|
|
|
|
|
|
|
// Detect SGX features
|
|
|
|
//
|
|
|
|
// DEPRECATED in v0.12: will be removed in the future
|
2022-06-14 14:02:49 +00:00
|
|
|
if val, ok := s.features.Attributes[SecurityFeature].Elements["sgx.enabled"]; ok {
|
2022-07-04 07:56:35 +00:00
|
|
|
s.features.Attributes[SgxFeature] = nfdv1alpha1.NewAttributeFeatures(map[string]string{"enabled": val})
|
2022-06-28 08:44:21 +00:00
|
|
|
}
|
2021-11-12 11:25:19 +00:00
|
|
|
|
2022-03-18 13:40:03 +00:00
|
|
|
// Detect Secure Execution features
|
2022-06-28 08:44:21 +00:00
|
|
|
//
|
|
|
|
// DEPRECATED in v0.12: will be removed in the future
|
2022-06-14 14:02:49 +00:00
|
|
|
if val, ok := s.features.Attributes[SecurityFeature].Elements["se.enabled"]; ok {
|
2022-07-04 07:56:35 +00:00
|
|
|
s.features.Attributes[SeFeature] = nfdv1alpha1.NewAttributeFeatures(map[string]string{"enabled": val})
|
2022-06-28 08:44:21 +00:00
|
|
|
}
|
2022-03-18 13:40:03 +00:00
|
|
|
|
2021-03-02 13:17:33 +00:00
|
|
|
// Detect SST features
|
2022-07-04 07:56:35 +00:00
|
|
|
s.features.Attributes[SstFeature] = nfdv1alpha1.NewAttributeFeatures(discoverSST())
|
2021-03-02 13:17:33 +00:00
|
|
|
|
|
|
|
// Detect hyper-threading
|
2022-07-04 07:56:35 +00:00
|
|
|
s.features.Attributes[TopologyFeature] = nfdv1alpha1.NewAttributeFeatures(discoverTopology())
|
2021-03-02 13:17:33 +00:00
|
|
|
|
2023-01-13 09:05:28 +00:00
|
|
|
// Detect Coprocessor features
|
|
|
|
s.features.Attributes[CoprocessorFeature] = nfdv1alpha1.NewAttributeFeatures(discoverCoprocessor())
|
|
|
|
|
2021-03-02 13:17:33 +00:00
|
|
|
utils.KlogDump(3, "discovered cpu features:", " ", s.features)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFeatures method of the FeatureSource Interface
|
2022-07-04 11:05:58 +00:00
|
|
|
func (s *cpuSource) GetFeatures() *nfdv1alpha1.Features {
|
2021-03-02 13:17:33 +00:00
|
|
|
if s.features == nil {
|
2022-07-04 11:05:58 +00:00
|
|
|
s.features = nfdv1alpha1.NewFeatures()
|
2019-02-19 08:52:46 +00:00
|
|
|
}
|
2021-03-02 13:17:33 +00:00
|
|
|
return s.features
|
|
|
|
}
|
2019-02-19 08:52:46 +00:00
|
|
|
|
2022-03-28 09:51:23 +00:00
|
|
|
func getCPUModel() map[string]string {
|
|
|
|
cpuModelInfo := make(map[string]string)
|
|
|
|
cpuModelInfo["vendor_id"] = cpuid.CPU.VendorID.String()
|
|
|
|
cpuModelInfo["family"] = strconv.Itoa(cpuid.CPU.Family)
|
|
|
|
cpuModelInfo["id"] = strconv.Itoa(cpuid.CPU.Model)
|
|
|
|
|
|
|
|
return cpuModelInfo
|
|
|
|
}
|
|
|
|
|
2021-03-02 13:17:33 +00:00
|
|
|
func discoverTopology() map[string]string {
|
|
|
|
features := make(map[string]string)
|
|
|
|
|
|
|
|
if ht, err := haveThreadSiblings(); err != nil {
|
|
|
|
klog.Errorf("failed to detect hyper-threading: %v", err)
|
|
|
|
} else {
|
|
|
|
features["hardware_multithreading"] = strconv.FormatBool(ht)
|
2021-03-02 20:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 13:17:33 +00:00
|
|
|
return features
|
2018-07-17 09:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if any (online) CPUs have thread siblings
|
|
|
|
func haveThreadSiblings() (bool, error) {
|
2020-05-20 11:32:07 +00:00
|
|
|
|
2022-10-06 11:05:11 +00:00
|
|
|
files, err := os.ReadDir(hostpath.SysfsDir.Path("bus/cpu/devices"))
|
2018-07-17 09:26:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
// Try to read siblings from topology
|
2022-10-06 11:05:11 +00:00
|
|
|
siblings, err := os.ReadFile(hostpath.SysfsDir.Path("bus/cpu/devices", file.Name(), "topology/thread_siblings_list"))
|
2018-07-17 09:26:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
for _, char := range siblings {
|
|
|
|
// If list separator found, we determine that there are multiple siblings
|
|
|
|
if char == ',' || char == '-' {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// No siblings were found
|
|
|
|
return false, nil
|
|
|
|
}
|
2019-05-13 14:10:55 +00:00
|
|
|
|
2021-03-01 07:02:22 +00:00
|
|
|
func (s *cpuSource) initCpuidFilter() {
|
2019-05-13 14:10:55 +00:00
|
|
|
newFilter := keyFilter{keys: map[string]struct{}{}}
|
2020-04-21 19:03:37 +00:00
|
|
|
if len(s.config.Cpuid.AttributeWhitelist) > 0 {
|
|
|
|
for _, k := range s.config.Cpuid.AttributeWhitelist {
|
2019-05-13 14:10:55 +00:00
|
|
|
newFilter.keys[k] = struct{}{}
|
|
|
|
}
|
|
|
|
newFilter.whitelist = true
|
|
|
|
} else {
|
2020-04-21 19:03:37 +00:00
|
|
|
for _, k := range s.config.Cpuid.AttributeBlacklist {
|
2019-05-13 14:10:55 +00:00
|
|
|
newFilter.keys[k] = struct{}{}
|
|
|
|
}
|
|
|
|
newFilter.whitelist = false
|
|
|
|
}
|
2020-04-21 19:03:37 +00:00
|
|
|
s.cpuidFilter = &newFilter
|
2019-05-13 14:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f keyFilter) unmask(k string) bool {
|
|
|
|
if f.whitelist {
|
|
|
|
if _, ok := f.keys[k]; ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, ok := f.keys[k]; !ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2021-03-01 07:02:22 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
source.Register(&src)
|
|
|
|
}
|