From d7b0ffc7604075639f3eedbfe63fc0f12b87d23f Mon Sep 17 00:00:00 2001
From: jiangheng <jiangheng14@huawei.com>
Date: Tue, 28 Nov 2023 16:34:16 +0800
Subject: [PATCH] log: add errevent log and tcp exception statistics

---
 src/api/api_msg.c        | 1 +
 src/api/sockets.c        | 3 ++-
 src/core/tcp_in.c        | 2 ++
 src/include/lwip/stats.h | 3 +++
 4 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/api/api_msg.c b/src/api/api_msg.c
index 8d98be6..531da40 100644
--- a/src/api/api_msg.c
+++ b/src/api/api_msg.c
@@ -613,6 +613,7 @@ accept_function(void *arg, struct tcp_pcb *newpcb, err_t err)
     sys_mbox_free(&newconn->recvmbox);
     sys_mbox_set_invalid(&newconn->recvmbox);
     netconn_free(newconn);
+    MIB2_STATS_INC(mib2.tcpacceptmboxfull);
     return ERR_MEM;
   } else {
     /* Register event with callback */
diff --git a/src/api/sockets.c b/src/api/sockets.c
index 62052f2..65c69d4 100644
--- a/src/api/sockets.c
+++ b/src/api/sockets.c
@@ -2797,9 +2797,10 @@ event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len)
 #endif
       break;
     case NETCONN_EVT_ERROR:
+      LWIP_DEBUGF(GAZELLE_DEBUG_SERIOUS, ("event_callback: have errevent, err=%d\n", conn->pending_err));
       sock->errevent = 1;
 #if GAZELLE_ENABLE
-    if (netif_is_rtc_mode(netif_default)) {
+      if (netif_is_rtc_mode(netif_default)) {
         add_sock_event_nolock(sock, EPOLLERR);
       } else {
         add_sock_event(sock, EPOLLERR);
diff --git a/src/core/tcp_in.c b/src/core/tcp_in.c
index 1076f20..03b9942 100644
--- a/src/core/tcp_in.c
+++ b/src/core/tcp_in.c
@@ -770,6 +770,7 @@ tcp_listen_input(struct tcp_pcb_listen *pcb)
 #if TCP_LISTEN_BACKLOG
     if (pcb->accepts_pending >= pcb->backlog) {
       LWIP_DEBUGF(TCP_DEBUG, ("tcp_listen_input: listen backlog exceeded for port %"U16_F"\n", tcphdr->dest));
+      MIB2_STATS_INC(mib2.tcplistendrops);
       return;
     }
 #endif /* TCP_LISTEN_BACKLOG */
@@ -1845,6 +1846,7 @@ tcp_receive(struct tcp_pcb *pcb)
 
       } else {
         /* We get here if the incoming segment is out-of-sequence. */
+        MIB2_STATS_INC(mib2.tcpoutofseq);
 
 #if TCP_QUEUE_OOSEQ
         /* We queue the segment on the ->ooseq queue. */
diff --git a/src/include/lwip/stats.h b/src/include/lwip/stats.h
index 5953a74..805836c 100644
--- a/src/include/lwip/stats.h
+++ b/src/include/lwip/stats.h
@@ -153,6 +153,9 @@ struct stats_mib2 {
   u32_t tcpfinackcnt;
   u32_t tcpdelayackcnt;
   u32_t tcpredusedcnt;
+  u32_t tcpoutofseq;
+  u32_t tcpacceptmboxfull;
+  u32_t tcplistendrops;
 
   /* UDP */
   u32_t udpindatagrams;
-- 
2.27.0

