Skip to content

Commit 2790223

Browse files
authored
Verify already started protocol handlers (#832)
1 parent 6fc7f27 commit 2790223

File tree

7 files changed

+42
-25
lines changed

7 files changed

+42
-25
lines changed

bpf/common/connection_info.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ enum protocol_type : u8 {
1818
k_protocol_type_unknown = 0,
1919
k_protocol_type_mysql = 1,
2020
k_protocol_type_postgres = 2,
21+
k_protocol_type_http = 3,
2122
};
2223

2324
// Struct to keep information on the connections in flight

bpf/generictracer/k_tracer.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,8 @@ int obi_handle_buf_with_args(void *ctx) {
11291129

11301130
if (is_http(args->small_buf, MIN_HTTP_SIZE, &args->packet_type)) {
11311131
bpf_tail_call(ctx, &jump_table, k_tail_protocol_http);
1132-
} else if (is_http2_or_grpc(args->small_buf, MIN_HTTP2_SIZE)) {
1132+
} else if (is_http2_or_grpc(args->small_buf, MIN_HTTP2_SIZE) &&
1133+
(args->protocol_type != k_protocol_type_http)) {
11331134
bpf_dbg_printk("Found HTTP2 or gRPC connection");
11341135
http2_conn_info_data_t data = {
11351136
.id = 0,

bpf/generictracer/k_tracer_defs.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,23 @@ typedef struct recv_args {
3030
} recv_args_t;
3131

3232
static __always_inline enum protocol_type
33-
protocol_type_for_conn_info(const connection_info_t *info) {
34-
const enum protocol_type *cached_protocol_type = bpf_map_lookup_elem(&protocol_cache, info);
33+
protocol_type_for_conn_info(const pid_connection_info_t *info) {
34+
const enum protocol_type *cached_protocol_type =
35+
bpf_map_lookup_elem(&protocol_cache, &info->conn);
36+
if (!cached_protocol_type) {
37+
if (already_tracked_http(info)) {
38+
return k_protocol_type_http;
39+
}
40+
}
3541
return cached_protocol_type ? *cached_protocol_type : k_protocol_type_unknown;
3642
}
3743

38-
static __always_inline call_protocol_args_t *make_protocol_args(
39-
connection_info_t *info, void *u_buf, int bytes_len, u8 ssl, u8 direction, u16 orig_dport) {
44+
static __always_inline call_protocol_args_t *make_protocol_args(const pid_connection_info_t *info,
45+
void *u_buf,
46+
int bytes_len,
47+
u8 ssl,
48+
u8 direction,
49+
u16 orig_dport) {
4050
call_protocol_args_t *args = protocol_args();
4151
if (!args) {
4252
return 0;
@@ -60,7 +70,7 @@ static __always_inline void handle_buf_with_connection(void *ctx,
6070
u8 direction,
6171
u16 orig_dport) {
6272
call_protocol_args_t *args =
63-
make_protocol_args(&pid_conn->conn, u_buf, bytes_len, ssl, direction, orig_dport);
73+
make_protocol_args(pid_conn, u_buf, bytes_len, ssl, direction, orig_dport);
6474
if (!args) {
6575
return;
6676
}

bpf/generictracer/protocol_http.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ static __always_inline u32 trace_type_from_meta(http_connection_metadata_t *meta
4646
return TRACE_TYPE_SERVER;
4747
}
4848

49+
static __always_inline u8 already_tracked_http(const pid_connection_info_t *p_conn) {
50+
http_info_t *http_info = bpf_map_lookup_elem(&ongoing_http, p_conn);
51+
return (http_info && !(http_info->delayed || http_info->submitted));
52+
}
53+
4954
static __always_inline void
5055
http_get_or_create_trace_info(http_connection_metadata_t *meta,
5156
u32 pid,
@@ -485,6 +490,8 @@ static __always_inline int http_send_large_buffer(http_info_t *req,
485490

486491
req->has_large_buffers = true;
487492

493+
bpf_dbg_printk("sending large buffer, size=%d", bytes_len);
494+
488495
bpf_ringbuf_output(&events, large_buf, total_size & k_large_buf_max_size_mask, get_flags());
489496
return 0;
490497
}

bpf/generictracer/protocol_http2.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ static __always_inline u8 http2_flag_new(u8 flags) {
3939
return flags & http2_conn_flag_new;
4040
}
4141

42+
static __always_inline u8 already_tracked_http2(const pid_connection_info_t *p_conn) {
43+
http2_conn_info_data_t *http2_info = bpf_map_lookup_elem(&ongoing_http2_connections, p_conn);
44+
return http2_info != 0;
45+
}
46+
4247
static __always_inline http2_grpc_request_t *empty_http2_info() {
4348
int zero = 0;
4449
http2_grpc_request_t *value = bpf_map_lookup_elem(&http2_info_mem, &zero);

bpf/generictracer/protocol_tcp.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ static __always_inline void init_new_trace(tp_info_t *tp) {
4343
#endif
4444
}
4545

46+
static __always_inline u8 already_tracked_tcp(const pid_connection_info_t *p_conn) {
47+
tcp_req_t *tcp_info = bpf_map_lookup_elem(&ongoing_tcp_req, p_conn);
48+
return tcp_info != 0;
49+
}
50+
4651
static __always_inline void set_tcp_trace_info(
4752
u32 type, connection_info_t *conn, tp_info_t *tp, u32 pid, u8 ssl, u16 orig_dport) {
4853
tp_info_pid_t *tp_p = tp_buf();
@@ -131,6 +136,8 @@ static __always_inline int tcp_send_large_buffer(tcp_req_t *req,
131136
ret = postgres_send_large_buffer(req, u_buf, bytes_len, packet_type, direction, action);
132137
}
133138
break;
139+
case k_protocol_type_http:
140+
break;
134141
case k_protocol_type_unknown:
135142
break;
136143
}

bpf/tpinjector/tpinjector.c

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
#include <common/trace_util.h>
1717
#include <common/tracing.h>
1818

19-
#include <generictracer/types/http2_conn_info_data.h>
20-
#include <generictracer/maps/ongoing_tcp_req.h>
21-
#include <generictracer/maps/ongoing_http2_connections.h>
19+
#include <generictracer/protocol_http.h>
20+
#include <generictracer/protocol_tcp.h>
21+
#include <generictracer/protocol_http2.h>
2222

2323
#include <logger/bpf_dbg.h>
2424

@@ -171,22 +171,8 @@ static __always_inline u8 is_tracked_go_request(const tp_info_pid_t *tp) {
171171
}
172172

173173
static __always_inline u8 already_tracked(const pid_connection_info_t *p_conn) {
174-
http_info_t *http_info = bpf_map_lookup_elem(&ongoing_http, p_conn);
175-
if (http_info && !(http_info->delayed || http_info->submitted)) {
176-
return 1;
177-
}
178-
179-
tcp_req_t *tcp_info = bpf_map_lookup_elem(&ongoing_tcp_req, p_conn);
180-
if (tcp_info) {
181-
return 1;
182-
}
183-
184-
http2_conn_info_data_t *http2_info = bpf_map_lookup_elem(&ongoing_http2_connections, p_conn);
185-
if (http2_info) {
186-
return 1;
187-
}
188-
189-
return 0;
174+
return already_tracked_http(p_conn) || already_tracked_tcp(p_conn) ||
175+
already_tracked_http2(p_conn);
190176
}
191177

192178
// This code is copied from the kprobe on tcp_sendmsg and it's called from

0 commit comments

Comments
 (0)