Skip to content

Commit

Permalink
Add support for tracking http2 client and server requests (#568)
Browse files Browse the repository at this point in the history
  • Loading branch information
grcevski authored Jan 24, 2024
1 parent d4d8dab commit 9553916
Show file tree
Hide file tree
Showing 47 changed files with 741 additions and 104 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,5 @@ test/integration/components/rubytestserver/testapi/tmp/storage/*
test/integration/components/dotnetserver/obj/*
test/oats/build/*
test/cmd/grpc/client/grpclient
test/integration/components/gohttp2/client/client
test/integration/components/gohttp2/server/http2srv
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ prereqs:
mkdir -p $(TEST_OUTPUT)/run
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint@v1.54.2)
$(call go-install-tool,$(BPF2GO),github.com/cilium/ebpf/cmd/bpf2go@v0.10.0)
$(call go-install-tool,$(GO_OFFSETS_TRACKER),github.com/grafana/go-offsets-tracker/cmd/go-offsets-tracker@v0.1.6)
$(call go-install-tool,$(GO_OFFSETS_TRACKER),github.com/grafana/go-offsets-tracker/cmd/go-offsets-tracker@v0.1.7)
$(call go-install-tool,$(GOIMPORTS_REVISER),github.com/incu6us/goimports-reviser/v3@v3.4.5)
$(call go-install-tool,$(GO_LICENSES),github.com/google/go-licenses@v1.6.0)
$(call go-install-tool,$(KIND),sigs.k8s.io/kind@v0.20.0)
Expand Down
22 changes: 17 additions & 5 deletions bpf/go_nethttp.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ int uprobe_readRequestReturns(struct pt_regs *ctx) {
return 0;
}

SEC("uprobe/WriteHeader")
int uprobe_WriteHeader(struct pt_regs *ctx) {
bpf_dbg_printk("=== uprobe/WriteHeader === ");
static __always_inline int writeHeaderHelper(struct pt_regs *ctx, u64 req_offset) {
bpf_dbg_printk("=== uprobe/WriteHeader === ");
void *goroutine_addr = GOROUTINE_PTR(ctx);
bpf_dbg_printk("goroutine_addr %lx", goroutine_addr);

Expand Down Expand Up @@ -144,7 +143,7 @@ int uprobe_WriteHeader(struct pt_regs *ctx) {

// Get request struct
void *req_ptr = 0;
bpf_probe_read(&req_ptr, sizeof(req_ptr), (void *)(resp_ptr + resp_req_pos));
bpf_probe_read(&req_ptr, sizeof(req_ptr), (void *)(resp_ptr + req_offset));

if (!req_ptr) {
bpf_printk("can't find req inside the response value");
Expand Down Expand Up @@ -195,6 +194,11 @@ int uprobe_WriteHeader(struct pt_regs *ctx) {
return 0;
}

SEC("uprobe/WriteHeader")
int uprobe_WriteHeader(struct pt_regs *ctx) {
return writeHeaderHelper(ctx, resp_req_pos);
}

#ifndef NO_HEADER_PROPAGATION
struct {
__uint(type, BPF_MAP_TYPE_HASH);
Expand Down Expand Up @@ -378,4 +382,12 @@ SEC("uprobe/header_writeSubset")
int uprobe_writeSubset(struct pt_regs *ctx) {
return 0;
}
#endif
#endif

// HTTP 2.0 server support
SEC("uprobe/http2ResponseWriterStateWriteHeader")
int uprobe_http2ResponseWriterStateWriteHeader(struct pt_regs *ctx) {
bpf_dbg_printk("=== uprobe/proc http2 responseWriterState writeHeader === ");

return writeHeaderHelper(ctx, rws_req_pos);
}
1 change: 1 addition & 0 deletions bpf/go_nethttp.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ volatile const u64 resp_req_pos;
volatile const u64 req_header_ptr_pos;
volatile const u64 io_writer_buf_ptr_pos;
volatile const u64 io_writer_n_pos;
volatile const u64 rws_req_pos;

#endif
2 changes: 1 addition & 1 deletion bpf/trace_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ static __always_inline void get_or_create_trace_info(http_connection_metadata_t
#ifdef BPF_TRACEPARENT
// The below buffer scan can be expensive on high volume of requests. We make it optional
// for customers to enable it. Off by default.
if (!capture_header_buffer || !bpf_core_enum_value_exists(enum bpf_func_id, BPF_FUNC_loop)) {
if (!capture_header_buffer) {
bpf_map_update_elem(&trace_map, conn, tp_p, BPF_ANY);
server_or_client_trace(meta, conn, tp_p);
return;
Expand Down
32 changes: 32 additions & 0 deletions configs/offsets/http2/inspect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"fmt"
"net/http"
"os"

"golang.org/x/net/http2"
)

func checkErr(err error, msg string) {
if err == nil {
return
}
fmt.Printf("ERROR: %s: %s\n", msg, err)
os.Exit(1)
}

func main() {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %v, http: %v\n", r.URL.Path, r.TLS == nil)
})

server := &http.Server{
Addr: "0.0.0.0:8080",
Handler: handler,
}
http2.ConfigureServer(server, nil)

fmt.Printf("Listening [0.0.0.0:8080]...\n")
checkErr(server.ListenAndServeTLS("cert.pem", "key.pem"), "while listening")
}
14 changes: 13 additions & 1 deletion configs/offsets/tracker_input.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,21 @@
],
"vendor/golang.org/x/net/http2/hpack.Encoder": [
"w"
]
]
}
},
"golang.org/x/net": {
"inspect": "./configs/offsets/http2/inspect.go",
"versions": ">= 0.12.0",
"fields": {
"golang.org/x/net/http2.responseWriterState": [
"req"
],
"golang.org/x/net/http2/hpack.Encoder": [
"w"
]
}
},
"google.golang.org/grpc": {
"versions": ">= 1.40",
"fields": {
Expand Down
Binary file modified pkg/internal/ebpf/httpfltr/bpf_tp_bpfel_arm64.o
Binary file not shown.
Binary file modified pkg/internal/ebpf/httpfltr/bpf_tp_bpfel_x86.o
Binary file not shown.
Binary file modified pkg/internal/ebpf/httpfltr/bpf_tp_debug_bpfel_arm64.o
Binary file not shown.
Binary file modified pkg/internal/ebpf/httpfltr/bpf_tp_debug_bpfel_x86.o
Binary file not shown.
Binary file modified pkg/internal/ebpf/httpssl/bpf_tp_bpfel_arm64.o
Binary file not shown.
Binary file modified pkg/internal/ebpf/httpssl/bpf_tp_bpfel_x86.o
Binary file not shown.
Binary file modified pkg/internal/ebpf/httpssl/bpf_tp_debug_bpfel_arm64.o
Binary file not shown.
Binary file modified pkg/internal/ebpf/httpssl/bpf_tp_debug_bpfel_x86.o
Binary file not shown.
27 changes: 15 additions & 12 deletions pkg/internal/ebpf/nethttp/bpf_bpfel_arm64.go

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

Binary file modified pkg/internal/ebpf/nethttp/bpf_bpfel_arm64.o
Binary file not shown.
27 changes: 15 additions & 12 deletions pkg/internal/ebpf/nethttp/bpf_bpfel_x86.go

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

Binary file modified pkg/internal/ebpf/nethttp/bpf_bpfel_x86.o
Binary file not shown.
27 changes: 15 additions & 12 deletions pkg/internal/ebpf/nethttp/bpf_debug_bpfel_arm64.go

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

Binary file modified pkg/internal/ebpf/nethttp/bpf_debug_bpfel_arm64.o
Binary file not shown.
27 changes: 15 additions & 12 deletions pkg/internal/ebpf/nethttp/bpf_debug_bpfel_x86.go

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

Binary file modified pkg/internal/ebpf/nethttp/bpf_debug_bpfel_x86.o
Binary file not shown.
27 changes: 15 additions & 12 deletions pkg/internal/ebpf/nethttp/bpf_tp_bpfel_arm64.go

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

Binary file modified pkg/internal/ebpf/nethttp/bpf_tp_bpfel_arm64.o
Binary file not shown.
Loading

0 comments on commit 9553916

Please sign in to comment.