You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by GitBox <gi...@apache.org> on 2022/07/24 06:03:10 UTC

[GitHub] [skywalking-rover] wu-sheng commented on a diff in pull request #40: Enhance the network profiling

wu-sheng commented on code in PR #40:
URL: https://github.com/apache/skywalking-rover/pull/40#discussion_r928200946


##########
bpf/profiling/network/netmonitor.c:
##########
@@ -127,63 +153,36 @@ static __always_inline void submit_new_connection(struct pt_regs* ctx, __u32 fun
     // fill the connection
     event->role = con.role;
     event->socket_family = con.socket_family;
-    __u16 port;
-    if (socket != NULL) {
-        event->need_complete_addr = 0;
-        // only get from accept function(server side)
-        struct sock* s;
-        BPF_CORE_READ_INTO(&s, socket, sk);
-
-        if (con.socket_family == AF_INET) {
-            BPF_CORE_READ_INTO(&port, s, __sk_common.skc_num);
-            event->local_port = port;
-            BPF_CORE_READ_INTO(&event->local_addr_v4, s, __sk_common.skc_rcv_saddr);
-            BPF_CORE_READ_INTO(&port, s, __sk_common.skc_dport);
-            event->remote_port = bpf_ntohs(port);
-            BPF_CORE_READ_INTO(&event->remote_addr_v4, s, __sk_common.skc_daddr);
-        } else if (con.socket_family == AF_INET6) {
-            BPF_CORE_READ_INTO(&port, s, __sk_common.skc_num);
-            event->local_port = port;
-            BPF_CORE_READ_INTO(&event->local_addr_v6, s, __sk_common.skc_v6_rcv_saddr.in6_u.u6_addr8);
-            BPF_CORE_READ_INTO(&port, s, __sk_common.skc_dport);
-            event->remote_port = bpf_ntohs(port);
-            BPF_CORE_READ_INTO(&event->remote_addr_v6, s, __sk_common.skc_v6_daddr.in6_u.u6_addr8);
-       } else {
-            event->local_port = 0;
-            event->remote_port = 0;
-       }
-    } else if (addr != NULL) {
-        event->need_complete_addr = 1;
-        if (con.socket_family == AF_INET) {
-            struct sockaddr_in *daddr = (struct sockaddr_in *)addr;
-            bpf_probe_read(&event->remote_addr_v4, sizeof(event->remote_addr_v4), &daddr->sin_addr.s_addr);
-            bpf_probe_read(&port, sizeof(port), &daddr->sin_port);
-            event->remote_port = bpf_ntohs(port);
-        } else if (con.socket_family == AF_INET6) {
-            struct sockaddr_in6 *daddr = (struct sockaddr_in6 *)addr;
-            bpf_probe_read(&event->remote_addr_v6, sizeof(event->remote_addr_v6), &daddr->sin6_addr.s6_addr);
-            bpf_probe_read(&port, sizeof(port), &daddr->sin6_port);
-            event->remote_port = bpf_ntohs(port);
-        } else {
-            event->remote_port = 0;
+    event->need_complete_addr = need_complete_addr;
+    event->local_addr_v4 = con.local_addr_v4;
+    __builtin_memcpy(&event->local_addr_v6, &con.local_addr_v4, 16*sizeof(__u8));
+    event->local_port = con.local_port;
+    event->remote_addr_v4 = con.remote_addr_v4;
+    __builtin_memcpy(&event->remote_addr_v6, &con.remote_addr_v6, 16*sizeof(__u8));
+    event->remote_port = con.remote_port;
+
+    __u32 ret = bpf_perf_event_output(ctx, &socket_connection_event_queue, BPF_F_CURRENT_CPU, event, sizeof(*event));
+    // if not send event success, then update to the event not sent
+    if (ret < 0) {
+        struct active_connection_t *con = bpf_map_lookup_elem(&active_connection_map, &conid);
+        if (con != NULL) {
+            con->connect_event_send = false;
+            bpf_map_update_elem(&active_connection_map, &conid, con, 0);
         }
-    } else {
-        event->need_complete_addr = 1;
-        // clean the cache in LRU(remote port is enough)
-        event->remote_port = 0;
     }
-
-    bpf_perf_event_output(ctx, &socket_connection_event_queue, BPF_F_CURRENT_CPU, event, sizeof(*event));
 }
 
 static __inline void notify_close_connection(struct pt_regs* ctx, __u64 conid, struct active_connection_t* con, __u64 start_time, __u64 end_time) {
-    // only trace ipv4, v6, or unknown
-    if (family_should_trace(con->socket_family) == false) {
-        return;
-    }
-    // pid is contains
-    if (tgid_should_trace(con->pid) == false) {
-        return;
+    // if the connect event not send, then check the pid or socket family
+    if (con->connect_event_send == false) {
+        // only trace ipv4, v6, or unknown
+        if (family_should_trace(con->socket_family) == false) {
+            return;
+        }
+        // pid is contains

Review Comment:
   This comment seems not clear, could you polish it?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org