From 61bf27272a56a426909a809a8a20dcf4220a3273 Mon Sep 17 00:00:00 2001
From: zhengjiebing <zhengjiebing_yewu@cmss.chinamobile.com>
Date: Wed, 29 Nov 2023 11:10:51 +0800
Subject: [PATCH] dfx: gazellectl -c support ipv6
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

 日期：  Thu Dec 7 14:28:51 2023 +0800
---
 src/common/gazelle_dfx_msg.h           |  6 ++--
 src/common/gazelle_opt.h               |  4 +++
 src/lstack/core/lstack_control_plane.c |  4 +--
 src/lstack/core/lstack_lwip.c          |  8 +++---
 src/ltran/ltran_dfx.c                  | 39 ++++++++++++++++----------
 5 files changed, 38 insertions(+), 23 deletions(-)

diff --git a/src/common/gazelle_dfx_msg.h b/src/common/gazelle_dfx_msg.h
index ac6ea5e..698846f 100644
--- a/src/common/gazelle_dfx_msg.h
+++ b/src/common/gazelle_dfx_msg.h
@@ -16,6 +16,8 @@
 #include <sys/types.h>
 #include <stdint.h>
 
+#include <lwip/reg_sock.h>
+
 #define GAZELLE_CLIENT_NUM_MIN           1
 #define GAZELLE_LOG_LEVEL_MAX            10
 #define GAZELLECTL_TIMEOUT               5000 // millisecond
