Skip to content

Commit

Permalink
Socket Update (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagoftsm authored Mar 21, 2022
1 parent 88c5296 commit 466d540
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 45 deletions.
2 changes: 1 addition & 1 deletion kernel-collector
80 changes: 50 additions & 30 deletions src/socket.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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";

28 changes: 14 additions & 14 deletions src/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@

#include "socket.skel.h"

// Socket functions
char *function_list[] = { "inet_csk_accept",
"tcp_retransmit_skb",
"tcp_cleanup_rbuf",
"tcp_close",
"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

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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]);

Expand All @@ -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)
{
Expand Down

0 comments on commit 466d540

Please sign in to comment.