Skip to content

Commit 0a5e19e

Browse files
authored
bpf: tpinjector: drop http split packets only after storing the buffer (#821)
1 parent 25a3fda commit 0a5e19e

File tree

1 file changed

+33
-39
lines changed

1 file changed

+33
-39
lines changed

bpf/tpinjector/tpinjector.c

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,25 @@ static __always_inline u8 is_tracked_go_request(const tp_info_pid_t *tp) {
170170
return tp != NULL && tp->valid;
171171
}
172172

173+
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;
190+
}
191+
173192
// This code is copied from the kprobe on tcp_sendmsg and it's called from
174193
// the sock_msg program, which does the packet extension for injecting the
175194
// Traceparent. Since the sock_msg runs before the kprobe on tcp_sendmsg, we
@@ -184,14 +203,14 @@ static __always_inline u8 protocol_detector(struct sk_msg_md *msg,
184203
const egress_key_t *e_key) {
185204
bpf_dbg_printk("=== [protocol detector] %d size %d===", id, msg->size);
186205

187-
send_args_t s_args = {.size = msg->size};
188-
__builtin_memcpy(&s_args.p_conn.conn, conn, sizeof(connection_info_t));
206+
pid_connection_info_t p_conn = {};
207+
__builtin_memcpy(&p_conn.conn, conn, sizeof(connection_info_t));
189208

190-
dbg_print_http_connection_info(&s_args.p_conn.conn);
191-
sort_connection_info(&s_args.p_conn.conn);
192-
s_args.p_conn.pid = pid_from_pid_tgid(id);
209+
dbg_print_http_connection_info(&p_conn.conn);
210+
sort_connection_info(&p_conn.conn);
211+
p_conn.pid = pid_from_pid_tgid(id);
193212

194-
if (s_args.size == 0 || is_ssl_connection(&s_args.p_conn)) {
213+
if (msg->size == 0 || is_ssl_connection(&p_conn)) {
195214
return 0;
196215
}
197216

@@ -223,6 +242,14 @@ static __always_inline u8 protocol_detector(struct sk_msg_md *msg,
223242
return 0;
224243
}
225244

245+
// We should check if we have already seen this request and we've
246+
// started tracking it. We only want to extend the first packet that
247+
// looks like HTTP, not something that's passing HTTP in the body.
248+
if (already_tracked(&p_conn)) {
249+
bpf_dbg_printk("already extended before, ignoring this packet...");
250+
return 0;
251+
}
252+
226253
if (is_http_request_buf((const unsigned char *)msg_ptr)) {
227254
bpf_dbg_printk("Setting up request to be extended");
228255

@@ -440,30 +467,6 @@ static __always_inline bool handle_go_request(struct sk_msg_md *msg,
440467
return true;
441468
}
442469

443-
static __always_inline u8 already_tracked(const connection_info_t *conn, u64 pid_tid) {
444-
pid_connection_info_t p_conn = {0};
445-
p_conn.conn = *conn;
446-
u32 host_pid = pid_from_pid_tgid(pid_tid);
447-
p_conn.pid = host_pid;
448-
449-
http_info_t *http_info = bpf_map_lookup_elem(&ongoing_http, &p_conn);
450-
if (http_info && !(http_info->delayed || http_info->submitted)) {
451-
return 1;
452-
}
453-
454-
tcp_req_t *tcp_info = bpf_map_lookup_elem(&ongoing_tcp_req, &p_conn);
455-
if (tcp_info) {
456-
return 1;
457-
}
458-
459-
http2_conn_info_data_t *http2_info = bpf_map_lookup_elem(&ongoing_http2_connections, &p_conn);
460-
if (http2_info) {
461-
return 1;
462-
}
463-
464-
return 0;
465-
}
466-
467470
// Sock_msg program which detects packets where it should add space for
468471
// the 'Traceparent' string. It extends the HTTP header and writes the
469472
// Traceparent string.
@@ -486,15 +489,6 @@ int obi_packet_extender(struct sk_msg_md *msg) {
486489
return SK_PASS;
487490
}
488491

489-
// We should first check if we have already seen this request and we've
490-
// started tracking it. We only want to extend the first packet that
491-
// looks like HTTP, not something that's passing HTTP in the body.
492-
493-
if (already_tracked(&conn, id)) {
494-
bpf_dbg_printk("already extended before, ignoring this packet...");
495-
return SK_PASS;
496-
}
497-
498492
bpf_dbg_printk("MSG %llx:%d ->", conn.s_ip[3], conn.s_port);
499493
bpf_dbg_printk("MSG TO %llx:%d", conn.d_ip[3], conn.d_port);
500494
bpf_dbg_printk("MSG SIZE: %u", msg->size);

0 commit comments

Comments
 (0)