From 66b0ba4be7a5c3b79482eafdc28fd75e8f44e761 Mon Sep 17 00:00:00 2001
From: jiangheng <jiangheng14@huawei.com>
Date: Wed, 27 Dec 2023 10:26:07 +0800
Subject: [PATCH] fix rte_ring_create/free time consuming

---
 src/lstack/core/lstack_dpdk.c    | 27 +++------------------------
 src/lstack/core/lstack_lwip.c    | 14 ++++++--------
 src/lstack/include/lstack_dpdk.h |  1 -
 3 files changed, 9 insertions(+), 33 deletions(-)

diff --git a/src/lstack/core/lstack_dpdk.c b/src/lstack/core/lstack_dpdk.c
index 6933ecd..729a84c 100644
--- a/src/lstack/core/lstack_dpdk.c
+++ b/src/lstack/core/lstack_dpdk.c
@@ -245,43 +245,22 @@ struct rte_mempool *create_mempool(const char *name, uint32_t count, uint32_t si
     return mempool;
 }
 
-struct rte_ring *create_ring(const char *name, uint32_t count, uint32_t flags, int32_t queue_id)
-{
-    char ring_name[RTE_RING_NAMESIZE] = {0};
-    struct rte_ring *ring;
-
-    int32_t ret = snprintf_s(ring_name, sizeof(ring_name), RTE_RING_NAMESIZE - 1,
-        "%s_%d_%d", name, get_global_cfg_params()->process_idx,  queue_id);
-    if (ret < 0) {
-        LSTACK_LOG(ERR, LSTACK, "snprintf_s fail ret=%d\n", ret);
-        return NULL;
-    }
-
-    ring = rte_ring_create(ring_name, count, rte_socket_id(), flags);
-    if (ring == NULL) {
-        LSTACK_LOG(ERR, LSTACK, "%s create failed. errno: %d.\n", name, rte_errno);
-    }
-
-    return ring;
-}
-
 int32_t create_shared_ring(struct protocol_stack *stack)
 {
     lockless_queue_init(&stack->rpc_queue);
 
     if (use_ltran()) {
-        stack->rx_ring = create_ring("RING_RX", VDEV_RX_QUEUE_SZ, RING_F_SP_ENQ | RING_F_SC_DEQ, stack->queue_id);
+        stack->rx_ring = gazelle_ring_create_fast("RING_RX", VDEV_RX_QUEUE_SZ, RING_F_SP_ENQ | RING_F_SC_DEQ);
         if (stack->rx_ring == NULL) {
             return -1;
         }
 
-        stack->tx_ring = create_ring("RING_TX", VDEV_TX_QUEUE_SZ, RING_F_SP_ENQ | RING_F_SC_DEQ, stack->queue_id);
+        stack->tx_ring = gazelle_ring_create_fast("RING_TX", VDEV_TX_QUEUE_SZ, RING_F_SP_ENQ | RING_F_SC_DEQ);
         if (stack->tx_ring == NULL) {
             return -1;
         }
 
-        stack->reg_ring = create_ring("SHARED_REG_RING", VDEV_REG_QUEUE_SZ, RING_F_SP_ENQ | RING_F_SC_DEQ,
-            stack->queue_id);
+        stack->reg_ring = gazelle_ring_create_fast("SHARED_REG_RING", VDEV_REG_QUEUE_SZ, RING_F_SP_ENQ | RING_F_SC_DEQ);
         if (stack->reg_ring == NULL) {
             return -1;
         }
diff --git a/src/lstack/core/lstack_lwip.c b/src/lstack/core/lstack_lwip.c
index c167ba4..4953f3d 100644
--- a/src/lstack/core/lstack_lwip.c
+++ b/src/lstack/core/lstack_lwip.c
@@ -72,13 +72,13 @@ static void reset_sock_data(struct lwip_sock *sock)
     /* check null pointer in ring_free func */
     if (sock->recv_ring) {
         free_ring_pbuf(sock->recv_ring);
-        rte_ring_free(sock->recv_ring);
+        gazelle_ring_free_fast(sock->recv_ring);
         sock->recv_ring = NULL;
     }
 
     if (sock->send_ring) {
         free_ring_pbuf(sock->send_ring);
-        rte_ring_free(sock->send_ring);
+        gazelle_ring_free_fast(sock->send_ring);
         sock->send_ring = NULL;
     }
 
@@ -169,7 +169,6 @@ static bool replenish_send_idlembuf(struct protocol_stack *stack, struct lwip_so
 
 void do_lwip_init_sock(int32_t fd)
 {
-    static _Atomic uint32_t name_tick = 0;
     struct protocol_stack *stack = get_protocol_stack();
     struct lwip_sock *sock = get_socket(fd);
     if (sock == NULL) {
@@ -178,18 +177,17 @@ void do_lwip_init_sock(int32_t fd)
 
     reset_sock_data(sock);
 
-    sock->recv_ring = create_ring("sock_recv", SOCK_RECV_RING_SIZE, RING_F_SP_ENQ | RING_F_SC_DEQ,
-        atomic_fetch_add(&name_tick, 1));
+    sock->recv_ring = gazelle_ring_create_fast("sock_recv", SOCK_RECV_RING_SIZE, RING_F_SP_ENQ | RING_F_SC_DEQ);
     if (sock->recv_ring == NULL) {
         LSTACK_LOG(ERR, LSTACK, "sock_recv create failed. errno: %d.\n", rte_errno);
         return;
     }
 
-    sock->send_ring = create_ring("sock_send",
+    sock->send_ring = gazelle_ring_create_fast("sock_send",
         get_global_cfg_params()->send_ring_size,
-        RING_F_SP_ENQ | RING_F_SC_DEQ,
-        atomic_fetch_add(&name_tick, 1));
+        RING_F_SP_ENQ | RING_F_SC_DEQ);
     if (sock->send_ring == NULL) {
+        gazelle_ring_free_fast(sock->recv_ring);
         LSTACK_LOG(ERR, LSTACK, "sock_send create failed. errno: %d.\n", rte_errno);
         return;
     }
diff --git a/src/lstack/include/lstack_dpdk.h b/src/lstack/include/lstack_dpdk.h
index 05f5bc6..4a160e0 100644
--- a/src/lstack/include/lstack_dpdk.h
+++ b/src/lstack/include/lstack_dpdk.h
@@ -44,7 +44,6 @@ struct rte_mbuf;
 int32_t fill_mbuf_to_ring(struct rte_mempool *mempool, struct rte_ring *ring, uint32_t mbuf_num);
 int32_t dpdk_eal_init(void);
 int32_t pktmbuf_pool_init(struct protocol_stack *stack);
-struct rte_ring *create_ring(const char *name, uint32_t count, uint32_t flags, int32_t queue_id);
 struct rte_mempool *create_mempool(const char *name, uint32_t count, uint32_t size,
                                    uint32_t flags, int32_t idx);
 int32_t create_shared_ring(struct protocol_stack *stack);
-- 
2.27.0

