Skip to content

Commit

Permalink
Fix a minor code logic issue of xdp tracing
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Asphaltt authored and brb committed Oct 4, 2024
1 parent 1de15ff commit 0dc9829
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions internal/pwru/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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 {
Expand All @@ -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)
}

Expand All @@ -188,19 +188,27 @@ 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)
}
}

{
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)
}
}
Expand Down

0 comments on commit 0dc9829

Please sign in to comment.