| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package main |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
_ "embed" |
| 9 |
"fmt" |
| 10 |
"io" |
| 11 |
|
| 12 |
"github.com/cilium/ebpf" |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
const ( |
| 19 |
bpfProgEgressProgFunc = "egress_prog_func" |
| 20 |
bpfProgIngressProgFunc = "ingress_prog_func" |
| 21 |
bpfVarEgressPktCount = "egress_pkt_count" |
| 22 |
bpfVarIngressPktCount = "ingress_pkt_count" |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
func loadBpf() (*ebpf.CollectionSpec, error) { |
| 27 |
reader := bytes.NewReader(_BpfBytes) |
| 28 |
spec, err := ebpf.LoadCollectionSpecFromReader(reader) |
| 29 |
if err != nil { |
| 30 |
return nil, fmt.Errorf("can't load bpf: %w", err) |
| 31 |
} |
| 32 |
|
| 33 |
return spec, err |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
func loadBpfObjects(obj any, opts *ebpf.CollectionOptions) error { |
| 46 |
spec, err := loadBpf() |
| 47 |
if err != nil { |
| 48 |
return err |
| 49 |
} |
| 50 |
|
| 51 |
return spec.LoadAndAssign(obj, opts) |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
type bpfSpecs struct { |
| 58 |
bpfProgramSpecs |
| 59 |
bpfMapSpecs |
| 60 |
bpfVariableSpecs |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
type bpfProgramSpecs struct { |
| 67 |
EgressProgFunc *ebpf.ProgramSpec `ebpf:"egress_prog_func"` |
| 68 |
IngressProgFunc *ebpf.ProgramSpec `ebpf:"ingress_prog_func"` |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
type bpfMapSpecs struct { |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
type bpfVariableSpecs struct { |
| 81 |
EgressPktCount *ebpf.VariableSpec `ebpf:"egress_pkt_count"` |
| 82 |
IngressPktCount *ebpf.VariableSpec `ebpf:"ingress_pkt_count"` |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
type bpfObjects struct { |
| 89 |
bpfPrograms |
| 90 |
bpfMaps |
| 91 |
bpfVariables |
| 92 |
} |
| 93 |
|
| 94 |
func (o *bpfObjects) Close() error { |
| 95 |
return _BpfClose( |
| 96 |
&o.bpfPrograms, |
| 97 |
&o.bpfMaps, |
| 98 |
) |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
type bpfMaps struct { |
| 105 |
} |
| 106 |
|
| 107 |
func (m *bpfMaps) Close() error { |
| 108 |
return _BpfClose() |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
type bpfVariables struct { |
| 115 |
EgressPktCount *ebpf.Variable `ebpf:"egress_pkt_count"` |
| 116 |
IngressPktCount *ebpf.Variable `ebpf:"ingress_pkt_count"` |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
type bpfPrograms struct { |
| 123 |
EgressProgFunc *ebpf.Program `ebpf:"egress_prog_func"` |
| 124 |
IngressProgFunc *ebpf.Program `ebpf:"ingress_prog_func"` |
| 125 |
} |
| 126 |
|
| 127 |
func (p *bpfPrograms) Close() error { |
| 128 |
return _BpfClose( |
| 129 |
p.EgressProgFunc, |
| 130 |
p.IngressProgFunc, |
| 131 |
) |
| 132 |
} |
| 133 |
|
| 134 |
func _BpfClose(closers ...io.Closer) error { |
| 135 |
for _, closer := range closers { |
| 136 |
if err := closer.Close(); err != nil { |
| 137 |
return err |
| 138 |
} |
| 139 |
} |
| 140 |
return nil |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
var _BpfBytes []byte |