From 70a390384a97bca7048ec98eabd363e4fdf60bcc Mon Sep 17 00:00:00 2001
From: yinbin <yinbin8@huawei.com>
Date: Sat, 3 Aug 2024 19:31:59 +0800
Subject: [PATCH] INIT: fix lwip_init failed because of dpdk set errno

---
 src/api/sys_arch.c    |  5 ++++-
 src/core/mem.c        |  2 --
 src/core/memp.c       |  5 +----
 src/include/arch/cc.h | 15 +++++++++++----
 4 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/src/api/sys_arch.c b/src/api/sys_arch.c
index 553a1c7..dcfd0a4 100644
--- a/src/api/sys_arch.c
+++ b/src/api/sys_arch.c
@@ -417,9 +417,12 @@ u8_t *sys_hugepage_malloc(const char *name, unsigned size)
     SYS_FORMAT_NAME(memname, sizeof(memname), "%s_%d", name, rte_gettid());
     mz = rte_memzone_reserve(memname, size, rte_socket_id(), 0);
     if (mz == NULL) {
-        LWIP_DEBUGF(SYS_DEBUG | GAZELLE_DEBUG_SERIOUS, ("sys_hugepage_malloc: failed to reserver memory for mempool[%s]\n", memname));
+        LWIP_DEBUGF(SYS_DEBUG | GAZELLE_DEBUG_SERIOUS, ("sys_hugepage_malloc: failed to reserver memory for mempool[%s], errno %d\n", memname, errno));
         set_errno(ENOMEM);
         return NULL;
+    } else {
+        /* Ignore dpdk errno when mem allocation is successful */
+        errno = 0;
     }
 
     memset(mz->addr, 0, mz->len);
diff --git a/src/core/mem.c b/src/core/mem.c
index b5e50c0..78a1dd4 100644
--- a/src/core/mem.c
+++ b/src/core/mem.c
@@ -518,8 +518,6 @@ mem_init(void)
 #if GAZELLE_ENABLE
   /* see LWIP_RAM_HEAP_POINTER */
   LWIP_MEMORY_INIT_VAR(LWIP_RAM_HEAP_POINTER, MEM_SIZE_ALIGNED + (2U * SIZEOF_STRUCT_MEM));
-  if (errno != 0)
-    return;
 #endif /* GAZELLE_ENABLE */
 
   struct mem *mem;
diff --git a/src/core/memp.c b/src/core/memp.c
index 6c858be..01cf362 100644
--- a/src/core/memp.c
+++ b/src/core/memp.c
@@ -228,11 +228,8 @@ void
 memp_init(void)
 {
 #if GAZELLE_ENABLE
-#define LWIP_MEMPOOL(name,num,size,desc)  LWIP_MEMPOOL_INIT_VAR(name,num,size,desc)
+#define LWIP_MEMPOOL(name,num,size,desc)  LWIP_MEMPOOL_INIT_VAR(name,num,size,desc);
 #include "lwip/priv/memp_std.h"
-
-  if (errno != 0)
-    return;
 #endif /* GAZELLE_ENABLE */
 
   u16_t i;
diff --git a/src/include/arch/cc.h b/src/include/arch/cc.h
index 8527b44..51f87bb 100644
--- a/src/include/arch/cc.h
+++ b/src/include/arch/cc.h
@@ -46,8 +46,12 @@
 #define LWIP_DECLARE_MEMORY_ALIGNED(variable_name, size) \
   static PER_THREAD u8_t *variable_name;
 
-#define LWIP_MEMORY_INIT_VAR(name, size)    \
-  name = sys_hugepage_malloc(#name, size);
+#define LWIP_MEMORY_INIT_VAR(name, size) do { \
+  name = sys_hugepage_malloc(#name, size);    \
+  if (name == NULL)                           \
+    return;                                   \
+} while(0)
+
 
 #define LWIP_MEMPOOL_DECLARE(name,num,size,desc)            \
   LWIP_DECLARE_MEMORY_ALIGNED(memp_memory_ ## name ## _base, 0); \
@@ -56,13 +60,16 @@
                                                             \
   PER_THREAD struct memp_desc memp_ ## name;
 
-#define LWIP_MEMPOOL_INIT_VAR(name,num,size,desc)           \
+#define LWIP_MEMPOOL_INIT_VAR(name,num,size,desc) do {  \
   memp_memory_ ## name ## _base = sys_hugepage_malloc(#name, LWIP_MEM_ALIGN_BUFFER((num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size)))); \
+  if (memp_memory_ ## name ## _base == NULL)                \
+    return;                                                 \
   sys_mempool_var_init(&memp_ ## name,                      \
                        desc, size, num,                     \
                        memp_memory_ ## name ## _base,       \
                        &memp_tab_ ## name,                  \
                        &memp_stat_ ## name);                \
-  memp_pools[MEMP_ ## name] = &memp_ ## name;
+  memp_pools[MEMP_ ## name] = &memp_ ## name;               \
+} while(0)
 
 #endif /* _LWIP_ARCH_CC_H_ */
-- 
2.34.1

