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