-
Notifications
You must be signed in to change notification settings - Fork 3
/
features.go
250 lines (209 loc) · 5.75 KB
/
features.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package skbtrace
import (
"bytes"
"fmt"
"math/big"
"regexp"
"strconv"
"strings"
"syscall"
"golang.org/x/exp/maps"
)
// FeatureComponent groups features by components they are implemented by.
// FeatureComponentExternal can be used by commands built on top of skbtrace which
// can use customized kernel module versioned separately from kernel version.
type FeatureComponent int
const (
FeatureComponentKernel FeatureComponent = iota
FeatureComponentBPFTrace
FeatureComponentExternal
FeatureComponentCount
)
type featureState byte
const (
featureAuto featureState = iota
featureForceEnable
featureForceDisable
)
type FeatureFlagMask struct {
bits *big.Int
}
type Version struct {
// Open-Source derived versions
Major int
Submajor int
Minor int
// NOTE: Yandex Cloud derivatives use build numbers and do not alter open-source versions
Build int
Date int
}
type VersionProvider interface {
Get() ([]byte, error)
Parse(verBytes []byte) (Version, error)
GetDefault() Version
}
func NewVersionFromMatches(matches [][]byte) (ver Version, err error) {
for i, ptr := range []*int{&ver.Major, &ver.Submajor, &ver.Minor, &ver.Build, &ver.Date} {
if i >= len(matches) {
break
}
*ptr, err = strconv.Atoi(string(matches[i]))
if err != nil {
return ver, fmt.Errorf("error parsing version component #%d: %w", i, err)
}
}
return ver, nil
}
func (v Version) EqualOrNewer(v2 Version) bool {
if v.Major != v2.Major {
return v.Major > v2.Major
}
if v.Submajor != v2.Submajor {
return v.Submajor > v2.Submajor
}
if v.Minor != v2.Minor {
return v.Minor > v2.Minor
}
if v.Build != v2.Build {
return v.Build > v2.Build
}
return v.Date >= v2.Date
}
func (v Version) String() string {
b := bytes.NewBuffer(nil)
fmt.Fprintf(b, "%d.%d.%d", v.Major, v.Submajor, v.Minor)
if v.Build > 0 {
fmt.Fprintf(b, "-%d", v.Build)
if v.Date > 0 {
fmt.Fprintf(b, ".%06d", v.Date)
}
}
return b.String()
}
type Feature struct {
Component FeatureComponent
Name string
Help string
Commit string
MinVersion Version
// flag is assigned after registering feature in init
flag int
}
var knownFeatures [FeatureComponentCount][]*Feature
func RegisterFeatures(features ...*Feature) {
for _, feature := range features {
component := feature.Component
feature.flag = len(knownFeatures[component]) + 1
knownFeatures[component] = append(knownFeatures[component], feature)
}
}
func GetKnownFeatures(component FeatureComponent) []*Feature {
return knownFeatures[component]
}
type FeatureComponentSpec struct {
Component FeatureComponent
Provider VersionProvider
}
// ProcessFeatures produces feature mask for the current setup. Default is detection
// by a version, but additionally some features can be disabled (if preceded with !) or
// enabled via mask argument containing names of the features separated by commas.
func (spec FeatureComponentSpec) ProcessFeatures(verArg, maskArg string) (mask FeatureFlagMask, err error) {
ver, err := getVersion(verArg, spec.Provider)
if err != nil {
return FeatureFlagMask{}, err
}
var forcedState map[string]featureState
if maskArg != "" {
forcedState, err = parseMaskArgument(maskArg)
if err != nil {
return FeatureFlagMask{}, err
}
}
bits := big.NewInt(0)
loop:
for _, feature := range knownFeatures[spec.Component] {
if forcedState != nil {
switch forcedState[feature.Name] {
case featureForceEnable:
bits.SetBit(bits, int(feature.flag), 1)
fallthrough
case featureForceDisable:
delete(forcedState, feature.Name)
continue loop
case featureAuto:
// Handle auto value: compare versions
}
}
if ver.EqualOrNewer(feature.MinVersion) {
bits.SetBit(bits, int(feature.flag), 1)
}
}
if len(forcedState) > 0 {
return FeatureFlagMask{}, fmt.Errorf(
"unrecognized features specified in mask: %s",
strings.Join(maps.Keys(forcedState), ","))
}
return FeatureFlagMask{bits: bits}, nil
}
func getVersion(verArg string, provider VersionProvider) (ver Version, err error) {
var verBytes []byte
if verArg == "" {
verBytes, err = provider.Get()
if err != nil {
return provider.GetDefault(), err
}
} else {
verBytes = []byte(verArg)
}
return provider.Parse(verBytes)
}
func parseMaskArgument(maskArg string) (map[string]featureState, error) {
tokens := strings.Split(maskArg, ",")
stateMap := make(map[string]featureState, len(tokens))
for _, token := range tokens {
state := featureForceEnable
if strings.HasPrefix(token, "!") {
token = token[1:]
state = featureForceDisable
}
if _, hasState := stateMap[token]; hasState {
return nil, fmt.Errorf(
"cannot parse feature mask: feature %q is already specified", token)
}
stateMap[token] = state
}
return stateMap, nil
}
func (mask FeatureFlagMask) Supports(feature *Feature) bool {
if feature.flag == 0 {
panic(fmt.Errorf("trying to check feature %q that wasn't registered", feature.Name))
}
return mask.bits.Bit(feature.flag) == 1
}
var reKernelVersion = regexp.MustCompile(`(\d+)\.(\d+)\.(\d+)`)
type KernelVersionProvider struct{}
func (k *KernelVersionProvider) Get() ([]byte, error) {
var utsname syscall.Utsname
if err := syscall.Uname(&utsname); err != nil {
return nil, err
}
buf := bytes.NewBuffer(make([]byte, 0, len(utsname.Version)))
for _, r := range utsname.Release {
if r == 0 {
break
}
buf.WriteByte(byte(r))
}
return buf.Bytes(), nil
}
func (k *KernelVersionProvider) Parse(verBytes []byte) (Version, error) {
matches := reKernelVersion.FindSubmatch(verBytes)
if len(matches) < 4 {
return safeDefaultVersion, fmt.Errorf(
"not enough numeric components in version %q, at least 3 are needed", verBytes)
}
return NewVersionFromMatches(matches[1:])
}
func (k *KernelVersionProvider) GetDefault() Version {
return Version{Major: 4, Submajor: 14}
}