| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package test |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
_ "embed" |
| 9 |
"fmt" |
| 10 |
"io" |
| 11 |
"structs" |
| 12 |
|
| 13 |
"github.com/cilium/ebpf" |
| 14 |
) |
| 15 |
|
| 16 |
type testBar struct { |
| 17 |
_ structs.HostLayout |
| 18 |
A uint64 |
| 19 |
B uint32 |
| 20 |
_ [4]byte |
| 21 |
} |
| 22 |
|
| 23 |
type testBarfoo struct { |
| 24 |
_ structs.HostLayout |
| 25 |
Bar int64 |
| 26 |
Baz bool |
| 27 |
_ [3]byte |
| 28 |
Boo testE |
| 29 |
} |
| 30 |
|
| 31 |
type testBaz struct { |
| 32 |
_ structs.HostLayout |
| 33 |
A uint64 |
| 34 |
} |
| 35 |
|
| 36 |
type testE uint32 |
| 37 |
|
| 38 |
const ( |
| 39 |
testEHOOPY testE = 0 |
| 40 |
testEFROOD testE = 1 |
| 41 |
) |
| 42 |
|
| 43 |
type testUbar struct { |
| 44 |
_ structs.HostLayout |
| 45 |
A uint32 |
| 46 |
_ [4]byte |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
const ( |
| 53 |
testMapMap1 = "map1" |
| 54 |
testProgFilter = "filter" |
| 55 |
testVarAnInt = "an_int" |
| 56 |
testVarIntArray = "int_array" |
| 57 |
testVarMyConstant = "my_constant" |
| 58 |
testVarStructArray = "struct_array" |
| 59 |
testVarStructConst = "struct_const" |
| 60 |
testVarStructVar = "struct_var" |
| 61 |
testVarUnionVar = "union_var" |
| 62 |
) |
| 63 |
|
| 64 |
|
| 65 |
func loadTest() (*ebpf.CollectionSpec, error) { |
| 66 |
reader := bytes.NewReader(_TestBytes) |
| 67 |
spec, err := ebpf.LoadCollectionSpecFromReader(reader) |
| 68 |
if err != nil { |
| 69 |
return nil, fmt.Errorf("can't load test: %w", err) |
| 70 |
} |
| 71 |
|
| 72 |
return spec, err |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
func loadTestObjects(obj any, opts *ebpf.CollectionOptions) error { |
| 85 |
spec, err := loadTest() |
| 86 |
if err != nil { |
| 87 |
return err |
| 88 |
} |
| 89 |
|
| 90 |
return spec.LoadAndAssign(obj, opts) |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
type testSpecs struct { |
| 97 |
testProgramSpecs |
| 98 |
testMapSpecs |
| 99 |
testVariableSpecs |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
type testProgramSpecs struct { |
| 106 |
Filter *ebpf.ProgramSpec `ebpf:"filter"` |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
type testMapSpecs struct { |
| 113 |
Map1 *ebpf.MapSpec `ebpf:"map1"` |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
type testVariableSpecs struct { |
| 120 |
AnInt *ebpf.VariableSpec `ebpf:"an_int"` |
| 121 |
IntArray *ebpf.VariableSpec `ebpf:"int_array"` |
| 122 |
MyConstant *ebpf.VariableSpec `ebpf:"my_constant"` |
| 123 |
StructArray *ebpf.VariableSpec `ebpf:"struct_array"` |
| 124 |
StructConst *ebpf.VariableSpec `ebpf:"struct_const"` |
| 125 |
StructVar *ebpf.VariableSpec `ebpf:"struct_var"` |
| 126 |
UnionVar *ebpf.VariableSpec `ebpf:"union_var"` |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
type testObjects struct { |
| 133 |
testPrograms |
| 134 |
testMaps |
| 135 |
testVariables |
| 136 |
} |
| 137 |
|
| 138 |
func (o *testObjects) Close() error { |
| 139 |
return _TestClose( |
| 140 |
&o.testPrograms, |
| 141 |
&o.testMaps, |
| 142 |
) |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
type testMaps struct { |
| 149 |
Map1 *ebpf.Map `ebpf:"map1"` |
| 150 |
} |
| 151 |
|
| 152 |
func (m *testMaps) Close() error { |
| 153 |
return _TestClose( |
| 154 |
m.Map1, |
| 155 |
) |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
type testVariables struct { |
| 162 |
AnInt *ebpf.Variable `ebpf:"an_int"` |
| 163 |
IntArray *ebpf.Variable `ebpf:"int_array"` |
| 164 |
MyConstant *ebpf.Variable `ebpf:"my_constant"` |
| 165 |
StructArray *ebpf.Variable `ebpf:"struct_array"` |
| 166 |
StructConst *ebpf.Variable `ebpf:"struct_const"` |
| 167 |
StructVar *ebpf.Variable `ebpf:"struct_var"` |
| 168 |
UnionVar *ebpf.Variable `ebpf:"union_var"` |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
type testPrograms struct { |
| 175 |
Filter *ebpf.Program `ebpf:"filter"` |
| 176 |
} |
| 177 |
|
| 178 |
func (p *testPrograms) Close() error { |
| 179 |
return _TestClose( |
| 180 |
p.Filter, |
| 181 |
) |
| 182 |
} |
| 183 |
|
| 184 |
func _TestClose(closers ...io.Closer) error { |
| 185 |
for _, closer := range closers { |
| 186 |
if err := closer.Close(); err != nil { |
| 187 |
return err |
| 188 |
} |
| 189 |
} |
| 190 |
return nil |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
var _TestBytes []byte |