From 466d540d22df7bf2e929615c5fbcdcf690ec6b0a Mon Sep 17 00:00:00 2001 From: thiagoftsm Date: Mon, 21 Mar 2022 17:55:29 +0000 Subject: [PATCH] Socket Update (#17) --- kernel-collector | 2 +- src/socket.bpf.c | 80 ++++++++++++++++++++++++++++++------------------ src/socket.c | 28 ++++++++--------- 3 files changed, 65 insertions(+), 45 deletions(-) diff --git a/kernel-collector b/kernel-collector index a2bed9b..cee6ea6 160000 --- a/kernel-collector +++ b/kernel-collector @@ -1 +1 @@ -Subproject commit a2bed9b392ffcf73bb0669b5d2290edf3668d178 +Subproject commit cee6ea64364c920ff686da64a256c2cd7c8fc03f diff --git a/src/socket.bpf.c b/src/socket.bpf.c index 1a3ef0a..938c2b2 100644 --- a/src/socket.bpf.c +++ b/src/socket.bpf.c @@ -444,22 +444,6 @@ static inline int netdata_common_tcp_close(struct inet_sock *is) return 0; } -static inline int netdata_common_tcp_drop(struct sk_buff *skb) -{ - __u16 protocol; - struct sock *sk = BPF_CORE_READ(skb, sk); - BPF_CORE_READ_INTO(&protocol, sk, sk_protocol); - - if (protocol != IPPROTO_TCP) - return 0; - - libnetdata_update_global(&tbl_global_sock, NETDATA_KEY_TCP_DROP, 1); - - update_pid_cleanup(1, 0); - - return 0; -} - static inline int netdata_common_udp_recvmsg(struct sock *sk) { __u64 pid_tgid = bpf_get_current_pid_tgid(); @@ -485,6 +469,32 @@ static inline int netdata_common_tcp_connect(int ret, enum socket_counters succe return 0; } +/*********************************************************************************** + * + * CLEANUP COMMON + * + ***********************************************************************************/ + +static inline int netdata_common_socket_cleanup() +{ + __u32 key = NETDATA_CONTROLLER_APPS_ENABLED; + __u32 *apps = bpf_map_lookup_elem(&socket_ctrl ,&key); + if (apps) { + if (*apps == 0) + return 0; + } else + return 0; + + __u64 pid_tgid = bpf_get_current_pid_tgid(); + key = (__u32)(pid_tgid >> 32); + netdata_bandwidth_t *removeme = (netdata_bandwidth_t *) bpf_map_lookup_elem(&tbl_bandwidth, &key); + if (removeme) { + bpf_map_delete_elem(&tbl_bandwidth, &key); + } + + return 0; +} + /*********************************************************************************** * * SOCKET SECTION(kprobe) @@ -544,14 +554,6 @@ int BPF_KPROBE(netdata_tcp_close_kprobe) return netdata_common_tcp_close(is); } -SEC("kprobe/__kfree_skb") -int BPF_KPROBE(netdata_tcp_drop_kprobe) -{ - struct sk_buff *skb = (struct sk_buff *)PT_REGS_PARM1(ctx); - - return netdata_common_tcp_drop(skb); -} - // https://elixir.bootlin.com/linux/v5.6.14/source/net/ipv4/udp.c#L1726 SEC("kprobe/udp_recvmsg") int BPF_KPROBE(netdata_udp_recvmsg_kprobe) @@ -626,6 +628,18 @@ int BPF_KPROBE(netdata_udp_sendmsg_kprobe) return common_udp_send_message(is, sent, 0); } +/*********************************************************************************** + * + * CLEANUP SECTION(kprobe) + * + ***********************************************************************************/ + +SEC("kprobe/release_task") +int BPF_KPROBE(netdata_socket_release_task_kprobe) +{ + return netdata_common_socket_cleanup(); +} + /*********************************************************************************** * * SOCKET SECTION(tracepoint) @@ -678,12 +692,6 @@ int BPF_PROG(netdata_tcp_close_fentry, struct sock *sk) return netdata_common_tcp_close(is); } -SEC("fentry/__kfree_skb") -int BPF_PROG(netdata_tcp_drop_fentry, struct sk_buff *skb) -{ - return netdata_common_tcp_drop(skb); -} - // https://elixir.bootlin.com/linux/v5.6.14/source/net/ipv4/udp.c#L1726 SEC("fentry/udp_recvmsg") int BPF_PROG(netdata_udp_recvmsg_fentry, struct sock *sk) @@ -726,5 +734,17 @@ int BPF_PROG(netdata_udp_sendmsg_fexit, struct sock *sk, struct msghdr *msg, siz return common_udp_send_message(is, sent, ret); } +/*********************************************************************************** + * + * CLEANUP SECTION(kprobe) + * + ***********************************************************************************/ + +SEC("fentry/release_task") +int BPF_KPROBE(netdata_socket_release_task_fentry) +{ + return netdata_common_socket_cleanup(); +} + char _license[] SEC("license") = "GPL"; diff --git a/src/socket.c b/src/socket.c index c57bfcf..17c3dab 100644 --- a/src/socket.c +++ b/src/socket.c @@ -16,6 +16,7 @@ #include "socket.skel.h" +// Socket functions char *function_list[] = { "inet_csk_accept", "tcp_retransmit_skb", "tcp_cleanup_rbuf", @@ -23,10 +24,11 @@ char *function_list[] = { "inet_csk_accept", "udp_recvmsg", "tcp_sendmsg", "udp_sendmsg", - "__kfree_skb", "tcp_v4_connect", "tcp_v6_connect"}; +#define NETDATA_CLEANUP_FUNCTIONS "release_task" + #define NETDATA_IPV4 4 #define NETDATA_IPV6 6 @@ -68,12 +70,6 @@ static int ebpf_attach_probes(struct socket_bpf *obj) if (ret) return -1; - obj->links.netdata_tcp_drop_kprobe = bpf_program__attach_kprobe(obj->progs.netdata_tcp_drop_kprobe, - false, function_list[NETDATA_FCNT_TCP_DROP]); - ret = libbpf_get_error(obj->links.netdata_tcp_drop_kprobe); - if (ret) - return -1; - obj->links.netdata_udp_recvmsg_kprobe = bpf_program__attach_kprobe(obj->progs.netdata_udp_recvmsg_kprobe, false, function_list[NETDATA_FCNT_UDP_RECEVMSG]); ret = libbpf_get_error(obj->links.netdata_udp_recvmsg_kprobe); @@ -110,6 +106,12 @@ static int ebpf_attach_probes(struct socket_bpf *obj) if (ret) return -1; + obj->links.netdata_socket_release_task_kprobe = bpf_program__attach_kprobe(obj->progs.netdata_socket_release_task_kprobe, + false, NETDATA_CLEANUP_FUNCTIONS); + ret = libbpf_get_error(obj->links.netdata_socket_release_task_kprobe); + if (ret) + return -1; + return 0; } @@ -119,16 +121,15 @@ static void ebpf_disable_probes(struct socket_bpf *obj) bpf_program__set_autoload(obj->progs.netdata_tcp_v4_connect_kretprobe, false); bpf_program__set_autoload(obj->progs.netdata_tcp_v6_connect_kretprobe, false); bpf_program__set_autoload(obj->progs.netdata_tcp_retransmit_skb_kprobe, false); - bpf_program__set_autoload(obj->progs.netdata_tcp_retransmit_skb_kprobe, false); bpf_program__set_autoload(obj->progs.netdata_tcp_cleanup_rbuf_kprobe, false); bpf_program__set_autoload(obj->progs.netdata_tcp_close_kprobe, false); - bpf_program__set_autoload(obj->progs.netdata_tcp_drop_kprobe, false); bpf_program__set_autoload(obj->progs.netdata_udp_recvmsg_kprobe, false); bpf_program__set_autoload(obj->progs.netdata_udp_recvmsg_kretprobe, false); bpf_program__set_autoload(obj->progs.netdata_tcp_sendmsg_kretprobe, false); bpf_program__set_autoload(obj->progs.netdata_tcp_sendmsg_kprobe, false); bpf_program__set_autoload(obj->progs.netdata_udp_sendmsg_kretprobe, false); bpf_program__set_autoload(obj->progs.netdata_udp_sendmsg_kprobe, false); + bpf_program__set_autoload(obj->progs.netdata_socket_release_task_kprobe, false); } static void ebpf_disable_trampoline(struct socket_bpf *obj) @@ -139,12 +140,12 @@ static void ebpf_disable_trampoline(struct socket_bpf *obj) bpf_program__set_autoload(obj->progs.netdata_tcp_retransmit_skb_fentry, false); bpf_program__set_autoload(obj->progs.netdata_tcp_cleanup_rbuf_fentry, false); bpf_program__set_autoload(obj->progs.netdata_tcp_close_fentry, false); - bpf_program__set_autoload(obj->progs.netdata_tcp_drop_fentry, false); bpf_program__set_autoload(obj->progs.netdata_udp_recvmsg_fentry, false); bpf_program__set_autoload(obj->progs.netdata_tcp_sendmsg_fentry, false); bpf_program__set_autoload(obj->progs.netdata_tcp_sendmsg_fexit, false); bpf_program__set_autoload(obj->progs.netdata_udp_sendmsg_fentry, false); bpf_program__set_autoload(obj->progs.netdata_udp_sendmsg_fexit, false); + bpf_program__set_autoload(obj->progs.netdata_socket_release_task_fentry, false); } static void ebpf_set_trampoline_target(struct socket_bpf *obj) @@ -167,9 +168,6 @@ static void ebpf_set_trampoline_target(struct socket_bpf *obj) bpf_program__set_attach_target(obj->progs.netdata_tcp_close_fentry, 0, function_list[NETDATA_FCNT_TCP_CLOSE]); - bpf_program__set_attach_target(obj->progs.netdata_tcp_drop_fentry, 0, - function_list[NETDATA_FCNT_TCP_DROP]); - bpf_program__set_attach_target(obj->progs.netdata_udp_recvmsg_fentry, 0, function_list[NETDATA_FCNT_UDP_RECEVMSG]); @@ -184,8 +182,10 @@ static void ebpf_set_trampoline_target(struct socket_bpf *obj) bpf_program__set_attach_target(obj->progs.netdata_udp_sendmsg_fexit, 0, function_list[NETDATA_FCNT_UDP_SENDMSG]); -} + bpf_program__set_attach_target(obj->progs.netdata_socket_release_task_fentry, 0, + NETDATA_CLEANUP_FUNCTIONS); +} static inline int ebpf_load_and_attach(struct socket_bpf *obj, int selector) {