| 1 |
package ebpf |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"sync" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/go-quicktest/qt" |
| 9 |
|
| 10 |
"github.com/cilium/ebpf/asm" |
| 11 |
"github.com/cilium/ebpf/btf" |
| 12 |
"github.com/cilium/ebpf/internal" |
| 13 |
"github.com/cilium/ebpf/internal/platform" |
| 14 |
"github.com/cilium/ebpf/internal/testutils" |
| 15 |
) |
| 16 |
|
| 17 |
var haveTestmod = sync.OnceValues(func() (bool, error) { |
| 18 |
if platform.IsWindows { |
| 19 |
return false, nil |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
testmod, err := btf.FindHandle(func(info *btf.HandleInfo) bool { |
| 24 |
return info.IsModule() && info.Name == "bpf_testmod" |
| 25 |
}) |
| 26 |
if err != nil && !errors.Is(err, btf.ErrNotFound) { |
| 27 |
return false, err |
| 28 |
} |
| 29 |
testmod.Close() |
| 30 |
|
| 31 |
return testmod != nil, nil |
| 32 |
}) |
| 33 |
|
| 34 |
var haveTestmodOps = sync.OnceValues(func() (bool, error) { |
| 35 |
haveTestMod, err := haveTestmod() |
| 36 |
if err != nil { |
| 37 |
return false, err |
| 38 |
} |
| 39 |
if !haveTestMod { |
| 40 |
return false, nil |
| 41 |
} |
| 42 |
|
| 43 |
target := btf.Type((*btf.Struct)(nil)) |
| 44 |
_, module, err := findTargetInKernel("bpf_struct_ops_bpf_testmod_ops", &target, btf.NewCache()) |
| 45 |
if err != nil && !errors.Is(err, btf.ErrNotFound) { |
| 46 |
return false, err |
| 47 |
} |
| 48 |
if errors.Is(err, btf.ErrNotFound) { |
| 49 |
return false, nil |
| 50 |
} |
| 51 |
defer module.Close() |
| 52 |
|
| 53 |
return true, nil |
| 54 |
}) |
| 55 |
|
| 56 |
func requireTestmod(tb testing.TB) { |
| 57 |
tb.Helper() |
| 58 |
|
| 59 |
testutils.SkipOnOldKernel(tb, "5.11", "bpf_testmod") |
| 60 |
|
| 61 |
testmod, err := haveTestmod() |
| 62 |
if err != nil { |
| 63 |
tb.Fatal(err) |
| 64 |
} |
| 65 |
if !testmod { |
| 66 |
tb.Skip("bpf_testmod not loaded") |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
func requireTestmodOps(tb testing.TB) { |
| 71 |
tb.Helper() |
| 72 |
|
| 73 |
testutils.SkipOnOldKernel(tb, "5.11", "bpf_testmod") |
| 74 |
testmodOps, err := haveTestmodOps() |
| 75 |
if err != nil { |
| 76 |
tb.Fatal(err) |
| 77 |
} |
| 78 |
if !testmodOps { |
| 79 |
tb.Skip("bpf_testmod_ops not loaded") |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func newMap(tb testing.TB, spec *MapSpec, opts *MapOptions) (*Map, error) { |
| 84 |
tb.Helper() |
| 85 |
|
| 86 |
spec = fixupMapSpec(spec) |
| 87 |
|
| 88 |
if opts == nil { |
| 89 |
opts = new(MapOptions) |
| 90 |
} |
| 91 |
|
| 92 |
m, err := NewMapWithOptions(spec, *opts) |
| 93 |
testutils.SkipIfNotSupportedOnOS(tb, err) |
| 94 |
if err != nil { |
| 95 |
return nil, err |
| 96 |
} |
| 97 |
|
| 98 |
tb.Cleanup(func() { m.Close() }) |
| 99 |
return m, nil |
| 100 |
} |
| 101 |
|
| 102 |
func mustNewMap(tb testing.TB, spec *MapSpec, opts *MapOptions) *Map { |
| 103 |
tb.Helper() |
| 104 |
|
| 105 |
m, err := newMap(tb, spec, opts) |
| 106 |
qt.Assert(tb, qt.IsNil(err)) |
| 107 |
|
| 108 |
return m |
| 109 |
} |
| 110 |
|
| 111 |
func createMap(tb testing.TB, typ MapType, maxEntries uint32) *Map { |
| 112 |
tb.Helper() |
| 113 |
|
| 114 |
return mustNewMap(tb, &MapSpec{ |
| 115 |
Name: "test", |
| 116 |
Type: typ, |
| 117 |
KeySize: 4, |
| 118 |
ValueSize: 4, |
| 119 |
MaxEntries: maxEntries, |
| 120 |
}, nil) |
| 121 |
} |
| 122 |
|
| 123 |
func createMapInMap(tb testing.TB, outer, inner MapType) *Map { |
| 124 |
tb.Helper() |
| 125 |
|
| 126 |
return mustNewMap(tb, &MapSpec{ |
| 127 |
Type: outer, |
| 128 |
KeySize: 4, |
| 129 |
MaxEntries: 2, |
| 130 |
InnerMap: &MapSpec{ |
| 131 |
Type: inner, |
| 132 |
KeySize: 4, |
| 133 |
ValueSize: 4, |
| 134 |
MaxEntries: 2, |
| 135 |
}, |
| 136 |
}, nil) |
| 137 |
} |
| 138 |
|
| 139 |
func newProgram(tb testing.TB, spec *ProgramSpec, opts *ProgramOptions) (*Program, error) { |
| 140 |
tb.Helper() |
| 141 |
|
| 142 |
if opts == nil { |
| 143 |
opts = new(ProgramOptions) |
| 144 |
} |
| 145 |
|
| 146 |
spec = fixupProgramSpec(spec) |
| 147 |
|
| 148 |
prog, err := NewProgramWithOptions(spec, *opts) |
| 149 |
testutils.SkipIfNotSupportedOnOS(tb, err) |
| 150 |
if err != nil { |
| 151 |
return nil, err |
| 152 |
} |
| 153 |
|
| 154 |
tb.Cleanup(func() { prog.Close() }) |
| 155 |
return prog, nil |
| 156 |
} |
| 157 |
|
| 158 |
func mustNewProgram(tb testing.TB, spec *ProgramSpec, opts *ProgramOptions) *Program { |
| 159 |
tb.Helper() |
| 160 |
|
| 161 |
prog, err := newProgram(tb, spec, opts) |
| 162 |
qt.Assert(tb, qt.IsNil(err)) |
| 163 |
return prog |
| 164 |
} |
| 165 |
|
| 166 |
func createProgram(tb testing.TB, typ ProgramType, retval int64) *Program { |
| 167 |
tb.Helper() |
| 168 |
|
| 169 |
return mustNewProgram(tb, &ProgramSpec{ |
| 170 |
Name: "test", |
| 171 |
Type: typ, |
| 172 |
Instructions: asm.Instructions{ |
| 173 |
asm.LoadImm(asm.R0, retval, asm.DWord), |
| 174 |
asm.Return(), |
| 175 |
}, |
| 176 |
License: "MIT", |
| 177 |
}, nil) |
| 178 |
} |
| 179 |
|
| 180 |
var basicProgramSpec = &ProgramSpec{ |
| 181 |
Name: "test", |
| 182 |
Type: SocketFilter, |
| 183 |
Instructions: asm.Instructions{ |
| 184 |
asm.LoadImm(asm.R0, 2, asm.DWord), |
| 185 |
asm.Return(), |
| 186 |
}, |
| 187 |
License: "MIT", |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
func createBasicProgram(tb testing.TB) *Program { |
| 193 |
return mustNewProgram(tb, basicProgramSpec, nil) |
| 194 |
} |
| 195 |
|
| 196 |
func newCollection(tb testing.TB, spec *CollectionSpec, opts *CollectionOptions) (*Collection, error) { |
| 197 |
tb.Helper() |
| 198 |
|
| 199 |
testutils.SkipNonNativeEndian(tb, spec.ByteOrder) |
| 200 |
|
| 201 |
spec = fixupCollectionSpec(spec) |
| 202 |
|
| 203 |
if opts == nil { |
| 204 |
opts = new(CollectionOptions) |
| 205 |
} |
| 206 |
|
| 207 |
c, err := NewCollectionWithOptions(spec, *opts) |
| 208 |
testutils.SkipIfNotSupportedOnOS(tb, err) |
| 209 |
if err != nil { |
| 210 |
return nil, err |
| 211 |
} |
| 212 |
|
| 213 |
tb.Cleanup(func() { c.Close() }) |
| 214 |
return c, nil |
| 215 |
} |
| 216 |
|
| 217 |
func mustNewCollection(tb testing.TB, spec *CollectionSpec, opts *CollectionOptions) *Collection { |
| 218 |
tb.Helper() |
| 219 |
c, err := newCollection(tb, spec, opts) |
| 220 |
qt.Assert(tb, qt.IsNil(err)) |
| 221 |
return c |
| 222 |
} |
| 223 |
|
| 224 |
func loadAndAssign(tb testing.TB, spec *CollectionSpec, to any, opts *CollectionOptions) error { |
| 225 |
tb.Helper() |
| 226 |
spec = fixupCollectionSpec(spec) |
| 227 |
err := spec.LoadAndAssign(to, opts) |
| 228 |
testutils.SkipIfNotSupported(tb, err) |
| 229 |
return err |
| 230 |
} |
| 231 |
|
| 232 |
func mustLoadAndAssign(tb testing.TB, spec *CollectionSpec, to any, opts *CollectionOptions) { |
| 233 |
qt.Assert(tb, qt.IsNil(loadAndAssign(tb, spec, to, opts))) |
| 234 |
} |
| 235 |
|
| 236 |
func mustRun(tb testing.TB, prog *Program, opts *RunOptions) (retval uint32) { |
| 237 |
tb.Helper() |
| 238 |
|
| 239 |
if opts == nil { |
| 240 |
opts = &RunOptions{} |
| 241 |
} |
| 242 |
if platform.IsLinux && opts.Data == nil { |
| 243 |
opts.Data = internal.EmptyBPFContext |
| 244 |
} |
| 245 |
if platform.IsWindows { |
| 246 |
switch prog.Type() { |
| 247 |
case WindowsSample: |
| 248 |
const minSampleContextLen = 32 |
| 249 |
|
| 250 |
if opts.Context == nil { |
| 251 |
opts.Context = make([]byte, minSampleContextLen) |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
ret, err := prog.Run(opts) |
| 257 |
testutils.SkipIfNotSupported(tb, err) |
| 258 |
qt.Assert(tb, qt.IsNil(err)) |
| 259 |
|
| 260 |
return ret |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
func fixupMapType(typ MapType) MapType { |
| 267 |
if !platform.IsWindows { |
| 268 |
return typ |
| 269 |
} |
| 270 |
|
| 271 |
switch typ { |
| 272 |
case Array: |
| 273 |
return WindowsArray |
| 274 |
case Hash: |
| 275 |
return WindowsHash |
| 276 |
case ProgramArray: |
| 277 |
return WindowsProgramArray |
| 278 |
case PerCPUHash: |
| 279 |
return WindowsPerCPUHash |
| 280 |
case PerCPUArray: |
| 281 |
return WindowsPerCPUArray |
| 282 |
case LRUHash: |
| 283 |
return WindowsLRUHash |
| 284 |
case LRUCPUHash: |
| 285 |
return WindowsLRUCPUHash |
| 286 |
case ArrayOfMaps: |
| 287 |
return WindowsArrayOfMaps |
| 288 |
case HashOfMaps: |
| 289 |
return WindowsHashOfMaps |
| 290 |
case LPMTrie: |
| 291 |
return WindowsLPMTrie |
| 292 |
case Queue: |
| 293 |
return WindowsQueue |
| 294 |
case Stack: |
| 295 |
return WindowsStack |
| 296 |
case RingBuf: |
| 297 |
return WindowsRingBuf |
| 298 |
default: |
| 299 |
return typ |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
func fixupMapSpec(spec *MapSpec) *MapSpec { |
| 304 |
if !platform.IsWindows { |
| 305 |
return spec |
| 306 |
} |
| 307 |
|
| 308 |
spec = spec.Copy() |
| 309 |
spec.Type = fixupMapType(spec.Type) |
| 310 |
if spec.InnerMap != nil { |
| 311 |
spec.InnerMap.Type = fixupMapType(spec.InnerMap.Type) |
| 312 |
} |
| 313 |
|
| 314 |
return spec |
| 315 |
} |
| 316 |
|
| 317 |
func fixupProgramType(typ ProgramType) ProgramType { |
| 318 |
if !platform.IsWindows { |
| 319 |
return typ |
| 320 |
} |
| 321 |
|
| 322 |
switch typ { |
| 323 |
case SocketFilter: |
| 324 |
return WindowsSample |
| 325 |
case XDP: |
| 326 |
return WindowsSample |
| 327 |
default: |
| 328 |
return typ |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
func fixupProgramSpec(spec *ProgramSpec) *ProgramSpec { |
| 333 |
if !platform.IsWindows { |
| 334 |
return spec |
| 335 |
} |
| 336 |
|
| 337 |
spec = spec.Copy() |
| 338 |
spec.Type = fixupProgramType(spec.Type) |
| 339 |
|
| 340 |
for i, ins := range spec.Instructions { |
| 341 |
if ins.IsBuiltinCall() { |
| 342 |
switch asm.BuiltinFunc(ins.Constant) { |
| 343 |
case asm.FnMapUpdateElem: |
| 344 |
spec.Instructions[i].Constant = int64(asm.WindowsFnMapUpdateElem) |
| 345 |
case asm.FnMapLookupElem: |
| 346 |
spec.Instructions[i].Constant = int64(asm.WindowsFnMapLookupElem) |
| 347 |
case asm.FnTailCall: |
| 348 |
spec.Instructions[i].Constant = int64(asm.WindowsFnTailCall) |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
return spec |
| 354 |
} |
| 355 |
|
| 356 |
func fixupCollectionSpec(spec *CollectionSpec) *CollectionSpec { |
| 357 |
if !platform.IsWindows { |
| 358 |
return spec |
| 359 |
} |
| 360 |
|
| 361 |
spec = spec.Copy() |
| 362 |
for name := range spec.Maps { |
| 363 |
spec.Maps[name] = fixupMapSpec(spec.Maps[name]) |
| 364 |
} |
| 365 |
|
| 366 |
for name := range spec.Programs { |
| 367 |
spec.Programs[name] = fixupProgramSpec(spec.Programs[name]) |
| 368 |
} |
| 369 |
|
| 370 |
return spec |
| 371 |
} |