-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor code of attaching skb-tracking kprobe
Hide the attaching details of tracking skb from `main.go`. Signed-off-by: Leon Hwang <[email protected]>
- Loading branch information
Showing
2 changed files
with
63 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/* Copyright 2024 Authors of Cilium */ | ||
|
||
package pwru | ||
|
||
import ( | ||
"errors" | ||
"log" | ||
"os" | ||
|
||
"github.com/cilium/ebpf" | ||
"github.com/cilium/ebpf/link" | ||
) | ||
|
||
type skbTracker struct { | ||
links []link.Link | ||
} | ||
|
||
func (t *skbTracker) Close() { | ||
for _, l := range t.links { | ||
_ = l.Close() | ||
} | ||
t.links = nil | ||
} | ||
|
||
func TrackSkb(coll *ebpf.Collection, haveFexit bool) *skbTracker { | ||
var t skbTracker | ||
|
||
kp, err := link.Kprobe("kfree_skbmem", coll.Programs["kprobe_skb_lifetime_termination"], nil) | ||
if err != nil { | ||
if !errors.Is(err, os.ErrNotExist) { | ||
log.Fatalf("Opening kprobe kfree_skbmem: %s\n", err) | ||
} else { | ||
log.Printf("Warn: kfree_skbmem not found, pwru is likely to mismatch skb due to lack of skb lifetime management\n") | ||
} | ||
} else { | ||
t.links = append(t.links, kp) | ||
} | ||
|
||
if haveFexit { | ||
progs := []*ebpf.Program{ | ||
coll.Programs["fexit_skb_clone"], | ||
coll.Programs["fexit_skb_copy"], | ||
} | ||
for _, prog := range progs { | ||
fexit, err := link.AttachTracing(link.TracingOptions{ | ||
Program: prog, | ||
}) | ||
if err != nil { | ||
if !errors.Is(err, os.ErrNotExist) { | ||
log.Fatalf("Opening tracing(%s): %s\n", prog, err) | ||
} | ||
} else { | ||
t.links = append(t.links, fexit) | ||
} | ||
} | ||
} | ||
|
||
return &t | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters