forked from Asphaltt/skbtracer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebpf.go
236 lines (195 loc) · 4.83 KB
/
ebpf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package skbtracer
import (
"bytes"
"fmt"
"sync"
"github.com/dropbox/goebpf"
)
var (
eventReceived uint64
eventLost uint64
eventAbnormal uint64
)
// EventStat is the statistics of bpf perf event.
type EventStat struct {
Received uint64
Lost uint64
Abnormal uint64
}
// GetEventStat you should get event stat after the cancel function calling.
func GetEventStat() EventStat {
return EventStat{eventReceived, eventLost, eventAbnormal}
}
type bpfProgram struct {
bpf goebpf.System
pe *goebpf.PerfEvents
wg sync.WaitGroup
ev chan *Event
stop chan struct{}
withCallstack bool
}
// Start loads bpf programs from bpfProg with config.
// It loads the kprobes from bpfProg, and attaches the kprobes into the kernel.
// Return an event channel and a cancel function, the cancel funtion is for
// detaching the kprobes from the kernel.
func Start(bpfProg []byte, c *Config) (<-chan *Event, func(), error) {
bp, err := loadProgram(bpfProg, c)
if err != nil {
return nil, nil, err
}
return bp.ev, bp.detachProbes, nil
}
func loadProgram(bpfProg []byte, c *Config) (*bpfProgram, error) {
// create system
bpf := goebpf.NewDefaultEbpfSystem()
// load compiled ebpf elf file
if err := bpf.Load(bytes.NewReader(bpfProg)); err != nil {
return nil, fmt.Errorf("failed to load elf file, err: %w", err)
}
// load programs
for _, prog := range bpf.GetPrograms() {
if err := prog.Load(); err != nil {
return nil, fmt.Errorf("failed to load prog(%s), err: %w",
prog.GetName(), err)
}
}
var bp bpfProgram
bp.bpf = bpf
bp.ev = make(chan *Event)
bp.stop = make(chan struct{})
bp.withCallstack = c.CallStack
err := bp.storeConfig(c)
if err := bp.attachProbes(); err != nil {
return nil, fmt.Errorf("failed to attach kprobes, err: %w", err)
}
return &bp, err
}
func (p *bpfProgram) storeConfig(c *Config) error {
if err := c.parse(); err != nil {
return err
}
m := p.bpf.GetMapByName("tracer_cfg")
if m == nil {
return fmt.Errorf("bpf map(tracer_cfg) not found")
}
update := func(k byte, v uint64, key string) error {
err := m.Upsert(k, v)
if err != nil {
err = fmt.Errorf("failed to store bpf config(%s), err: %w", key, err)
}
return err
}
bool2uint64 := func(b bool) uint64 {
if b {
return 1
}
return 0
}
configs := []struct {
name string
k byte
v uint64
}{
{"pid", 1, uint64(c.Pid)},
{"ip", 2, uint64(c.ip)},
{"port", 3, uint64(c.Port)},
{"icmpid", 4, uint64(c.IcmpID)},
{"dropstack", 5, bool2uint64(c.DropStack)},
{"callstack", 6, bool2uint64(c.CallStack)},
{"iptable", 7, bool2uint64(c.Iptable)},
{"noroute", 8, bool2uint64(c.NoRoute)},
{"keep", 9, bool2uint64(c.Keep)},
{"proto", 10, uint64(c.proto)},
{"netns", 11, uint64(c.NetNS)},
}
for _, c := range configs {
if c.v != 0 {
if err := update(c.k, c.v, c.name); err != nil {
return err
}
}
}
return nil
}
func (p *bpfProgram) startPerfEvents(events <-chan []byte) {
p.wg.Add(1)
go func(events <-chan []byte) {
defer p.wg.Done()
for {
var data []byte
select {
case <-p.stop:
return
case b, ok := <-events:
if !ok {
return
}
data = b
}
var ev bpfEvent
err := ev.unmarshal(data)
if err != nil {
eventAbnormal++
continue
}
e := ev.toEvent()
p.getCallStack(ev.KernelStackID, e)
select {
case p.ev <- e:
case <-p.stop:
return
}
}
}(events)
}
func (p *bpfProgram) getCallStack(id int32, e *Event) {
if !p.withCallstack || id < 0 {
return
}
m := p.bpf.GetMapByName("stacks")
if m == nil {
return
}
e.CallStack, _ = m.Lookup(id)
}
func (p *bpfProgram) stopPerfEvents() {
p.pe.Stop()
close(p.stop)
p.wg.Wait()
eventReceived = uint64(p.pe.EventsReceived)
eventLost = uint64(p.pe.EventsLost)
}
func (p *bpfProgram) attachProbes() error {
// attach all probe programs
for _, prog := range p.bpf.GetPrograms() {
if err := prog.Attach(nil); err != nil {
return fmt.Errorf("failed to attach prog(%s), err: %w",
prog.GetName(), err)
}
}
// get handles to perf event map
m := p.bpf.GetMapByName("route_event")
if m == nil {
return fmt.Errorf("bpf map(route_event) not found")
}
// create perf events
var err error
p.pe, err = goebpf.NewPerfEvents(m)
if err != nil {
return fmt.Errorf("failed to new perf-event, err: %w", err)
}
events, err := p.pe.StartForAllProcessesAndCPUs(4096)
if err != nil {
return fmt.Errorf("failed to start perf-event, err: %w", err)
}
// start event listeners
p.startPerfEvents(events)
return nil
}
func (p *bpfProgram) detachProbes() {
p.stopPerfEvents()
for _, prog := range p.bpf.GetPrograms() {
prog.Detach()
prog.Close()
}
}