From b90801a943f140a62761158a058d1076212dccf3 Mon Sep 17 00:00:00 2001
From: yangchen <yangchen145@huawei.com>
Date: Thu, 18 Jan 2024 10:01:52 +0800
Subject: [PATCH] udp: do not merge data into last pbuf

---
 src/lstack/core/lstack_lwip.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/lstack/core/lstack_lwip.c b/src/lstack/core/lstack_lwip.c
index 87ec1f5..6ebe589 100644
--- a/src/lstack/core/lstack_lwip.c
+++ b/src/lstack/core/lstack_lwip.c
@@ -542,14 +542,14 @@ static ssize_t do_lwip_fill_sendring(struct lwip_sock *sock, const void *buf, si
     }
 
     struct protocol_stack *stack = sock->stack;
-    if (!stack|| len == 0) {
+    if (!stack || len == 0) {
         return 0;
     }
 
     ssize_t send_len = 0;
 
     /* merge data into last pbuf */
-    if (sock->remain_len) {
+    if (!NETCONN_IS_UDP(sock) && sock->remain_len) {
         send_len = merge_data_lastpbuf(sock, (char *)buf, len);
         if (send_len >= len) {
             send_len = len;
@@ -1045,6 +1045,11 @@ ssize_t do_lwip_read_from_stack(int32_t fd, void *buf, size_t len, int32_t flags
                 calculate_lstack_latency(&sock->stack->latency, pbuf, GAZELLE_LATENCY_READ);
             }
             gazelle_ring_read_over(sock->recv_ring);
+
+            /* in udp, if pbuf remaining len less than copy_len, discard these packets */
+            if (recvd > 0 && NETCONN_IS_UDP(sock)) {
+                break;
+            }
         }
     }
 
-- 
2.33.0

