Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelroquetto committed Dec 20, 2024
1 parent 176420c commit 963f892
Show file tree
Hide file tree
Showing 17 changed files with 851 additions and 461 deletions.
10 changes: 0 additions & 10 deletions pkg/internal/ebpf/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,6 @@ var IntegrityModeOverride = false

var ActiveNamespaces = make(map[uint32]uint32)

// These represent unique traffic control (tc) handles to be supplied as
// arguments to RegisterIngress, RegisterEgress or RegisterTC. They all start
// from the 0xb310 offset in simplistic attempt to avoid collisions with
// 3rdparty handles.
const (
NetollyTCHandle = 0xb310 + iota
HTTPTracerTCHandle
TCTracerTCHandle
)

// ProbeDesc holds the information of the instrumentation points of a given
// function/symbol
type ProbeDesc struct {
Expand Down
244 changes: 0 additions & 244 deletions pkg/internal/ebpf/common/tc_linux.go

This file was deleted.

3 changes: 0 additions & 3 deletions pkg/internal/ebpf/generictracer/generictracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,9 +481,6 @@ func (p *Tracer) AlreadyInstrumentedLib(id uint64) bool {
return module != nil
}

func (p *Tracer) SetupTC() {
}

func (p *Tracer) Run(ctx context.Context, eventsChan chan<- []request.Span) {
// At this point we now have loaded the bpf objects, which means we should insert any
// pids that are allowed into the bpf map
Expand Down
1 change: 0 additions & 1 deletion pkg/internal/ebpf/generictracer/generictracer_notlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,5 @@ func (p *Tracer) UnlinkInstrumentedLib(_ uint64) {}
func (p *Tracer) AlreadyInstrumentedLib(_ uint64) bool { return false }
func (p *Tracer) Run(_ context.Context, _ chan<- []request.Span) {}
func (p *Tracer) Constants() map[string]any { return nil }
func (p *Tracer) SetupTC() {}
func (p *Tracer) SetupTailCalls() {}
func (p *Tracer) RegisterOffsets(_ *exec.FileInfo, _ *goexec.Offsets) {}
2 changes: 0 additions & 2 deletions pkg/internal/ebpf/gotracer/gotracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,6 @@ func (p *Tracer) AlreadyInstrumentedLib(_ uint64) bool {
return false
}

func (p *Tracer) SetupTC() {}

func (p *Tracer) Run(ctx context.Context, eventsChan chan<- []request.Span) {
ebpfcommon.SharedRingbuf(
p.cfg,
Expand Down
51 changes: 19 additions & 32 deletions pkg/internal/ebpf/httptracer/httptracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import (
"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/ebpf/tcmanager"
"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"
)
Expand All @@ -27,19 +26,15 @@ type Tracer struct {
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
tcManager tcmanager.TCManager
}

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{},
}
}

Expand Down Expand Up @@ -121,7 +116,11 @@ func (p *Tracer) AlreadyInstrumentedLib(uint64) bool {
return false
}

func (p *Tracer) SetupTC() {
func (p *Tracer) startTC(ctx context.Context) {
if p.tcManager != nil {
return
}

if !p.cfg.EBPF.UseTCForL7CP {
return
}
Expand All @@ -131,41 +130,29 @@ func (p *Tracer) SetupTC() {
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)
p.tcManager = tcmanager.NewNetlinkManager()
p.tcManager.AddProgram("tc/tc_http_egress", p.bpfObjects.BeylaTcHttpEgress, tcmanager.AttachmentEgress)
p.tcManager.AddProgram("tc/tc_http_ingress", p.bpfObjects.BeylaTcHttpIngress, tcmanager.AttachmentIngress)
p.tcManager.Start(ctx)
}

func (p *Tracer) Run(ctx context.Context, _ chan<- []request.Span) {
p.startTC(ctx)

<-ctx.Done()

p.bpfObjects.Close()

p.closeTC()
p.stopTC()
}

func (p *Tracer) registerTC(iface ifaces.Interface) {
links := ebpfcommon.RegisterTC(iface,
p.bpfObjects.BeylaTcHttpEgress.FD(), ebpfcommon.HTTPTracerTCHandle, "tc/tc_http_egress",
p.bpfObjects.BeylaTcHttpIngress.FD(), ebpfcommon.HTTPTracerTCHandle, "tc/tc_http_ingress",
p.log)

if links == nil {
func (p *Tracer) stopTC() {
if p.tcManager == 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.BeylaTcHttpEgress.Close()
p.bpfObjects.BeylaTcHttpIngress.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{}
p.tcManager.Stop()
p.tcManager = nil
}
1 change: 0 additions & 1 deletion pkg/internal/ebpf/httptracer/httptracer_notlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,5 @@ func (p *Tracer) UnlinkInstrumentedLib(_ uint64) {}
func (p *Tracer) AlreadyInstrumentedLib(_ uint64) bool { return false }
func (p *Tracer) Run(_ context.Context, _ chan<- []request.Span) {}
func (p *Tracer) Constants() map[string]any { return nil }
func (p *Tracer) SetupTC() {}
func (p *Tracer) SetupTailCalls() {}
func (p *Tracer) RegisterOffsets(_ *exec.FileInfo, _ *goexec.Offsets) {}
Loading

0 comments on commit 963f892

Please sign in to comment.