From 01be1587c5e6771da95d6cf8d387c0b5ba15c275 Mon Sep 17 00:00:00 2001
From: jiangheng <jiangheng14@huawei.com>
Date: Wed, 27 Dec 2023 10:19:34 +0800
Subject: [PATCH] fix rte_ring_create time consuming

---
 src/api/sockets.c           |  2 +-
 src/api/sys_arch.c          | 63 ++++++++++++++++++++++++++++---------
 src/include/arch/sys_arch.h |  4 +++
 3 files changed, 54 insertions(+), 15 deletions(-)

diff --git a/src/api/sockets.c b/src/api/sockets.c
index b7ee304..15053b3 100644
--- a/src/api/sockets.c
+++ b/src/api/sockets.c
@@ -2810,7 +2810,7 @@ event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len)
 #endif
       break;
     case NETCONN_EVT_ERROR:
-      if (conn->pending_err != ERR_OK) {
+      if ((conn->pending_err != ERR_OK) && (conn->pending_err != ERR_RST)) {
           LWIP_DEBUGF(GAZELLE_DEBUG_SERIOUS, ("event_callback: have errevent, err=%d, fd=%d\n", conn->pending_err, conn->socket));
       }
       sock->errevent = 1;
diff --git a/src/api/sys_arch.c b/src/api/sys_arch.c
index 1bc3aee..332d460 100644
--- a/src/api/sys_arch.c
+++ b/src/api/sys_arch.c
@@ -37,6 +37,7 @@
 #include <unistd.h>
 
 #include <rte_memzone.h>
+#include <rte_malloc.h>
 
 #include "lwip/err.h"
 #include "lwip/mem.h"
@@ -89,9 +90,50 @@ static int mbox_wait_func(void)
     return eth_dev_poll();
 }
 
+struct rte_ring *gazelle_ring_create_fast(const char *name, uint32_t size, uint32_t flags)
+{
+    ssize_t ring_size;
+    char ring_name[RTE_MEMZONE_NAMESIZE] = {0};
+    struct rte_ring *ring;
+
+    ring_size = rte_ring_get_memsize(size);
+    if (ring_size < 0) {
+        RTE_LOG(ERR, EAL, "rte_ring_get_memszie failed\n");
+        return NULL;
+    }
+
+    /*
+     * rte_ring_create is not used because it calls memzone_lookup_thread_unsafe function
+     * time consuming when there are many rings
+     */
+    ring = rte_malloc_socket(NULL, ring_size, RTE_CACHE_LINE_SIZE, rte_socket_id());
+    if (ring == NULL) {
+        RTE_LOG(ERR, EAL, "cannot create rte_ring for mbox\n");
+        return NULL;
+    }
+
+    if (snprintf(ring_name, sizeof(ring_name), "%s""%"PRIXPTR, name, (uintptr_t)ring) < 0) {
+        rte_free(ring);
+        RTE_LOG(ERR, EAL, "snprintf failed\n");
+        return NULL;
+    }
+
+    if (rte_ring_init(ring, ring_name, size, flags) != 0) {
+        rte_free(ring);
+        RTE_LOG(ERR, EAL, "cannot init rte_ring for mbox\n");
+        return NULL;
+    }
+
+    return ring;
+}
+
+void gazelle_ring_free_fast(struct rte_ring *ring)
+{
+    rte_free(ring);
+}
+
 err_t sys_mbox_new(struct sys_mbox **mb, int size)
 {
-    int ret;
     struct sys_mbox *mbox;
 
     mbox = (struct sys_mbox *)memp_malloc(MEMP_SYS_MBOX);
@@ -100,21 +142,14 @@ err_t sys_mbox_new(struct sys_mbox **mb, int size)
     }
 
     mbox->flags = RING_F_SP_ENQ | RING_F_SC_DEQ;
-
-    ret = snprintf(mbox->name, sizeof(mbox->name), MBOX_NAME_PREFIX"%"PRIXPTR, (uintptr_t)mbox);
-    if (ret < 0) {
-        memp_free(MEMP_SYS_MBOX, mbox);
-        return ERR_VAL;
-    }
-
     mbox->size = size;
     mbox->socket_id = rte_socket_id();
-    mbox->ring = rte_ring_create(mbox->name, mbox->size, mbox->socket_id, mbox->flags);
-    if (!mbox->ring) {
-		RTE_LOG(ERR, EAL, "cannot create rte_ring for mbox\n");
-        memp_free(MEMP_SYS_MBOX, mbox);
-        return ERR_MEM;
+
+    mbox->ring = gazelle_ring_create_fast(MBOX_NAME_PREFIX, mbox->size, mbox->flags);
+    if (mbox->ring == NULL) {
+        sys_mbox_free(&mbox);
     }
+
     mbox->wait_fn = mbox_wait_func;
     *mb = mbox;
 
@@ -125,7 +160,7 @@ void sys_mbox_free(struct sys_mbox **mb)
 {
     struct sys_mbox *mbox = *mb;
     if (mbox->ring != NULL) {
-        rte_ring_free(mbox->ring);
+        gazelle_ring_free_fast(mbox->ring);
         mbox->ring = NULL;
     }
     memp_free(MEMP_SYS_MBOX, mbox);
diff --git a/src/include/arch/sys_arch.h b/src/include/arch/sys_arch.h
index 5e95f3d..bf7e437 100644
--- a/src/include/arch/sys_arch.h
+++ b/src/include/arch/sys_arch.h
@@ -123,6 +123,10 @@ static __rte_always_inline uint32_t gazelle_st_ring_dequeue_burst(struct rte_rin
 
     return n;
 }
+
+void gazelle_ring_free_fast(struct rte_ring *ring);
+struct rte_ring *gazelle_ring_create_fast(const char *name, uint32_t size, uint32_t flags);
+
 #endif
 
 void sys_calibrate_tsc(void);
-- 
2.33.0

