Debian Codegraph @

internal/feature.go

raw symbols SCIP
1 package internal
2
3 import (
4 "errors"
5 "fmt"
6 "runtime"
7 "sync"
8
9 "github.com/cilium/ebpf/internal/platform"
10 )
11
12 // ErrNotSupported indicates that a feature is not supported.
13 var ErrNotSupported = errors.New("not supported")
14
15 // ErrNotSupportedOnOS indicates that a feature is not supported on the current
16 // operating system.
17 var ErrNotSupportedOnOS = fmt.Errorf("%w on %s", ErrNotSupported, runtime.GOOS)
18
19 // ErrRestrictedKernel is returned when kernel address information is restricted
20 // by kernel.kptr_restrict and/or net.core.bpf_jit_harden sysctls.
21 var ErrRestrictedKernel = errors.New("restricted by kernel.kptr_restrict and/or net.core.bpf_jit_harden sysctls")
22
23 // UnsupportedFeatureError is returned by FeatureTest() functions.
24 type UnsupportedFeatureError struct {
25 // The minimum version required for this feature.
26 //
27 // On Linux this refers to the mainline kernel version, on other platforms
28 // to the version of the runtime.
29 //
30 // Used for the error string, and for sanity checking during testing.
31 MinimumVersion Version
32
33 // The name of the feature that isn't supported.
34 Name string
35 }
36
37 func (ufe *UnsupportedFeatureError) Error() string {
38 if ufe.MinimumVersion.Unspecified() {
39 return fmt.Sprintf("%s not supported", ufe.Name)
40 }
41 return fmt.Sprintf("%s not supported (requires >= %s)", ufe.Name, ufe.MinimumVersion)
42 }
43
44 // Is indicates that UnsupportedFeatureError is ErrNotSupported.
45 func (ufe *UnsupportedFeatureError) Is(target error) bool {
46 return target == ErrNotSupported
47 }
48
49 // FeatureTest caches the result of a [FeatureTestFn].
50 //
51 // Fields should not be modified after creation.
52 type FeatureTest struct {
53 // The name of the feature being detected.
54 Name string
55 // Version in the form Major.Minor[.Patch].
56 Version string
57 // The feature test itself.
58 Fn FeatureTestFn
59
60 mu sync.RWMutex
61 done bool
62 result error
63 }
64
65 // FeatureTestFn is used to determine whether the kernel supports
66 // a certain feature.
67 //
68 // The return values have the following semantics:
69 //
70 // err == ErrNotSupported: the feature is not available
71 // err == nil: the feature is available
72 // err != nil: the test couldn't be executed
73 type FeatureTestFn func() error
74
75 // NewFeatureTest is a convenient way to create a single [FeatureTest].
76 //
77 // versions specifies in which version of a BPF runtime a feature appeared.
78 // The format is "GOOS:Major.Minor[.Patch]". GOOS may be omitted when targeting
79 // Linux. Returns [ErrNotSupportedOnOS] if there is no version specified for the
80 // current OS.
81 func NewFeatureTest(name string, fn FeatureTestFn, versions ...string) func() error {
82 version, err := platform.SelectVersion(versions)
83 if err != nil {
84 return func() error { return err }
85 }
86
87 if version == "" {
88 return func() error {
89 // We don't return an UnsupportedFeatureError here, since that will
90 // trigger version checks which don't make sense.
91 return fmt.Errorf("%s: %w", name, ErrNotSupportedOnOS)
92 }
93 }
94
95 ft := &FeatureTest{
96 Name: name,
97 Version: version,
98 Fn: fn,
99 }
100
101 return ft.execute
102 }
103
104 // execute the feature test.
105 //
106 // The result is cached if the test is conclusive.
107 //
108 // See [FeatureTestFn] for the meaning of the returned error.
109 func (ft *FeatureTest) execute() error {
110 ft.mu.RLock()
111 result, done := ft.result, ft.done
112 ft.mu.RUnlock()
113
114 if done {
115 return result
116 }
117
118 ft.mu.Lock()
119 defer ft.mu.Unlock()
120
121 // The test may have been executed by another caller while we were
122 // waiting to acquire ft.mu.
123 if ft.done {
124 return ft.result
125 }
126
127 err := ft.Fn()
128 if err == nil {
129 ft.done = true
130 return nil
131 }
132
133 if errors.Is(err, ErrNotSupported) {
134 var v Version
135 if ft.Version != "" {
136 v, err = NewVersion(ft.Version)
137 if err != nil {
138 return fmt.Errorf("feature %s: %w", ft.Name, err)
139 }
140 }
141
142 ft.done = true
143 ft.result = &UnsupportedFeatureError{
144 MinimumVersion: v,
145 Name: ft.Name,
146 }
147
148 return ft.result
149 }
150
151 // We couldn't execute the feature test to a point
152 // where it could make a determination.
153 // Don't cache the result, just return it.
154 return fmt.Errorf("detect support for %s: %w", ft.Name, err)
155 }
156
157 // FeatureMatrix groups multiple related feature tests into a map.
158 //
159 // Useful when there is a small number of discrete features which are known
160 // at compile time.
161 //
162 // It must not be modified concurrently with calling [FeatureMatrix.Result].
163 type FeatureMatrix[K comparable] map[K]*FeatureTest
164
165 // Result returns the outcome of the feature test for the given key.
166 //
167 // It's safe to call this function concurrently.
168 //
169 // Always returns [ErrNotSupportedOnOS] on Windows.
170 func (fm FeatureMatrix[K]) Result(key K) error {
171 ft, ok := fm[key]
172 if !ok {
173 return fmt.Errorf("no feature probe for %v", key)
174 }
175
176 if platform.IsWindows {
177 return fmt.Errorf("%s: %w", ft.Name, ErrNotSupportedOnOS)
178 }
179
180 return ft.execute()
181 }
182
183 // FeatureCache caches a potentially unlimited number of feature probes.
184 //
185 // Useful when there is a high cardinality for a feature test.
186 type FeatureCache[K comparable] struct {
187 mu sync.RWMutex
188 newTest func(K) *FeatureTest
189 features map[K]*FeatureTest
190 }
191
192 func NewFeatureCache[K comparable](newTest func(K) *FeatureTest) *FeatureCache[K] {
193 return &FeatureCache[K]{
194 newTest: newTest,
195 features: make(map[K]*FeatureTest),
196 }
197 }
198
199 func (fc *FeatureCache[K]) Result(key K) error {
200 if platform.IsWindows {
201 return fmt.Errorf("feature probe for %v: %w", key, ErrNotSupportedOnOS)
202 }
203
204 // NB: Executing the feature test happens without fc.mu taken.
205 return fc.retrieve(key).execute()
206 }
207
208 func (fc *FeatureCache[K]) retrieve(key K) *FeatureTest {
209 fc.mu.RLock()
210 ft := fc.features[key]
211 fc.mu.RUnlock()
212
213 if ft != nil {
214 return ft
215 }
216
217 fc.mu.Lock()
218 defer fc.mu.Unlock()
219
220 if ft := fc.features[key]; ft != nil {
221 return ft
222 }
223
224 ft = fc.newTest(key)
225 fc.features[key] = ft
226 return ft
227 }