From 4ab4406f6e59ee09d893e31104236518fc81e991 Mon Sep 17 00:00:00 2001
From: yangchen <yangchen145@huawei.com>
Date: Tue, 28 Nov 2023 16:11:09 +0800
Subject: [PATCH] add lwip log: tcp_rst & tcp_abandon & tcp_abort

---
 src/core/tcp.c           | 24 ++++++++++++++++--------
 src/core/tcp_in.c        | 19 +++++++++++++++++--
 src/include/lwip/debug.h |  4 ++--
 3 files changed, 35 insertions(+), 12 deletions(-)

diff --git a/src/core/tcp.c b/src/core/tcp.c
index f6c4cf1..73d1d1e 100644
--- a/src/core/tcp.c
+++ b/src/core/tcp.c
@@ -415,6 +415,9 @@ tcp_close_shutdown(struct tcp_pcb *pcb, u8_t rst_on_unacked_data)
 
       /* don't call tcp_abort here: we must not deallocate the pcb since
          that might not be expected when calling tcp_close */
+      LWIP_DEBUGF(GAZELLE_DEBUG_SERIOUS,
+                  ("tcp_close_shutdown: Not all data received by app, send RST, local_port=%d, remote_port=%d\n",
+                  pcb->local_port, pcb->remote_port));
       tcp_rst(pcb, pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
               pcb->local_port, pcb->remote_port);
 
@@ -682,7 +685,8 @@ tcp_abandon(struct tcp_pcb *pcb, int reset)
 #endif /* TCP_QUEUE_OOSEQ */
     tcp_backlog_accepted(pcb);
     if (send_rst) {
-      LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_abandon: sending RST\n"));
+      LWIP_DEBUGF(TCP_RST_DEBUG | GAZELLE_DEBUG_SERIOUS,
+                  ("tcp_abandon: send RST, local port=%d, remote port=%d\n", local_port, pcb->remote_port));
       tcp_rst(pcb, seqno, ackno, &pcb->local_ip, &pcb->remote_ip, local_port, pcb->remote_port);
     }
     last_state = pcb->state;
@@ -1577,6 +1581,9 @@ tcp_slowtmr_start:
 #endif
 
       if (pcb_reset) {
+        LWIP_DEBUGF(GAZELLE_DEBUG_SERIOUS,
+                    ("tcp_slowtmr: KEEPALIVE timeout, send RST, local port=%d, remote port=%d\n",
+                    pcb->local_port, pcb->remote_port));
         tcp_rst(pcb, pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
                 pcb->local_port, pcb->remote_port);
       }
@@ -1947,8 +1954,8 @@ tcp_kill_prio(u8_t prio)
     }
   }
   if (inactive != NULL) {
-    LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_prio: killing oldest PCB %p (%"S32_F")\n",
-                            (void *)inactive, inactivity));
+    LWIP_DEBUGF(TCP_DEBUG | GAZELLE_DEBUG_SERIOUS,
+                ("tcp_kill_prio: killing oldest PCB (%"S32_F")\n", inactivity));
     tcp_abort(inactive);
   }
 }
@@ -1978,8 +1985,8 @@ tcp_kill_state(enum tcp_state state)
     }
   }
   if (inactive != NULL) {
-    LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_closing: killing oldest %s PCB %p (%"S32_F")\n",
-                            tcp_state_str[state], (void *)inactive, inactivity));
+    LWIP_DEBUGF(TCP_DEBUG | GAZELLE_DEBUG_SERIOUS,
+                ("tcp_kill_closing: killing oldest %s PCB (%"S32_F")\n", tcp_state_str[state], inactivity));
     /* Don't send a RST, since no data is lost. */
     tcp_abandon(inactive, 0);
   }
@@ -2005,8 +2012,8 @@ tcp_kill_timewait(void)
     }
   }
   if (inactive != NULL) {
-    LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_timewait: killing oldest TIME-WAIT PCB %p (%"S32_F")\n",
-                            (void *)inactive, inactivity));
+    LWIP_DEBUGF(TCP_DEBUG | GAZELLE_DEBUG_SERIOUS,
+                ("tcp_kill_timewait: killing oldest TIME-WAIT PCB (%"S32_F")\n", inactivity));
     tcp_abort(inactive);
   }
 }
@@ -2548,7 +2555,8 @@ tcp_netif_ip_addr_changed_pcblist(const ip_addr_t *old_addr, struct tcp_pcb *pcb
        ) {
       /* this connection must be aborted */
       struct tcp_pcb *next = pcb->next;
-      LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb));
+      LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE | GAZELLE_DEBUG_SERIOUS,
+                  ("netif_set_ipaddr: aborting TCP\n"));
       tcp_abort(pcb);
       pcb = next;
     } else {
diff --git a/src/core/tcp_in.c b/src/core/tcp_in.c
index 10f8a14..4755ab5 100644
--- a/src/core/tcp_in.c
+++ b/src/core/tcp_in.c
@@ -592,6 +592,7 @@ tcp_input(struct pbuf *p, struct netif *inp)
               pbuf_free(rest);
             }
 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
+            LWIP_DEBUGF(GAZELLE_DEBUG_SERIOUS, ("tcp_input: received data although already closed, send RST\n"));
             tcp_abort(pcb);
             goto aborted;
           }
