From 45255b9698e8bf1054e9824a18d39859cb3d3356 Mon Sep 17 00:00:00 2001 From: pawsten Date: Sun, 12 Nov 2023 12:21:11 +0000 Subject: [PATCH 1/2] optimization --- bpf_program/metrics_utilities.h | 36 ++++----------------------------- 1 file changed, 4 insertions(+), 32 deletions(-) diff --git a/bpf_program/metrics_utilities.h b/bpf_program/metrics_utilities.h index 11ade44..2d4db71 100644 --- a/bpf_program/metrics_utilities.h +++ b/bpf_program/metrics_utilities.h @@ -8,16 +8,7 @@ __attribute__((always_inline)) static void update_stats(void *tuple, enum protocol proto, uint64_t sent, uint64_t received) { - void *map = NULL; - if (proto == IPV4) { - map = &stats_ipv4; - } - else if (proto == IPV6) { - map = &stats_ipv6; - } - else { - return; - } + void *map = (proto == IPV4) ? &stats_ipv4 : &stats_ipv6; struct stats_t empty = { 0 }; bpf_map_update_elem(map, tuple, &empty, BPF_NOEXIST); @@ -37,17 +28,7 @@ static void update_stats(void *tuple, enum protocol proto, uint64_t sent, uint64 __attribute__((always_inline)) static void update_tcp_stats(void *tuple, enum protocol proto, struct guess_status_t *status, struct sock *sk) { - void *map = NULL; - if (proto == IPV4) { - map = &tcp_stats_ipv4; - } - else if (proto == IPV6) { - map = &tcp_stats_ipv6; - } - else { - return; - } - + void *map = (proto == IPV4) ? &tcp_stats_ipv4 : &tcp_stats_ipv6; struct tcp_stats_t empty = { 0 }; bpf_map_update_elem(map, tuple, &empty, BPF_NOEXIST); struct tcp_stats_t* stats = bpf_map_lookup_elem(map, tuple); @@ -70,18 +51,9 @@ static void update_tcp_stats(void *tuple, enum protocol proto, struct guess_stat __attribute__((always_inline)) static void maybe_fix_missing_connection_tuple(enum protocol proto, void* tuple) { - void *map = NULL; - if (proto == IPV4) { - map = &tuplepid_ipv4; - } - else if (proto == IPV6) { - map = &tuplepid_ipv6; - } - else { - return; - } - + void *map = (proto == IPV4) ? &tuplepid_ipv4 : &tuplepid_ipv6; uint64_t pid = bpf_get_current_pid_tgid(); + if ((pid >> 32) <= 10) { // probe activated with insufficient context return; } From 0ae76d3819550a029689dcda5bc98f3b6c9ce519 Mon Sep 17 00:00:00 2001 From: Pawel Stenka Date: Mon, 13 Nov 2023 10:37:54 +0100 Subject: [PATCH 2/2] Cleanup --- bpf_program/tuples_utilities.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bpf_program/tuples_utilities.h b/bpf_program/tuples_utilities.h index a831522..90253df 100644 --- a/bpf_program/tuples_utilities.h +++ b/bpf_program/tuples_utilities.h @@ -91,7 +91,7 @@ static int read_ipv4_tuple(struct ipv4_tuple_t *tuple, struct guess_status_t *st tuple->saddr = saddr; tuple->daddr = daddr; - tuple->sport = sport; // TODO pjuszczyk Check if sport doesn't need to be flipped in some cases + tuple->sport = sport; tuple->dport = ntohs(dport); tuple->netns = net_ns_inum;