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 10, 2024
1 parent fabb29e commit 3a05678
Show file tree
Hide file tree
Showing 3 changed files with 3 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 @@ -162,7 +162,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 @@ -195,7 +194,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 @@ -342,7 +340,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 @@ -357,12 +354,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 @@ -391,7 +386,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 3a05678

Please sign in to comment.