-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
328 lines (273 loc) · 8.14 KB
/
main.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package main
import (
"bytes"
"context"
"encoding/binary"
"errors"
"log"
"os"
"os/signal"
"syscall"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/perf"
"github.com/cilium/ebpf/rlimit"
"github.com/jschwinger233/elibpcap"
"golang.org/x/sync/errgroup"
)
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc clang tcDump ./ebpf/tc_dump.c -- -D__TARGET_ARCH_x86 -I./ebpf/headers -Wall -g -O2 -mcpu=v3
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc clang fentryTc ./ebpf/fentry_tc.c -- -D__TARGET_ARCH_x86 -I./ebpf/headers -Wall -g -O2 -mcpu=v3
const (
DirectionIngress = 1
DirectionEgress = 2
DirIngress = "INGRESS"
DirEgress = "EGRESS"
)
func main() {
if err := rlimit.RemoveMemlock(); err != nil {
log.Fatalf("Failed to remove rlimit memlock: %v", err)
}
flags := parseFlags()
cfg := newConfig(flags)
devs := flags.getDevices()
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
specTc, err := loadTcDump()
if err != nil {
log.Fatalf("Failed to load bpf spec: %v", err)
}
eventMapSpec := specTc.Maps["events"]
eventMap, err := ebpf.NewMap(eventMapSpec)
if err != nil {
log.Fatalf("Failed to create perf-event map: %v", err)
}
defer eventMap.Close()
progSpec := specTc.Programs["on_ingress"]
progSpec.Instructions, err = elibpcap.Inject(flags.PcapFilterExpr,
progSpec.Instructions, elibpcap.Options{
AtBpf2Bpf: "filter_pcap_ebpf_l2",
DirectRead: true,
L2Skb: true,
})
if err != nil {
log.Fatalf("Failed to inject pcap filter: %v", err)
}
progSpec = specTc.Programs["on_egress"]
progSpec.Instructions, err = elibpcap.Inject(flags.PcapFilterExpr,
progSpec.Instructions, elibpcap.Options{
AtBpf2Bpf: "filter_pcap_ebpf_l2",
DirectRead: true,
L2Skb: true,
})
if err != nil {
log.Fatalf("Failed to inject pcap filter: %v", err)
}
specFentry, err := loadFentryTc()
if err != nil {
log.Fatalf("Failed to load bpf spec: %v", err)
}
progSpec = specFentry.Programs["fentry_tc"]
progSpec.Instructions, err = elibpcap.Inject(flags.PcapFilterExpr,
progSpec.Instructions, elibpcap.Options{
AtBpf2Bpf: "filter_pcap_ebpf_l2",
DirectRead: false,
L2Skb: true,
})
if err != nil {
log.Fatalf("Failed to inject pcap filter: %v", err)
}
rewriteConst := map[string]interface{}{
"__cfg": *cfg,
}
wg, ctx := errgroup.WithContext(ctx)
for idx := range devs {
ifindex, ifname := idx, devs[idx]
rewriteConst["IFINDEX"] = uint32(ifindex)
progIngress, okIngress, err := checkTcFilter(ifindex, true)
if err != nil {
log.Fatalf("Failed to check tc filter ingress for if@%d:%s: %v", ifindex, ifname, err)
}
progEgress, okEgress, err := checkTcFilter(ifindex, false)
if err != nil {
log.Fatalf("Failed to check tc filter egress for if@%d:%s: %v", ifindex, ifname, err)
}
if okIngress {
defer progIngress.Close()
obj, err := attachFentryTc(progIngress, eventMap, specFentry, rewriteConst, ifindex, ifname, true)
if err != nil {
log.Fatalf("Failed to attach fentry-tc for if@%d:%s %s: %v", ifindex, ifname, DirIngress, err)
}
defer obj.Close()
wg.Go(func() error {
runFentryTc(ctx, obj, ifindex, ifname, true)
return nil
})
}
if okEgress {
defer progEgress.Close()
obj, err := attachFentryTc(progEgress, eventMap, specFentry, rewriteConst, ifindex, ifname, false)
if err != nil {
log.Fatalf("Failed to attach fentry-tc for if@%d:%s %s: %v", ifindex, ifname, DirEgress, err)
}
defer obj.Close()
wg.Go(func() error {
runFentryTc(ctx, obj, ifindex, ifname, false)
return nil
})
}
if !okIngress || !okEgress {
if err := specTc.RewriteConstants(rewriteConst); err != nil {
log.Fatalf("Failed to rewrite const for if@%d:%s: %v", ifindex, ifname, err)
}
var obj tcDumpObjects
if err := specTc.LoadAndAssign(&obj, &ebpf.CollectionOptions{
MapReplacements: map[string]*ebpf.Map{
"events": eventMap,
},
Programs: ebpf.ProgramOptions{
LogSize: ebpf.DefaultVerifierLogSize * 4,
},
}); err != nil {
var ve *ebpf.VerifierError
if errors.As(err, &ve) {
log.Printf("Failed to load bpf obj for if@%d:%s: %v\n%+v", ifindex, ifname, err, ve)
}
log.Fatalf("Failed to load bpf obj for if@%d:%s: %v", ifindex, ifname, err)
}
defer obj.Close()
wg.Go(func() error {
runTcDump(ctx, &obj, ifindex, ifname, !okIngress, !okEgress, flags.KeepTcQdisc)
return nil
})
}
}
wg.Go(func() error {
handlePerfEvent(ctx, eventMap, devs)
return nil
})
_ = wg.Wait()
}
func runTcDump(ctx context.Context, obj *tcDumpObjects, ifindex int, ifname string,
withIngress, withEgress, keepTcQdisc bool,
) {
if !withIngress && !withEgress {
return
}
if err := replaceTcQdisc(ifindex); err != nil {
log.Printf("Failed to replace tc-qdisc for if@%d:%s: %v", ifindex, ifname, err)
return
} else if !keepTcQdisc {
defer deleteTcQdisc(ifindex)
}
if withIngress {
if err := addTcFilterIngress(ifindex, obj.OnIngress); err != nil {
log.Printf("Failed to add tc-filter ingress for if@%d:%s: %v", ifindex, ifname, err)
return
} else {
defer deleteTcFilterIngress(ifindex, obj.OnIngress)
}
log.Printf("Listening events for if@%d:%s %s by TC...", ifindex, ifname, DirIngress)
}
if withEgress {
if err := addTcFilterEgress(ifindex, obj.OnEgress); err != nil {
log.Printf("Failed to add tc-filter egress for if@%d:%s: %v", ifindex, ifname, err)
return
} else {
defer deleteTcFilterEgress(ifindex, obj.OnEgress)
}
log.Printf("Listening events for if@%d:%s %s by TC...", ifindex, ifname, DirEgress)
}
<-ctx.Done()
}
func copyMap(src map[string]any) map[string]any {
dst := make(map[string]any, len(src))
for k, v := range src {
dst[k] = v
}
return dst
}
func attachFentryTc(prog *ebpf.Program, eventMap *ebpf.Map, spec *ebpf.CollectionSpec, rc map[string]any,
ifindex int, ifname string, isIngress bool,
) (*fentryTcObjects, error) {
dir := uint16(DirectionEgress)
if isIngress {
dir = uint16(DirectionIngress)
}
rewriteConst := copyMap(rc)
rewriteConst["DIR"] = dir
if err := spec.RewriteConstants(rewriteConst); err != nil {
log.Fatalf("Failed to rewrite const for if@%d:%s: %v", ifindex, ifname, err)
}
progEntry, err := getEntryFuncName(prog)
if err != nil {
log.Fatalf("Failed to get entry func name for if@%d:%s: %v", ifindex, ifname, err)
}
progSpec := spec.Programs["fentry_tc"]
progSpec.AttachTarget = prog
progSpec.AttachTo = progEntry
var obj fentryTcObjects
if err := spec.LoadAndAssign(&obj, &ebpf.CollectionOptions{
MapReplacements: map[string]*ebpf.Map{
"events": eventMap,
},
Programs: ebpf.ProgramOptions{
LogSize: ebpf.DefaultVerifierLogSize * 4,
},
}); err != nil {
var ve *ebpf.VerifierError
if errors.As(err, &ve) {
log.Printf("Failed to load bpf obj for if@%d:%s: %v\n%+v", ifindex, ifname, err, ve)
}
log.Fatalf("Failed to load bpf obj for if@%d:%s: %v", ifindex, ifname, err)
}
return &obj, nil
}
func runFentryTc(ctx context.Context, obj *fentryTcObjects, ifindex int, ifname string, isIngress bool) {
var direction string
if isIngress {
direction = DirIngress
} else {
direction = DirEgress
}
fentry, err := link.AttachTracing(link.TracingOptions{
Program: obj.FentryTc,
})
if err != nil {
log.Printf("Failed to attach fentry-tc for if@%d:%s %s: %v", ifindex, ifname, direction, err)
return
}
defer fentry.Close()
log.Printf("Listening events for if@%d:%s %s by Fentry...", ifindex, ifname, direction)
<-ctx.Done()
}
func handlePerfEvent(ctx context.Context, events *ebpf.Map, devs map[int]string) {
eventReader, err := perf.NewReader(events, 4096)
if err != nil {
log.Printf("Failed to create perf-event reader : %v", err)
return
}
go func() {
<-ctx.Done()
eventReader.Close()
}()
var ev event
for {
event, err := eventReader.Read()
if err != nil {
if errors.Is(err, perf.ErrClosed) {
return
}
log.Printf("Reading perf-event: %v", err)
}
if event.LostSamples != 0 {
log.Printf("Lost %d events", event.LostSamples)
}
binary.Read(bytes.NewBuffer(event.RawSample), binary.LittleEndian, &ev)
ev.output(devs)
select {
case <-ctx.Done():
return
default:
}
}
}