From 7e5cb1784bc1172672bcd0b63067c2c745027053 Mon Sep 17 00:00:00 2001 From: Leon Hwang Date: Tue, 12 Nov 2024 21:26:32 +0800 Subject: [PATCH] bpf: Remove unnecessary OUTPUT_SKB macro When call set_skb_btf() or set_shinfo_btf(), these functions are protected by constant CFG. Then, while loading kprobe_pwru into kernel, if no --output-skb/--output-skb-shared-info, set_skb_btf()/set_shinfo_btf() won't be verified. Next, these unreachable code will be eliminated by verifier. Signed-off-by: Leon Hwang --- bpf/kprobe_pwru.c | 6 ------ build.go | 6 ++---- internal/pwru/utils.go | 19 +++++++++++++++++++ main.go | 10 +++++----- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/bpf/kprobe_pwru.c b/bpf/kprobe_pwru.c index 04fdb856..71d3382f 100644 --- a/bpf/kprobe_pwru.c +++ b/bpf/kprobe_pwru.c @@ -164,7 +164,6 @@ struct { __uint(value_size, MAX_STACK_DEPTH * sizeof(u64)); } print_stack_map SEC(".maps"); -#ifdef OUTPUT_SKB struct print_skb_value { u32 len; char str[PRINT_SKB_STR_SIZE]; @@ -197,7 +196,6 @@ struct { __type(key, u64); __type(value, struct print_shinfo_value); } print_shinfo_map SEC(".maps"); -#endif static __always_inline u32 get_netns(struct sk_buff *skb) { @@ -344,7 +342,6 @@ sync_fetch_and_add(void *id_map) { static __always_inline void set_skb_btf(struct sk_buff *skb, u64 *event_id) { -#ifdef OUTPUT_SKB static struct btf_ptr p = {}; static struct print_skb_value v = {}; u64 id; @@ -359,12 +356,10 @@ set_skb_btf(struct sk_buff *skb, u64 *event_id) { } bpf_map_update_elem(&print_skb_map, event_id, &v, BPF_ANY); -#endif } static __always_inline void set_shinfo_btf(struct sk_buff *skb, u64 *event_id) { -#ifdef OUTPUT_SKB struct skb_shared_info *shinfo; static struct btf_ptr p = {}; static struct print_shinfo_value v = {}; @@ -393,7 +388,6 @@ set_shinfo_btf(struct sk_buff *skb, u64 *event_id) { } bpf_map_update_elem(&print_shinfo_map, event_id, &v, BPF_ANY); -#endif } static __always_inline u64 diff --git a/build.go b/build.go index 800426fc..a7258379 100644 --- a/build.go +++ b/build.go @@ -2,9 +2,7 @@ // Copyright (C) 2021 Authors of Cilium */ //go:generate sh -c "echo Generating for $TARGET_GOARCH" -//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target $TARGET_GOARCH -cc clang -no-strip KProbePWRU ./bpf/kprobe_pwru.c -- -DOUTPUT_SKB -I./bpf/headers -Wno-address-of-packed-member -//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target $TARGET_GOARCH -cc clang -no-strip KProbeMultiPWRU ./bpf/kprobe_pwru.c -- -DOUTPUT_SKB -DHAS_KPROBE_MULTI -I./bpf/headers -Wno-address-of-packed-member -//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target $TARGET_GOARCH -cc clang -no-strip KProbePWRUWithoutOutputSKB ./bpf/kprobe_pwru.c -- -I./bpf/headers -Wno-address-of-packed-member -//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target $TARGET_GOARCH -cc clang -no-strip KProbeMultiPWRUWithoutOutputSKB ./bpf/kprobe_pwru.c -- -D HAS_KPROBE_MULTI -I./bpf/headers -Wno-address-of-packed-member +//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target $TARGET_GOARCH -cc clang -no-strip KProbePWRU ./bpf/kprobe_pwru.c -- -I./bpf/headers -Wno-address-of-packed-member +//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target $TARGET_GOARCH -cc clang -no-strip KProbeMultiPWRU ./bpf/kprobe_pwru.c -- -DHAS_KPROBE_MULTI -I./bpf/headers -Wno-address-of-packed-member package main diff --git a/internal/pwru/utils.go b/internal/pwru/utils.go index c81fdf7c..eacae941 100644 --- a/internal/pwru/utils.go +++ b/internal/pwru/utils.go @@ -194,3 +194,22 @@ func HaveAvailableFilterFunctions() bool { _, err := getAvailableFilterFunctions() return err == nil } + +func HaveSnprintfBtf(kernelBtf *btf.Spec) bool { + types, err := kernelBtf.AnyTypesByName("bpf_func_id") + if err != nil { + return false + } + + for _, t := range types { + if enum, ok := t.(*btf.Enum); ok { + for _, v := range enum.Values { + if v.Name == "BPF_FUNC_snprintf_btf" { + return true + } + } + } + } + + return false +} diff --git a/main.go b/main.go index 75364063..e6c01efc 100644 --- a/main.go +++ b/main.go @@ -67,6 +67,10 @@ func main() { log.Fatalf("Failed to load BTF spec: %s", err) } + if (flags.OutputSkb || flags.OutputShinfo) && !pwru.HaveSnprintfBtf(btfSpec) { + log.Fatal("Unsupported to output skb or shinfo because bpf_snprintf_btf() is unavailable") + } + if flags.AllKMods { files, err := os.ReadDir("/sys/kernel/btf") if err != nil { @@ -119,12 +123,8 @@ func main() { switch { case (flags.OutputSkb || flags.OutputShinfo) && useKprobeMulti: bpfSpec, err = LoadKProbeMultiPWRU() - case flags.OutputSkb || flags.OutputShinfo: - bpfSpec, err = LoadKProbePWRU() - case useKprobeMulti: - bpfSpec, err = LoadKProbeMultiPWRUWithoutOutputSKB() default: - bpfSpec, err = LoadKProbePWRUWithoutOutputSKB() + bpfSpec, err = LoadKProbePWRU() } if err != nil { log.Fatalf("Failed to load bpf spec: %v", err)