@@ -683,10 +684,12 @@ aborted:
   } else {
     /* If no matching PCB was found, send a TCP RST (reset) to the
        sender. */
-    LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_input: no PCB match found, resetting.\n"));
     if (!(TCPH_FLAGS(tcphdr) & TCP_RST)) {
       TCP_STATS_INC(tcp.proterr);
       TCP_STATS_INC(tcp.drop);
+      LWIP_DEBUGF(TCP_RST_DEBUG | GAZELLE_DEBUG_SERIOUS,
+                  ("tcp_input: no PCB match found, send RST, dest port=%d, src port=%d\n",
+                  lwip_ntohs(tcphdr->dest), lwip_ntohs(tcphdr->src)));
       tcp_rst_netif(ip_data.current_input_netif, ackno, seqno + tcplen, ip_current_dest_addr(),
               ip_current_src_addr(), tcphdr->dest, tcphdr->src);
     }
@@ -761,7 +764,9 @@ tcp_listen_input(struct tcp_pcb_listen *pcb)
   if (flags & TCP_ACK) {
     /* For incoming segments with the ACK flag set, respond with a
        RST. */
-    LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_listen_input: ACK in LISTEN, sending reset\n"));
+    LWIP_DEBUGF(TCP_RST_DEBUG | GAZELLE_DEBUG_SERIOUS,
+                ("tcp_listen_input: ACK in LISTEN, send reset, dest port=%d, src port=%d\n",
+                lwip_ntohs(tcphdr->dest), lwip_ntohs(tcphdr->src)));
     tcp_rst_netif(ip_data.current_input_netif, ackno, seqno + tcplen, ip_current_dest_addr(),
             ip_current_src_addr(), tcphdr->dest, tcphdr->src);
   } else if (flags & TCP_SYN) {
@@ -854,6 +859,7 @@ tcp_listen_input(struct tcp_pcb_listen *pcb)
     /* Send a SYN|ACK together with the MSS option. */
     rc = tcp_enqueue_flags(npcb, TCP_SYN | TCP_ACK);
     if (rc != ERR_OK) {
+      LWIP_DEBUGF(GAZELLE_DEBUG_SERIOUS, ("tcp_listen_input: send SYN or ACK failed\n"));
       tcp_abandon(npcb, 0);
       PERF_RESUME(PERF_LAYER_TCP, PERF_POINT_TCP_SYN_RECV);
       return;
@@ -894,6 +900,9 @@ tcp_timewait_input(struct tcp_pcb *pcb)
        should be sent in reply */
     if (TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt, pcb->rcv_nxt + pcb->rcv_wnd)) {
       /* If the SYN is in the window it is an error, send a reset */
+      LWIP_DEBUGF(GAZELLE_DEBUG_SERIOUS,
+                  ("tcp_timewait_input: SYN in TIME_WAIT, send RST, dest port=%d, src port=%d\n",
+                  lwip_ntohs(tcphdr->dest), lwip_ntohs(tcphdr->src)));
       tcp_rst(pcb, ackno, seqno + tcplen, ip_current_dest_addr(),
               ip_current_src_addr(), tcphdr->dest, tcphdr->src);
       return;
@@ -1062,6 +1071,8 @@ tcp_process(struct tcp_pcb *pcb)
       /* received ACK? possibly a half-open connection */
       else if (flags & TCP_ACK) {
         /* send a RST to bring the other side in a non-synchronized state. */
+        LWIP_DEBUGF(GAZELLE_DEBUG_SERIOUS, ("tcp_process: ACK in SYN_SENT, send RST, dest port=%d, src port=%d\n",
+                    lwip_ntohs(tcphdr->dest), lwip_ntohs(tcphdr->src)));
         tcp_rst(pcb, ackno, seqno + tcplen, ip_current_dest_addr(),
                 ip_current_src_addr(), tcphdr->dest, tcphdr->src);
         /* Resend SYN immediately (don't wait for rto timeout) to establish
@@ -1104,6 +1115,7 @@ tcp_process(struct tcp_pcb *pcb)
              * the connection. */
             /* Already aborted? */
             if (err != ERR_ABRT) {
+              LWIP_DEBUGF(GAZELLE_DEBUG_SERIOUS, ("tcp_process: accept function returns with an error %d, send RST\n", err));
               tcp_abort(pcb);
             }
             return ERR_ABRT;
@@ -1131,6 +1143,9 @@ tcp_process(struct tcp_pcb *pcb)
           }
         } else {
           /* incorrect ACK number, send RST */
+          LWIP_DEBUGF(GAZELLE_DEBUG_SERIOUS, 
+                      ("tcp_process: incorrect ACK number in SYN_RCVD, send RST, ackno=%d, lastack=%d, snd_nxt=%d, dest port=%d, src port=%d\n",
+                      ackno, pcb->lastack, pcb->snd_nxt, lwip_ntohs(tcphdr->dest), lwip_ntohs(tcphdr->src)));
           tcp_rst(pcb, ackno, seqno + tcplen, ip_current_dest_addr(),
                   ip_current_src_addr(), tcphdr->dest, tcphdr->src);
         }
diff --git a/src/include/lwip/debug.h b/src/include/lwip/debug.h
index 4986973..c2de064 100644
--- a/src/include/lwip/debug.h
+++ b/src/include/lwip/debug.h
@@ -56,12 +56,12 @@
 /** Debug level: Serious. memory allocation failures, ... */
 #define LWIP_DBG_LEVEL_SERIOUS 0x02
 /** Debug level: Severe */
-#define LWIP_DBG_LEVEL_SEVERE  0x03
+#define LWIP_DBG_LEVEL_SEVERE  0x04
 /**
  * @}
  */
 
-#define LWIP_DBG_MASK_LEVEL    0x03
+#define LWIP_DBG_MASK_LEVEL    0x07
 /* compatibility define only */
 #define LWIP_DBG_LEVEL_OFF     LWIP_DBG_LEVEL_ALL
 
-- 
2.23.0