@@ -170,8 +172,8 @@ struct gazelle_stat_lstack_snmp {
 /* same as define in lwip/tcp.h - struct tcp_pcb_dp */
 struct gazelle_stat_lstack_conn_info {
     uint32_t state;
-    uint32_t rip;
-    uint32_t lip;
+    gz_addr_t rip;
+    gz_addr_t lip;
     uint16_t r_port;
     uint16_t l_port;
     uint32_t in_send;
diff --git a/src/common/gazelle_opt.h b/src/common/gazelle_opt.h
index 7316fc6..bb540f4 100644
--- a/src/common/gazelle_opt.h
+++ b/src/common/gazelle_opt.h
@@ -52,6 +52,10 @@
 #define STACK_THREAD_DEFAULT        4
 #define STACK_NIC_READ_DEFAULT      128
 
+/* same as define in lwip/ip_addr.h */
+#define GZ_ADDR_TYPE_V4             0
+#define GZ_ADDR_TYPE_V6             6
+
 #define MTU_DEFAULT_DATA_LEN        1460
 #define VLAN_HEAD_LEN               4
 #define IPV6_EXTRA_HEAD_LEN         20
diff --git a/src/lstack/core/lstack_control_plane.c b/src/lstack/core/lstack_control_plane.c
index 4633834..e7fcd26 100644
--- a/src/lstack/core/lstack_control_plane.c
+++ b/src/lstack/core/lstack_control_plane.c
@@ -361,9 +361,9 @@ static int32_t reg_conn(enum tcp_list_state table_state, enum reg_ring_type reg_
             continue;
         }
         qtuple.protocol = 0;
-        qtuple.src_ip = conn->conn_list[i].lip;
+        qtuple.src_ip = conn->conn_list[i].lip.u_addr.ip4.addr;
         qtuple.src_port = lwip_htons(conn->conn_list[i].l_port);
-        qtuple.dst_ip = conn->conn_list[i].rip;
+        qtuple.dst_ip = conn->conn_list[i].rip.u_addr.ip4.addr;
         qtuple.dst_port = lwip_htons(conn->conn_list[i].r_port);
 
         if ((table_state == LISTEN_LIST) && (!match_host_addr(qtuple.src_ip))) {
diff --git a/src/lstack/core/lstack_lwip.c b/src/lstack/core/lstack_lwip.c
index af9bf73..5a3b703 100644
--- a/src/lstack/core/lstack_lwip.c
+++ b/src/lstack/core/lstack_lwip.c
@@ -1135,8 +1135,8 @@ static void copy_pcb_to_conn(struct gazelle_stat_lstack_conn_info *conn, const s
 {
     struct netconn *netconn = (struct netconn *)pcb->callback_arg;
 
-    conn->lip = ip_2_ip4(&pcb->local_ip)->addr;
-    conn->rip = ip_2_ip4(&pcb->remote_ip)->addr;
+    conn->lip = *((gz_addr_t *)&pcb->local_ip);
+    conn->rip = *((gz_addr_t *)&pcb->remote_ip);
     conn->l_port = pcb->local_port;
     conn->r_port = pcb->remote_port;
     conn->in_send = pcb->snd_queuelen;
@@ -1229,11 +1229,11 @@ uint32_t do_lwip_get_conntable(struct gazelle_stat_lstack_conn_info *conn,
     for (struct tcp_pcb_listen *pcbl = tcp_listen_pcbs.listen_pcbs; pcbl != NULL && conn_num < max_num;
         pcbl = pcbl->next) {
         conn[conn_num].state = LISTEN_LIST;
-        conn[conn_num].lip = ip_2_ip4(&pcbl->local_ip)->addr;
+        conn[conn_num].lip = *((gz_addr_t *)&pcbl->local_ip);
         conn[conn_num].l_port = pcbl->local_port;
         conn[conn_num].tcp_sub_state = pcbl->state;
         struct netconn *netconn = (struct netconn *)pcbl->callback_arg;
-	conn[conn_num].fd = netconn != NULL ? netconn->socket : -1;
+        conn[conn_num].fd = netconn != NULL ? netconn->socket : -1;
         if (netconn != NULL && netconn->acceptmbox != NULL) {
             conn[conn_num].recv_cnt = rte_ring_count(netconn->acceptmbox->ring);
         }
diff --git a/src/ltran/ltran_dfx.c b/src/ltran/ltran_dfx.c
index d3ff527..273eeaa 100644
--- a/src/ltran/ltran_dfx.c
+++ b/src/ltran/ltran_dfx.c
@@ -233,6 +233,7 @@ static int32_t dfx_connect_ltran(bool use_ltran, bool probe)
             strlen(GAZELLE_DFX_SOCK_FILENAME) + 1);
         if (ret != EOK) {
             printf("%s:%d strncat_s fail ret=%d\n", __FUNCTION__, __LINE__, ret);
+            goto END;
         }
     } else {
         ret = strncat_s(addr.sun_path, sizeof(addr.sun_path), GAZELLE_REG_SOCK_FILENAME,
@@ -973,10 +974,11 @@ static void gazelle_print_lstack_stat_snmp(void *buf, const struct gazelle_stat_
 static void gazelle_print_lstack_stat_conn(void *buf, const struct gazelle_stat_msg_request *req_msg)
 {
     uint32_t i;
-    struct in_addr rip;
-    struct in_addr lip;
-    char str_ip[GAZELLE_SUBNET_LENGTH_MAX] = {0};
-    char str_rip[GAZELLE_SUBNET_LENGTH_MAX] = {0};
+    char str_ip[INET6_ADDRSTRLEN] = {0};
+    char str_rip[INET6_ADDRSTRLEN] = {0};
+    /* ip:port, 6 is the length reserved for port */
+    char str_laddr[INET6_ADDRSTRLEN + 6] = {0};
+    char str_raddr[INET6_ADDRSTRLEN + 6] = {0};
     struct gazelle_stack_dfx_data *stat = (struct gazelle_stack_dfx_data *)buf;
     struct gazelle_stat_lstack_conn *conn = &stat->data.conn;
     struct timeval time = {0};
@@ -986,30 +988,37 @@ static void gazelle_print_lstack_stat_conn(void *buf, const struct gazelle_stat_
     do {
         printf("\n------ stack tid: %6u ------time=%lu\n", stat->tid, time.tv_sec * 1000000 + time.tv_usec);
         printf("No.   Proto lwip_recv recv_ring in_send send_ring cwn      rcv_wnd  snd_wnd   snd_buf   snd_nxt"
-            "        lastack        rcv_nxt        events    epoll_ev  evlist fd     Local Address        "
-            "Foreign Address    State\n");
+            "        lastack        rcv_nxt        events    epoll_ev  evlist fd     Local Address"
+            "                                        Foreign Address                                      State\n");
         uint32_t unread_pkts = 0;
         uint32_t unsend_pkts = 0;
         for (i = 0; i < conn->conn_num && i < GAZELLE_LSTACK_MAX_CONN; i++) {
             struct gazelle_stat_lstack_conn_info *conn_info = &conn->conn_list[i];
 
-            rip.s_addr = conn_info->rip;
-            lip.s_addr = conn_info->lip;
+            uint32_t domain = conn_info->lip.type == GZ_ADDR_TYPE_V4 ? AF_INET : AF_INET6;
+            void *lip = (void *)&conn_info->lip;
+            void *rip = (void *)&conn_info->rip;
+
             if ((conn_info->state == GAZELLE_ACTIVE_LIST) || (conn_info->state == GAZELLE_TIME_WAIT_LIST)) {
+                inet_ntop(domain, lip, str_ip, sizeof(str_ip));
+                inet_ntop(domain, rip, str_rip, sizeof(str_rip));
+                sprintf_s(str_laddr, sizeof(str_laddr), "%s:%hu", str_ip, conn_info->l_port);
+                sprintf_s(str_raddr, sizeof(str_raddr), "%s:%hu", str_rip, conn_info->r_port);
                 printf("%-6utcp   %-10u%-10u%-8u%-10u%-9d%-9d%-10d%-10d%-15u%-15u%-15u%-10x%-10x%-7d%-7d"
-                    "%s:%hu   %s:%hu  %s\n", i, conn_info->recv_cnt, conn_info->recv_ring_cnt, conn_info->in_send,
+                    "%-52s %-52s %s\n", i, conn_info->recv_cnt, conn_info->recv_ring_cnt, conn_info->in_send,
                     conn_info->send_ring_cnt, conn_info->cwn, conn_info->rcv_wnd, conn_info->snd_wnd,
                     conn_info->snd_buf, conn_info->snd_nxt, conn_info->lastack, conn_info->rcv_nxt, conn_info->events,
                     conn_info->epoll_events, conn_info->eventlist, conn_info->fd,
-                    inet_ntop(AF_INET, &lip, str_ip, sizeof(str_ip)), conn_info->l_port,
-                    inet_ntop(AF_INET, &rip, str_rip, sizeof(str_rip)), conn_info->r_port,
-                    tcp_state_to_str(conn_info->tcp_sub_state));
+                    str_laddr, str_raddr, tcp_state_to_str(conn_info->tcp_sub_state));
             } else if (conn_info->state == GAZELLE_LISTEN_LIST) {
-                printf("%-6utcp    %-147u%-7d%s:%hu   0.0.0.0:*          LISTEN\n", i, conn_info->recv_cnt,
-                    conn_info->fd, inet_ntop(AF_INET, &lip, str_ip, sizeof(str_ip)), conn_info->l_port);
+                inet_ntop(domain, lip, str_ip, sizeof(str_ip));
+                sprintf_s(str_laddr, sizeof(str_laddr), "%s:%hu", str_ip, conn_info->l_port);
+                sprintf_s(str_raddr, sizeof(str_raddr), "%s:*", domain == AF_INET ? "0.0.0.0" : "::0");
+                printf("%-6utcp    %-147u%-7d%-52s %-52s LISTEN\n", i, conn_info->recv_cnt,
+                    conn_info->fd, str_laddr, str_raddr);
             } else {
                 printf("Got unknow tcp conn::%s:%5hu, state:%u\n",
-                    inet_ntop(AF_INET, &lip, str_ip, sizeof(str_ip)), conn_info->l_port, conn_info->state);
+                    inet_ntop(domain, lip, str_ip, sizeof(str_ip)), conn_info->l_port, conn_info->state);
             }
             unread_pkts += conn_info->recv_ring_cnt + conn_info->recv_cnt;
             unsend_pkts += conn_info->send_ring_cnt + conn_info->in_send;
-- 
2.27.0

