From 0dc98291c680e9a5935ed84f93d904c7f16528f8 Mon Sep 17 00:00:00 2001 From: Leon Hwang Date: Thu, 3 Oct 2024 20:26:56 +0800 Subject: [PATCH] Fix a minor code logic issue of xdp tracing In `TraceXDP()`, it traces XDP progs by `fentry_xdp` and `fexit_xdp` at the same time. However, in `tracing.trace()`, it will reset its `links` and `progs`. It should not reset them to support tracing XDP progs by `fentry_xdp` and `fexit_xdp` at the same time. So, it's better to propagate the target tracing bpf progs to `tracing.trace()` from `TraceXDP()` and `TraceTC()`. Signed-off-by: Leon Hwang --- internal/pwru/tracing.go | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/internal/pwru/tracing.go b/internal/pwru/tracing.go index 29c5f1fd..f1751f1b 100644 --- a/internal/pwru/tracing.go +++ b/internal/pwru/tracing.go @@ -127,13 +127,8 @@ func (t *tracing) traceProg(spec *ebpf.CollectionSpec, func (t *tracing) trace(coll *ebpf.Collection, spec *ebpf.CollectionSpec, opts *ebpf.CollectionOptions, outputSkb bool, outputShinfo bool, - n2a BpfProgName2Addr, progType ebpf.ProgramType, tracingName string, + n2a BpfProgName2Addr, progs []*ebpf.Program, tracingName string, ) error { - progs, err := listBpfProgs(progType) - if err != nil { - return fmt.Errorf("failed to list bpf progs: %w", err) - } - // Reusing maps from previous collection is to handle the events together // with the kprobes. replacedMaps := map[string]*ebpf.Map{ @@ -148,9 +143,6 @@ func (t *tracing) trace(coll *ebpf.Collection, spec *ebpf.CollectionSpec, } opts.MapReplacements = replacedMaps - t.links = make([]link.Link, 0, len(progs)) - t.progs = progs - var errg errgroup.Group for _, prog := range progs { @@ -174,8 +166,16 @@ func TraceTC(coll *ebpf.Collection, spec *ebpf.CollectionSpec, ) *tracing { log.Printf("Attaching tc-bpf progs...\n") + progs, err := listBpfProgs(ebpf.SchedCLS) + if err != nil { + log.Fatalf("failed to list tc-bpf progs: %v", err) + } + var t tracing - if err := t.trace(coll, spec, opts, outputSkb, outputShinfo, n2a, ebpf.SchedCLS, "fentry_tc"); err != nil { + t.progs = progs + t.links = make([]link.Link, 0, len(progs)) + + if err := t.trace(coll, spec, opts, outputSkb, outputShinfo, n2a, progs, "fentry_tc"); err != nil { log.Fatalf("failed to trace TC progs: %v", err) } @@ -188,11 +188,19 @@ func TraceXDP(coll *ebpf.Collection, spec *ebpf.CollectionSpec, ) *tracing { log.Printf("Attaching xdp progs...\n") + progs, err := listBpfProgs(ebpf.XDP) + if err != nil { + log.Fatalf("failed to list XDP progs: %v", err) + } + var t tracing + t.progs = progs + t.links = make([]link.Link, 0, len(progs)*2) + { spec := spec.Copy() delete(spec.Programs, "fexit_xdp") - if err := t.trace(coll, spec, opts, outputSkb, outputShinfo, n2a, ebpf.XDP, "fentry_xdp"); err != nil { + if err := t.trace(coll, spec, opts, outputSkb, outputShinfo, n2a, progs, "fentry_xdp"); err != nil { log.Fatalf("failed to trace XDP progs: %v", err) } } @@ -200,7 +208,7 @@ func TraceXDP(coll *ebpf.Collection, spec *ebpf.CollectionSpec, { spec := spec.Copy() delete(spec.Programs, "fentry_xdp") - if err := t.trace(coll, spec, opts, outputSkb, outputShinfo, n2a, ebpf.XDP, "fexit_xdp"); err != nil { + if err := t.trace(coll, spec, opts, outputSkb, outputShinfo, n2a, progs, "fexit_xdp"); err != nil { log.Fatalf("failed to trace XDP progs: %v", err) } }