Skip to content

Commit

Permalink
bpf: Remove unnecessary OUTPUT_SKB macro
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Asphaltt authored and brb committed Nov 19, 2024
1 parent ee69ff9 commit 7e5cb17
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
6 changes: 0 additions & 6 deletions bpf/kprobe_pwru.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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 = {};
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 19 additions & 0 deletions internal/pwru/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 5 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7e5cb17

Please sign in to comment.