| 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 |
|
| 13 |
var ErrNotSupported = errors.New("not supported") |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
var ErrNotSupportedOnOS = fmt.Errorf("%w on %s", ErrNotSupported, runtime.GOOS) |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
var ErrRestrictedKernel = errors.New("restricted by kernel.kptr_restrict and/or net.core.bpf_jit_harden sysctls") |
| 22 |
|
| 23 |
|
| 24 |
type UnsupportedFeatureError struct { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
MinimumVersion Version |
| 32 |
|
| 33 |
|
| 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 |
|
| 45 |
func (ufe *UnsupportedFeatureError) Is(target error) bool { |
| 46 |
return target == ErrNotSupported |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
type FeatureTest struct { |
| 53 |
|
| 54 |
Name string |
| 55 |
|
| 56 |
Version string |
| 57 |
|
| 58 |
Fn FeatureTestFn |
| 59 |
|
| 60 |
mu sync.RWMutex |
| 61 |
done bool |
| 62 |
result error |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
type FeatureTestFn func() error |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 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 |
|
| 90 |
|
| 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 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 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 |
|
| 122 |
|
| 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 |
|
| 152 |
|
| 153 |
|
| 154 |
return fmt.Errorf("detect support for %s: %w", ft.Name, err) |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
type FeatureMatrix[K comparable] map[K]*FeatureTest |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 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 |
|
| 184 |
|
| 185 |
|
| 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 |
|
| 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 |
} |