Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use kprobe for unreliable recvmsg return probe #1095

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 29 additions & 15 deletions bpf/http_sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,24 +434,12 @@ int BPF_KPROBE(kprobe_tcp_recvmsg, struct sock *sk, struct msghdr *msg, size_t l
return 0;
}

SEC("kretprobe/tcp_recvmsg")
int BPF_KRETPROBE(kretprobe_tcp_recvmsg, int copied_len) {
u64 id = bpf_get_current_pid_tgid();

if (!valid_pid(id)) {
return 0;
}

static __always_inline int return_recvmsg(void *ctx, u64 id, int copied_len) {
recv_args_t *args = bpf_map_lookup_elem(&active_recv_args, &id);

bpf_dbg_printk("=== tcp_recvmsg ret id=%d args=%llx copied_len %d ===", id, args, copied_len);
bpf_dbg_printk("=== return recvmsg id=%d args=%llx copied_len %d ===", id, args, copied_len);

if (!args) {
goto done;
}

if (copied_len <= 0) {
bpf_map_delete_elem(&active_recv_args, &id);
if (!args || (copied_len <= 0)) {
goto done;
}

Expand Down Expand Up @@ -502,6 +490,32 @@ int BPF_KRETPROBE(kretprobe_tcp_recvmsg, int copied_len) {
return 0;
}

SEC("kprobe/tcp_cleanup_rbuf")
int BPF_KPROBE(kprobe_tcp_cleanup_rbuf, struct sock *sk, int copied) {
u64 id = bpf_get_current_pid_tgid();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a nitpick:

Suggested change
u64 id = bpf_get_current_pid_tgid();
const u64 id = bpf_get_current_pid_tgid();

If I could make a recommendation, I'd suggest we start using const by default on our codebase moving forward, and dropping it only when required. There's a rationale about const-correctness here - it's mostly C++ oriented but it also applies to C (to an extent, in particular to preventing logic errors, improving clarity and optimisations, although I don't know how the latter goes for eBPF). Another interesting example is Rust. It makes everything const by default (and provides the mut keyword to explicitly tag variables that are not const)


if (!valid_pid(id)) {
return 0;
}

bpf_dbg_printk("=== tcp_cleanup_rbuf id=%d copied_len %d ===", id, copied);

return return_recvmsg(ctx, id, copied);
}

SEC("kretprobe/tcp_recvmsg")
int BPF_KRETPROBE(kretprobe_tcp_recvmsg, int copied_len) {
u64 id = bpf_get_current_pid_tgid();

if (!valid_pid(id)) {
return 0;
}

bpf_dbg_printk("=== kretprobe_tcp_recvmsg id=%d copied_len %d ===", id, copied_len);

return return_recvmsg(ctx, id, copied_len);
}

// Fall-back in case we don't see kretprobe on tcp_recvmsg in high network volume situations
SEC("socket/http_filter")
int socket__http_filter(struct __sk_buff *skb) {
Expand Down
3 changes: 3 additions & 0 deletions pkg/internal/ebpf/httpfltr/bpf_arm64_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified pkg/internal/ebpf/httpfltr/bpf_arm64_bpfel.o
Binary file not shown.
3 changes: 3 additions & 0 deletions pkg/internal/ebpf/httpfltr/bpf_debug_arm64_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified pkg/internal/ebpf/httpfltr/bpf_debug_arm64_bpfel.o
Binary file not shown.
3 changes: 3 additions & 0 deletions pkg/internal/ebpf/httpfltr/bpf_debug_x86_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified pkg/internal/ebpf/httpfltr/bpf_debug_x86_bpfel.o
Binary file not shown.
3 changes: 3 additions & 0 deletions pkg/internal/ebpf/httpfltr/bpf_tp_arm64_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified pkg/internal/ebpf/httpfltr/bpf_tp_arm64_bpfel.o
Binary file not shown.
3 changes: 3 additions & 0 deletions pkg/internal/ebpf/httpfltr/bpf_tp_debug_arm64_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified pkg/internal/ebpf/httpfltr/bpf_tp_debug_arm64_bpfel.o
Binary file not shown.
3 changes: 3 additions & 0 deletions pkg/internal/ebpf/httpfltr/bpf_tp_debug_x86_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified pkg/internal/ebpf/httpfltr/bpf_tp_debug_x86_bpfel.o
Binary file not shown.
3 changes: 3 additions & 0 deletions pkg/internal/ebpf/httpfltr/bpf_tp_x86_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified pkg/internal/ebpf/httpfltr/bpf_tp_x86_bpfel.o
Binary file not shown.
3 changes: 3 additions & 0 deletions pkg/internal/ebpf/httpfltr/bpf_x86_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified pkg/internal/ebpf/httpfltr/bpf_x86_bpfel.o
Binary file not shown.
3 changes: 3 additions & 0 deletions pkg/internal/ebpf/httpfltr/httpfltr.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ func (p *Tracer) KProbes() map[string]ebpfcommon.FunctionPrograms {
Start: p.bpfObjects.KprobeTcpRecvmsg,
End: p.bpfObjects.KretprobeTcpRecvmsg,
},
"tcp_cleanup_rbuf": {
Start: p.bpfObjects.KprobeTcpCleanupRbuf, // this kprobe runs the same code as recvmsg return, we use it because kretprobes can be unreliable.
},
"sys_clone": {
Required: true,
End: p.bpfObjects.KretprobeSysClone,
Expand Down
1 change: 1 addition & 0 deletions test/oats/kafka/docker-compose-beyla-java-kafka.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ services:
BEYLA_METRICS_INTERVAL: "10ms"
BEYLA_BPF_BATCH_TIMEOUT: "10ms"
BEYLA_LOG_LEVEL: "DEBUG"
BEYLA_BPF_DEBUG: "true"
OTEL_EXPORTER_OTLP_ENDPOINT: "http://collector:4318"
depends_on:
testserver:
Expand Down
1 change: 1 addition & 0 deletions test/oats/kafka/docker-compose-beyla-python-kafka.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ services:
BEYLA_METRICS_INTERVAL: "10ms"
BEYLA_BPF_BATCH_TIMEOUT: "10ms"
BEYLA_LOG_LEVEL: "DEBUG"
BEYLA_BPF_DEBUG: "true"
OTEL_EXPORTER_OTLP_ENDPOINT: "http://collector:4318"
depends_on:
testserver:
Expand Down
1 change: 1 addition & 0 deletions test/oats/redis/docker-compose-beyla-go-redis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ services:
BEYLA_METRICS_INTERVAL: "10ms"
BEYLA_BPF_BATCH_TIMEOUT: "10ms"
BEYLA_LOG_LEVEL: "DEBUG"
BEYLA_BPF_DEBUG: "true"
OTEL_EXPORTER_OTLP_ENDPOINT: "http://collector:4318"
depends_on:
testserver:
Expand Down
1 change: 1 addition & 0 deletions test/oats/redis/docker-compose-beyla-redis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ services:
BEYLA_METRICS_INTERVAL: "10ms"
BEYLA_BPF_BATCH_TIMEOUT: "10ms"
BEYLA_LOG_LEVEL: "DEBUG"
BEYLA_BPF_DEBUG: "true"
OTEL_EXPORTER_OTLP_ENDPOINT: "http://collector:4318"
depends_on:
testserver:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ services:
BEYLA_METRICS_INTERVAL: "10ms"
BEYLA_BPF_BATCH_TIMEOUT: "10ms"
BEYLA_LOG_LEVEL: "DEBUG"
BEYLA_BPF_DEBUG: "true"
OTEL_EXPORTER_OTLP_ENDPOINT: "http://collector:4318"
depends_on:
testserver:
Expand Down
1 change: 1 addition & 0 deletions test/oats/sql/docker-compose-beyla-gosqlclient.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ services:
BEYLA_METRICS_INTERVAL: "10ms"
BEYLA_BPF_BATCH_TIMEOUT: "10ms"
BEYLA_LOG_LEVEL: "DEBUG"
BEYLA_BPF_DEBUG: "true"
OTEL_EXPORTER_OTLP_ENDPOINT: "http://collector:4318"
depends_on:
testserver:
Expand Down
1 change: 1 addition & 0 deletions test/oats/sql/docker-compose-beyla-sql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ services:
BEYLA_METRICS_INTERVAL: "10ms"
BEYLA_BPF_BATCH_TIMEOUT: "10ms"
BEYLA_LOG_LEVEL: "DEBUG"
BEYLA_BPF_DEBUG: "true"
OTEL_EXPORTER_OTLP_ENDPOINT: "http://collector:4318"
depends_on:
testserver:
Expand Down
Loading