Skip to content

Commit cc9c63f

Browse files
jschwinger233brb
authored andcommitted
Stop tracking skb in the end of its lifetime
`kfree_skbmem` marks the end of an skb's lifetime, so we stop tracking it at that time when `--filter-track-skb` is enabled. Signed-off-by: Zhichuan Liang <[email protected]>
1 parent 3f39b56 commit cc9c63f

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

bpf/kprobe_pwru.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,4 +321,13 @@ PWRU_ADD_KPROBE(5)
321321
#undef PWRU_HAS_GET_FUNC_IP
322322
#undef PWRU_KPROBE_TYPE
323323

324+
SEC("kprobe/skb_lifetime_termination")
325+
int kprobe_skb_lifetime_termination(struct pt_regs *ctx) {
326+
u64 skb = (u64) PT_REGS_PARM1(ctx);
327+
if (cfg->track_skb)
328+
bpf_map_delete_elem(&skb_addresses, &skb);
329+
330+
return 0;
331+
}
332+
324333
char __license[] SEC("license") = "Dual BSD/GPL";

internal/pwru/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ type KProbePrograms interface {
138138
GetKprobeSkb3() *ebpf.Program
139139
GetKprobeSkb4() *ebpf.Program
140140
GetKprobeSkb5() *ebpf.Program
141+
GetKprobeSkbLifetimeTermination() *ebpf.Program
141142
}
142143

143144
type KProbeObjects interface {

main.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ func main() {
124124
}
125125

126126
for name, program := range bpfSpec.Programs {
127+
if name == "kprobe_skb_lifetime_termination" {
128+
continue
129+
}
127130
if err = libpcap.InjectFilter(program, flags.FilterPcap); err != nil {
128131
log.Fatalf("Failed to inject filter ebpf for %s: %v", name, err)
129132
}
@@ -153,6 +156,7 @@ func main() {
153156
kprobe3 := objs.GetKprobeSkb3()
154157
kprobe4 := objs.GetKprobeSkb4()
155158
kprobe5 := objs.GetKprobeSkb5()
159+
kprobeLifetimeTermination := objs.GetKprobeSkbLifetimeTermination()
156160

157161
events := objs.GetEvents()
158162
printStackMap := objs.GetPrintStackMap()
@@ -187,6 +191,22 @@ func main() {
187191
log.Printf("Attaching kprobes (via %s)...\n", msg)
188192
ignored := 0
189193
bar := pb.StartNew(len(funcs))
194+
195+
if flags.FilterTrackSkb {
196+
kp, err := link.Kprobe("kfree_skbmem", kprobeLifetimeTermination, nil)
197+
bar.Increment()
198+
if err != nil {
199+
if !errors.Is(err, os.ErrNotExist) {
200+
log.Fatalf("Opening kprobe kfree_skbmem: %s\n", err)
201+
} else {
202+
ignored += 1
203+
log.Printf("Warn: kfree_skbmem not found, pwru is likely to mismatch skb due to lack of skb lifetime management\n")
204+
}
205+
} else {
206+
kprobes = append(kprobes, kp)
207+
}
208+
}
209+
190210
funcsByPos := pwru.GetFuncsByPos(funcs)
191211
for pos, fns := range funcsByPos {
192212
var fn *ebpf.Program

0 commit comments

Comments
 (0)