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 <hffilwlqm@gmail.com>
  • Loading branch information
Asphaltt committed Nov 12, 2024
1 parent 1b59513 commit 690ce28
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 17 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@ pwru
release
tags
kprobepwru_*
kprobepwruwithoutoutputskb_*
kprobemultipwru_*
kprobemultipwruwithoutoutputskb_*
!pwru/
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
6 changes: 1 addition & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,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 690ce28

Please sign in to comment.