diff --git a/kernel/network_viewer_kern.c b/kernel/network_viewer_kern.c index 73420d81..1dab813f 100644 --- a/kernel/network_viewer_kern.c +++ b/kernel/network_viewer_kern.c @@ -150,12 +150,15 @@ struct bpf_map_def SEC("maps") tbl_sock_total_stats = { /** * Function used to update 64 bit values and avoid overflow */ -static void netdata_update_u64(u64 *res, u64 value) +static void netdata_update_u64(__u64 *res, __u64 value) { - if ( (0xFFFFFFFFFFFFFFFF - *res) <= value) + if (!value) + return; + + __sync_fetch_and_add(res, value); + if ( (0xFFFFFFFFFFFFFFFF - *res) <= value) { *res = value; - else - *res += value; + } } /* @@ -163,7 +166,7 @@ static void netdata_update_u64(u64 *res, u64 value) */ static void netdata_update_global(__u32 key, __u64 value) { - u64 *res; + __u64 *res; res = bpf_map_lookup_elem(&tbl_sock_total_stats, &key); if (res) { netdata_update_u64(res, value) ; @@ -307,7 +310,7 @@ int netdata_tcp_sendmsg(struct pt_regs* ctx) __u64 pid_tgid = bpf_get_current_pid_tgid(); __u32 pid = (__u32)(pid_tgid >> 32); __u32 tgid = (__u32)( 0x00000000FFFFFFFF & pid_tgid); - size_t sent = (size_t)PT_REGS_PARM3(ctx); + __u64 sent = (__u64)PT_REGS_PARM3(ctx); struct inet_sock *is = inet_sk((struct sock *)PT_REGS_PARM1(ctx)); netdata_update_global(NETDATA_KEY_CALLS_TCP_SENDMSG, 1); @@ -315,10 +318,10 @@ int netdata_tcp_sendmsg(struct pt_regs* ctx) family = set_idx_value(&idx, is, pid); tbl = (family == AF_INET6)?&tbl_conn_ipv6_stats:&tbl_conn_ipv4_stats; - netdata_update_global(NETDATA_KEY_BYTES_TCP_SENDMSG, (__u64)sent); - update_socket_table(tbl, &idx, (__u64) sent, 0, pid_tgid, 6); + update_socket_table(tbl, &idx, sent, 0, pid_tgid, 6); - update_pid_stats(pid, tgid, (__u64) sent, 0); + netdata_update_global(NETDATA_KEY_BYTES_TCP_SENDMSG, sent); + update_pid_stats(pid, tgid, sent, 0); #if NETDATASEL < 2 if (ret < 0) { @@ -351,13 +354,14 @@ int netdata_tcp_cleanup_rbuf(struct pt_regs* ctx) return 0; } + __u64 received = (__u64) PT_REGS_PARM2(ctx); + family = set_idx_value(&idx, is, pid); tbl = (family == AF_INET6)?&tbl_conn_ipv6_stats:&tbl_conn_ipv4_stats; - netdata_update_global(NETDATA_KEY_BYTES_TCP_CLEANUP_RBUF, (__u64) copied); - update_socket_table(tbl, &idx, 0, (__u64) copied, pid_tgid, 6); - - update_pid_stats(pid, tgid, 0, (__u64) copied); + netdata_update_global(NETDATA_KEY_BYTES_TCP_CLEANUP_RBUF, received); + update_socket_table(tbl, &idx, 0, received, pid_tgid, 6); + update_pid_stats(pid, tgid, 0, received); return 0; } @@ -377,7 +381,7 @@ int netdata_tcp_close(struct pt_regs* ctx) __u32 pid = (__u32)(pid_tgid >> 32); struct inet_sock *is = inet_sk((struct sock *)PT_REGS_PARM1(ctx)); - netdata_update_global(NETDATA_KEY_CALLS_TCP_CLOSE, 1); + netdata_update_global(NETDATA_KEY_CALLS_TCP_CLOSE, 1); family = set_idx_value(&idx, is, pid); tbl = (family == AF_INET6)?&tbl_conn_ipv6_stats:&tbl_conn_ipv4_stats; @@ -445,15 +449,17 @@ int trace_udp_ret_recvmsg(struct pt_regs* ctx) return 0; } + __u64 received = (__u64) PT_REGS_RC(ctx); + bpf_map_delete_elem(&tbl_nv_udp_conn_stats, &pid_tgid); family = set_idx_value(&idx, is, pid); tbl = (family == AF_INET6)?&tbl_conn_ipv6_stats:&tbl_conn_ipv4_stats; - netdata_update_global(NETDATA_KEY_BYTES_UDP_RECVMSG, (__u64) copied); - update_socket_table(tbl, &idx, 0, (__u64) copied, pid_tgid, 17); + netdata_update_global(NETDATA_KEY_BYTES_UDP_RECVMSG, received); + update_socket_table(tbl, &idx, 0, received, pid_tgid, 17); - update_pid_stats(pid, tgid, 0, (__u64) copied); + update_pid_stats(pid, tgid, 0, received); return 0; } @@ -487,9 +493,9 @@ int trace_udp_sendmsg(struct pt_regs* ctx) tbl = (family == AF_INET6)?&tbl_conn_ipv6_stats:&tbl_conn_ipv4_stats; update_socket_table(tbl, &idx, (__u64) sent, 0, pid_tgid, 17); + update_pid_stats(pid, tgid, (__u64) sent, 0); netdata_update_global(NETDATA_KEY_BYTES_UDP_SENDMSG, (__u64) sent); - update_pid_stats(pid, tgid, (__u64) sent, 0); #if NETDATASEL < 2 if (ret < 0) { diff --git a/kernel/process_kern.c b/kernel/process_kern.c index f746c7ae..1397dbb0 100644 --- a/kernel/process_kern.c +++ b/kernel/process_kern.c @@ -74,23 +74,29 @@ static unsigned int log2l(unsigned long v) static void netdata_update_u32(u32 *res, u32 value) { - if ( (0xFFFFFFFF - *res) <= value) + if (!value) + return; + + __sync_fetch_and_add(res, value); + if ( (0xFFFFFFFFFFFFFFFF - *res) <= value) { *res = value; - else - *res += value; + } } -static void netdata_update_u64(u64 *res, u64 value) +static void netdata_update_u64(__u64 *res, __u64 value) { - if ( (0x7FFFFFFFFFFFFFFF - *res) <= value) + if (!value) + return; + + __sync_fetch_and_add(res, value); + if ( (0xFFFFFFFFFFFFFFFF - *res) <= value) { *res = value; - else - *res += value; + } } static void netdata_update_global(__u32 key, __u64 value) { - u64 *res; + __u64 *res; res = bpf_map_lookup_elem(&tbl_total_stats, &key); if (res) { netdata_update_u64(res, value) ; @@ -181,7 +187,7 @@ int netdata_sys_write(struct pt_regs* ctx) tot = 0; #endif netdata_update_global(NETDATA_KEY_BYTES_VFS_WRITE, tot); - netdata_update_u64(&fill->write_bytes, (u64) tot); + netdata_update_u64(&fill->write_bytes, tot); #if NETDATASEL < 2 } #endif @@ -199,7 +205,7 @@ int netdata_sys_write(struct pt_regs* ctx) tot = 0; #endif netdata_update_global(NETDATA_KEY_BYTES_VFS_WRITE, tot); - data.write_bytes = (u64)tot; + data.write_bytes = tot; #if NETDATASEL < 2 } #endif @@ -249,7 +255,7 @@ int netdata_sys_writev(struct pt_regs* ctx) tot = 0; #endif netdata_update_global(NETDATA_KEY_BYTES_VFS_WRITEV, tot); - netdata_update_u64(&fill->writev_bytes, (u64) tot); + netdata_update_u64(&fill->writev_bytes, tot); #if NETDATASEL < 2 } #endif @@ -317,7 +323,7 @@ int netdata_sys_read(struct pt_regs* ctx) tot = 0; #endif netdata_update_global(NETDATA_KEY_BYTES_VFS_READ, tot); - netdata_update_u64(&fill->read_bytes, (u64) tot); + netdata_update_u64(&fill->read_bytes, tot); #if NETDATASEL < 2 } #endif @@ -385,7 +391,7 @@ int netdata_sys_readv(struct pt_regs* ctx) tot = 0; #endif netdata_update_global(NETDATA_KEY_BYTES_VFS_READV, tot); - netdata_update_u64(&fill->readv_bytes, (u64) tot); + netdata_update_u64(&fill->readv_bytes, tot); #if NETDATASEL < 2 } #endif @@ -701,7 +707,7 @@ int netdata_clone(struct pt_regs* ctx) __u64 pid_tgid = bpf_get_current_pid_tgid(); __u32 pid = (__u32)(pid_tgid >> 32); __u32 tgid = (__u32)( 0x00000000FFFFFFFF & pid_tgid); - u64 arg1 = (u64)PT_REGS_PARM2(ctx); + __u64 arg1 = (__u64)PT_REGS_PARM2(ctx); arg1 &= CLONE_THREAD|CLONE_VM; if(!arg1)