-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathhttptracer.go
167 lines (127 loc) · 4.22 KB
/
httptracer.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
//go:build linux
package httptracer
import (
"context"
"io"
"log/slog"
"github.com/cilium/ebpf"
"github.com/vishvananda/netlink"
"github.com/grafana/beyla/pkg/beyla"
ebpfcommon "github.com/grafana/beyla/pkg/internal/ebpf/common"
"github.com/grafana/beyla/pkg/internal/exec"
"github.com/grafana/beyla/pkg/internal/goexec"
"github.com/grafana/beyla/pkg/internal/netolly/ifaces"
"github.com/grafana/beyla/pkg/internal/request"
"github.com/grafana/beyla/pkg/internal/svc"
)
//go:generate $BPF2GO -cc $BPF_CLANG -cflags $BPF_CFLAGS -target amd64,arm64 bpf ../../../../bpf/tc_http_tp.c -- -I../../../../bpf/headers
//go:generate $BPF2GO -cc $BPF_CLANG -cflags $BPF_CFLAGS -target amd64,arm64 bpf_debug ../../../../bpf/tc_http_tp.c -- -I../../../../bpf/headers -DBPF_DEBUG
type Tracer struct {
cfg *beyla.Config
bpfObjects bpfObjects
closers []io.Closer
log *slog.Logger
qdiscs map[ifaces.Interface]*netlink.GenericQdisc
egressFilters map[ifaces.Interface]*netlink.BpfFilter
ingressFilters map[ifaces.Interface]*netlink.BpfFilter
}
func New(cfg *beyla.Config) *Tracer {
log := slog.With("component", "tc_http.Tracer")
return &Tracer{
log: log,
cfg: cfg,
qdiscs: map[ifaces.Interface]*netlink.GenericQdisc{},
egressFilters: map[ifaces.Interface]*netlink.BpfFilter{},
ingressFilters: map[ifaces.Interface]*netlink.BpfFilter{},
}
}
func (p *Tracer) AllowPID(uint32, uint32, *svc.Attrs) {}
func (p *Tracer) BlockPID(uint32, uint32) {}
func (p *Tracer) Load() (*ebpf.CollectionSpec, error) {
if p.cfg.EBPF.BpfDebug {
return loadBpf_debug()
}
return loadBpf()
}
func (p *Tracer) SetupTailCalls() {
for _, tc := range []struct {
index int
prog *ebpf.Program
}{
{
index: 0,
prog: p.bpfObjects.ExtendSkb,
},
} {
err := p.bpfObjects.TcL7JumpTable.Update(uint32(tc.index), uint32(tc.prog.FD()), ebpf.UpdateAny)
if err != nil {
p.log.Error("error loading info tail call jump table", "error", err)
}
}
}
func (p *Tracer) Constants() map[string]any {
return map[string]any{}
}
func (p *Tracer) RegisterOffsets(_ *exec.FileInfo, _ *goexec.Offsets) {}
func (p *Tracer) BpfObjects() any {
return &p.bpfObjects
}
func (p *Tracer) AddCloser(c ...io.Closer) {
p.closers = append(p.closers, c...)
}
func (p *Tracer) GoProbes() map[string][]*ebpfcommon.ProbeDesc {
return nil
}
func (p *Tracer) KProbes() map[string]ebpfcommon.ProbeDesc {
return nil
}
func (p *Tracer) Tracepoints() map[string]ebpfcommon.ProbeDesc {
return nil
}
func (p *Tracer) UProbes() map[string]map[string][]*ebpfcommon.ProbeDesc {
return nil
}
func (p *Tracer) SocketFilters() []*ebpf.Program {
return nil
}
func (p *Tracer) SockMsgs() []ebpfcommon.SockMsg { return nil }
func (p *Tracer) SockOps() []ebpfcommon.SockOps { return nil }
func (p *Tracer) RecordInstrumentedLib(uint64, []io.Closer) {}
func (p *Tracer) AddInstrumentedLibRef(uint64) {}
func (p *Tracer) UnlinkInstrumentedLib(uint64) {}
func (p *Tracer) AlreadyInstrumentedLib(uint64) bool {
return false
}
func (p *Tracer) SetupTC() {
if !p.cfg.EBPF.UseTCForL7CP {
return
}
p.log.Info("enabling L7 context-propagation with Linux Traffic Control")
if !ebpfcommon.SupportsEBPFLoops() {
p.log.Error("cannot enable L7 context-propagation, kernel 5.17 or newer required")
}
ebpfcommon.WatchAndRegisterTC(context.Background(), p.cfg.ChannelBufferLen, p.registerTC, p.log)
}
func (p *Tracer) Run(ctx context.Context, _ chan<- []request.Span) {
<-ctx.Done()
p.bpfObjects.Close()
p.closeTC()
}
func (p *Tracer) registerTC(iface ifaces.Interface) {
links := ebpfcommon.RegisterTC(iface, p.bpfObjects.TcHttpEgress.FD(), p.bpfObjects.TcHttpIngress.FD(), p.log)
if links == nil {
return
}
p.qdiscs[iface] = links.Qdisc
p.ingressFilters[iface] = links.IngressFilter
p.egressFilters[iface] = links.EgressFilter
}
func (p *Tracer) closeTC() {
p.log.Info("removing traffic control probes")
p.bpfObjects.TcHttpEgress.Close()
p.bpfObjects.TcHttpIngress.Close()
ebpfcommon.CloseTCLinks(p.qdiscs, p.egressFilters, p.ingressFilters, p.log)
p.egressFilters = map[ifaces.Interface]*netlink.BpfFilter{}
p.ingressFilters = map[ifaces.Interface]*netlink.BpfFilter{}
p.qdiscs = map[ifaces.Interface]*netlink.GenericQdisc{}
}