diff --git a/0229-fix-gazellectl-x-error-when-multiplt-user-nic-config.patch b/0229-fix-gazellectl-x-error-when-multiplt-user-nic-config.patch new file mode 100644 index 0000000..74c1313 --- /dev/null +++ b/0229-fix-gazellectl-x-error-when-multiplt-user-nic-config.patch @@ -0,0 +1,68 @@ +From dcfd85d26a09a516a924350fa6404ec4237c2514 Mon Sep 17 00:00:00 2001 +From: jiangheng12 +Date: Fri, 31 Mar 2023 16:05:46 +0800 +Subject: [PATCH] fix gazellectl -x error when multiplt user nic config + +--- + src/lstack/core/lstack_dpdk.c | 3 ++- + src/lstack/core/lstack_stack_stat.c | 2 +- + src/lstack/include/lstack_dpdk.h | 2 +- + src/ltran/ltran_dfx.c | 2 +- + 4 files changed, 5 insertions(+), 4 deletions(-) + +diff --git a/src/lstack/core/lstack_dpdk.c b/src/lstack/core/lstack_dpdk.c +index ebfebaa..e386dfc 100644 +--- a/src/lstack/core/lstack_dpdk.c ++++ b/src/lstack/core/lstack_dpdk.c +@@ -298,7 +298,8 @@ void lstack_log_level_init(void) + } + + // get port id +-inline uint16_t get_port_id(){ ++inline uint16_t get_port_id(void) ++{ + uint16_t port_id = get_global_cfg_params()->port_id; + return port_id; + } +diff --git a/src/lstack/core/lstack_stack_stat.c b/src/lstack/core/lstack_stack_stat.c +index eed7fbf..d1c749a 100644 +--- a/src/lstack/core/lstack_stack_stat.c ++++ b/src/lstack/core/lstack_stack_stat.c +@@ -260,7 +260,7 @@ int32_t handle_stack_cmd(int32_t fd, enum GAZELLE_STAT_MODE stat_mode) + struct protocol_stack_group *stack_group = get_protocol_stack_group(); + + if (stat_mode == GAZELLE_STAT_LSTACK_SHOW_XSTATS) { +- dpdk_nic_xstats_get(&dfx, 0); ++ dpdk_nic_xstats_get(&dfx, get_port_id()); + dfx.tid = 0; + dfx.eof = 1; + send_control_cmd_data(fd, &dfx); +diff --git a/src/lstack/include/lstack_dpdk.h b/src/lstack/include/lstack_dpdk.h +index 7c222ca..a896903 100644 +--- a/src/lstack/include/lstack_dpdk.h ++++ b/src/lstack/include/lstack_dpdk.h +@@ -52,7 +52,7 @@ void dpdk_skip_nic_init(void); + int32_t dpdk_init_lstack_kni(void); + void dpdk_restore_pci(void); + bool port_in_stack_queue(uint32_t src_ip, uint32_t dst_ip, uint16_t src_port, uint16_t dst_port); +-uint16_t get_port_id(); ++uint16_t get_port_id(void); + struct rte_mempool *create_pktmbuf_mempool(const char *name, uint32_t nb_mbuf,uint32_t mbuf_cache_size, uint16_t queue_id); + + void dpdk_nic_xstats_get(struct gazelle_stack_dfx_data *dfx, uint16_t port_id); +diff --git a/src/ltran/ltran_dfx.c b/src/ltran/ltran_dfx.c +index 9180e89..761bbb7 100644 +--- a/src/ltran/ltran_dfx.c ++++ b/src/ltran/ltran_dfx.c +@@ -137,7 +137,7 @@ static void gazelle_print_lstack_xstats(void *buf, const struct gazelle_stat_msg + struct nic_eth_xstats *xstats = &stat->data.nic_xstats; + static const char *nic_stats_border = "########################"; + +- printf("###### NIC extended statistics for port %-2d #########\n", 0); ++ printf("###### NIC extended statistics for port %-2d #########\n", xstats->port_id); + printf("%s############################\n",nic_stats_border); + if (xstats->len <= 0 || xstats->len > RTE_ETH_XSTATS_MAX_LEN) { + printf("xstats item(%d) num error!\n", xstats->len); +-- +2.33.0 + diff --git a/0230-fix-client-connect-number-unbalance-on-lstack.patch b/0230-fix-client-connect-number-unbalance-on-lstack.patch new file mode 100644 index 0000000..e22e7e8 --- /dev/null +++ b/0230-fix-client-connect-number-unbalance-on-lstack.patch @@ -0,0 +1,93 @@ +From 3a1ff6aa0c9d85e3a5021fd3b6d788efe77db52a Mon Sep 17 00:00:00 2001 +From: jiangheng12 +Date: Fri, 31 Mar 2023 20:15:28 +0800 +Subject: [PATCH] fix client connect number unbalance on lstack + +--- + src/lstack/core/lstack_lwip.c | 1 - + src/lstack/core/lstack_protocol_stack.c | 8 +++++++- + src/lstack/include/lstack_protocol_stack.h | 1 + + 3 files changed, 8 insertions(+), 2 deletions(-) + +diff --git a/src/lstack/core/lstack_lwip.c b/src/lstack/core/lstack_lwip.c +index 71abbc6..ebdb469 100644 +--- a/src/lstack/core/lstack_lwip.c ++++ b/src/lstack/core/lstack_lwip.c +@@ -192,7 +192,6 @@ void gazelle_init_sock(int32_t fd) + (void)replenish_send_idlembuf(stack, sock); + + sock->stack = stack; +- sock->stack->conn_num++; + init_list_node_null(&sock->recv_list); + init_list_node_null(&sock->event_list); + } +diff --git a/src/lstack/core/lstack_protocol_stack.c b/src/lstack/core/lstack_protocol_stack.c +index 92300ef..76914f8 100644 +--- a/src/lstack/core/lstack_protocol_stack.c ++++ b/src/lstack/core/lstack_protocol_stack.c +@@ -124,6 +124,7 @@ struct protocol_stack *get_bind_protocol_stack(void) + + /* same app communication thread bind same stack */ + if (bind_stack) { ++ bind_stack->conn_num++; + return bind_stack; + } + +@@ -140,6 +141,7 @@ struct protocol_stack *get_bind_protocol_stack(void) + return NULL; + } + } else { ++ pthread_spin_lock(&stack_group->socket_lock); + for (uint16_t i = 0; i < stack_group->stack_num; i++) { + struct protocol_stack* stack = stack_group->stacks[i]; + if (get_global_cfg_params()->seperate_send_recv) { +@@ -147,7 +149,7 @@ struct protocol_stack *get_bind_protocol_stack(void) + index = i; + min_conn_num = stack->conn_num; + } +- }else { ++ } else { + if (stack->conn_num < min_conn_num) { + index = i; + min_conn_num = stack->conn_num; +@@ -156,7 +158,9 @@ struct protocol_stack *get_bind_protocol_stack(void) + } + } + ++ stack_group->stacks[index]->conn_num++; + bind_stack = stack_group->stacks[index]; ++ pthread_spin_unlock(&stack_group->socket_lock); + return stack_group->stacks[index]; + } + +@@ -541,6 +545,7 @@ int32_t init_protocol_stack(void) + + init_list_node(&stack_group->poll_list); + pthread_spin_init(&stack_group->poll_list_lock, PTHREAD_PROCESS_PRIVATE); ++ pthread_spin_init(&stack_group->socket_lock, PTHREAD_PROCESS_PRIVATE); + + + if (init_protocol_sem() != 0) { +@@ -689,6 +694,7 @@ void stack_accept(struct rpc_msg *msg) + } + + msg->result = accept_fd; ++ sock->stack->conn_num++; + if (rte_ring_count(sock->conn->recvmbox->ring)) { + add_recv_list(accept_fd); + } +diff --git a/src/lstack/include/lstack_protocol_stack.h b/src/lstack/include/lstack_protocol_stack.h +index 001ab47..c75161f 100644 +--- a/src/lstack/include/lstack_protocol_stack.h ++++ b/src/lstack/include/lstack_protocol_stack.h +@@ -103,6 +103,7 @@ struct protocol_stack_group { + /* dfx stats */ + bool latency_start; + uint64_t call_alloc_fail; ++ pthread_spinlock_t socket_lock; + }; + + long get_stack_tid(void); +-- +2.33.0 + diff --git a/0231-kni-down-not-stop-nic.patch b/0231-kni-down-not-stop-nic.patch new file mode 100644 index 0000000..21d8103 --- /dev/null +++ b/0231-kni-down-not-stop-nic.patch @@ -0,0 +1,218 @@ +From 59c658fa0f75748fa7b49170b9fc34267ef1797c Mon Sep 17 00:00:00 2001 +From: jiangheng12 +Date: Mon, 3 Apr 2023 14:42:53 +0800 +Subject: [PATCH] kni down not stop nic + +--- + src/common/dpdk_common.c | 37 +++++++++++-------------- + src/common/dpdk_common.h | 2 +- + src/lstack/core/lstack_cfg.c | 8 +++--- + src/lstack/core/lstack_protocol_stack.c | 10 ++++--- + src/lstack/netif/lstack_ethdev.c | 8 ++++-- + src/ltran/ltran_forward.c | 5 ++-- + 6 files changed, 34 insertions(+), 36 deletions(-) + +diff --git a/src/common/dpdk_common.c b/src/common/dpdk_common.c +index f5a20dd..23c96d6 100644 +--- a/src/common/dpdk_common.c ++++ b/src/common/dpdk_common.c +@@ -34,16 +34,12 @@ + #define COMMON_INFO(fmt, ...) LSTACK_LOG(INFO, LSTACK, fmt, ##__VA_ARGS__) + #endif + +-static pthread_mutex_t g_kni_mutex = PTHREAD_MUTEX_INITIALIZER; + struct rte_kni *g_pkni = NULL; ++static volatile bool g_kni_started = false; + +-/* +- * lock for preventing data race between tx thread and down operation. +- * Don't need to add lock on rx because down operation and rx are in the same thread +- */ +-pthread_mutex_t *get_kni_mutex(void) ++bool get_kni_started(void) + { +- return &g_kni_mutex; ++ return g_kni_started; + } + + struct rte_kni* get_gazelle_kni(void) +@@ -62,23 +58,18 @@ static int32_t kni_config_network_interface(uint16_t port_id, uint8_t if_up) + } + + if (if_up != 0) { /* Configure network interface up */ +- if (!g_bond_dev_started) { +- pthread_mutex_lock(&g_kni_mutex); +- ret = rte_eth_dev_start(port_id); +- pthread_mutex_unlock(&g_kni_mutex); +- if (ret < 0) { +- COMMON_ERR("Failed to start port %hu ret=%d\n", port_id, ret); ++ if (!g_kni_started) { ++ g_kni_started = true; ++ if (!g_bond_dev_started) { ++ rte_eth_dev_start(port_id); ++ g_bond_dev_started = true; + } +- g_bond_dev_started = true; + } else { + COMMON_INFO("trying to start a started dev. \n"); + } + } else { /* Configure network interface down */ +- if (g_bond_dev_started) { +- pthread_mutex_lock(&g_kni_mutex); +- rte_eth_dev_stop(port_id); +- pthread_mutex_unlock(&g_kni_mutex); +- g_bond_dev_started = false; ++ if (g_kni_started) { ++ g_kni_started = false; + } else { + COMMON_INFO("trying to stop a stopped dev. \n"); + } +@@ -201,6 +192,12 @@ void dpdk_kni_release(void) + int32_t kni_process_tx(struct rte_mbuf **pkts_burst, uint32_t count) + { + uint32_t i; ++ if (!g_kni_started) { ++ for (i = 0; i < count; i++) { ++ rte_pktmbuf_free(pkts_burst[i]); ++ } ++ return 0; ++ } + + for (i = 0; i < count; ++i) { + struct rte_ipv4_hdr * ipv4_hdr = (struct rte_ipv4_hdr *)(rte_pktmbuf_mtod(pkts_burst[i], char*) +@@ -227,9 +224,7 @@ void kni_process_rx(uint16_t port) + + nb_kni_rx = rte_kni_rx_burst(g_pkni, pkts_burst, GAZELLE_KNI_READ_SIZE); + if (nb_kni_rx > 0) { +- pthread_mutex_lock(&g_kni_mutex); + nb_rx = rte_eth_tx_burst(port, 0, pkts_burst, nb_kni_rx); +- pthread_mutex_unlock(&g_kni_mutex); + + for (i = nb_rx; i < nb_kni_rx; ++i) { + rte_pktmbuf_free(pkts_burst[i]); +diff --git a/src/common/dpdk_common.h b/src/common/dpdk_common.h +index 6b107ae..2f0e8d1 100644 +--- a/src/common/dpdk_common.h ++++ b/src/common/dpdk_common.h +@@ -94,7 +94,7 @@ static __rte_always_inline void time_stamp_into_mbuf(uint32_t rx_count, struct r + } + } + +-pthread_mutex_t *get_kni_mutex(void); ++bool get_kni_started(void); + struct rte_kni* get_gazelle_kni(void); + int32_t dpdk_kni_init(uint16_t port, struct rte_mempool *pool); + int32_t kni_process_tx(struct rte_mbuf **pkts_burst, uint32_t count); +diff --git a/src/lstack/core/lstack_cfg.c b/src/lstack/core/lstack_cfg.c +index 168aa49..8558121 100644 +--- a/src/lstack/core/lstack_cfg.c ++++ b/src/lstack/core/lstack_cfg.c +@@ -1026,6 +1026,10 @@ static int parse_process_index(void) + } + } else { + g_config_params.process_idx = (uint8_t)config_setting_get_int(process_idx); ++ if (g_config_params.is_primary && g_config_params.process_idx != 0 || ++ !g_config_params.is_primary && g_config_params.process_idx == 0) { ++ return -EINVAL; ++ } + } + + return 0; +@@ -1045,9 +1049,5 @@ static int parse_tuple_filter(void) + return -EINVAL; + } + +- // check primary process_idx +- if (g_config_params.is_primary && g_config_params.process_idx != 0) { +- return -EINVAL; +- } + return 0; + } +diff --git a/src/lstack/core/lstack_protocol_stack.c b/src/lstack/core/lstack_protocol_stack.c +index 76914f8..a858b37 100644 +--- a/src/lstack/core/lstack_protocol_stack.c ++++ b/src/lstack/core/lstack_protocol_stack.c +@@ -483,7 +483,9 @@ static void* gazelle_stack_thread(void *arg) + * so processing KNI requests only in the thread with queue_id No.0 is sufficient. */ + if (kni_switch && !queue_id && !(wakeup_tick & 0xfff)) { + rte_kni_handle_request(get_gazelle_kni()); +- kni_handle_rx(get_port_id()); ++ if (get_kni_started()) { ++ kni_handle_rx(get_port_id()); ++ } + } + + wakeup_tick++; +@@ -557,9 +559,9 @@ int32_t init_protocol_stack(void) + + if (get_global_cfg_params()->is_primary) { + for (uint16_t idx = 0; idx < get_global_cfg_params()->tot_queue_num; idx++) { +- struct rte_mempool* rxtx_mbuf = create_pktmbuf_mempool("rxtx_mbuf", +- get_global_cfg_params()->mbuf_count_per_conn * get_global_cfg_params()->tcp_conn_count / stack_group->stack_num, RXTX_CACHE_SZ, idx); +- get_protocol_stack_group()->total_rxtx_pktmbuf_pool[idx] = rxtx_mbuf; ++ struct rte_mempool* rxtx_mbuf = create_pktmbuf_mempool("rxtx_mbuf", ++ get_global_cfg_params()->mbuf_count_per_conn * get_global_cfg_params()->tcp_conn_count / stack_group->stack_num, RXTX_CACHE_SZ, idx); ++ get_protocol_stack_group()->total_rxtx_pktmbuf_pool[idx] = rxtx_mbuf; + } + } + +diff --git a/src/lstack/netif/lstack_ethdev.c b/src/lstack/netif/lstack_ethdev.c +index e26fe30..4103f22 100644 +--- a/src/lstack/netif/lstack_ethdev.c ++++ b/src/lstack/netif/lstack_ethdev.c +@@ -719,7 +719,9 @@ void kni_handle_rx(uint16_t port_id) + + void kni_handle_tx(struct rte_mbuf *mbuf) + { +- if (!get_global_cfg_params()->kni_switch) { ++ if (!get_global_cfg_params()->kni_switch || ++ !get_kni_started()) { ++ rte_pktmbuf_free(mbuf); + return; + } + struct rte_ipv4_hdr *ipv4_hdr; +@@ -776,9 +778,9 @@ int32_t gazelle_eth_dev_poll(struct protocol_stack *stack, uint8_t use_ltran_fla + if (likely(transfer_type == TRANSFER_CURRENT_THREAD)) { + eth_dev_recv(stack->pkts[i], stack); + +- }else if (transfer_type == TRANSFER_KERNEL) { ++ } else if (transfer_type == TRANSFER_KERNEL) { + kni_handle_tx(stack->pkts[i]); +- }else { ++ } else { + /*transfer to other thread*/ + } + } +diff --git a/src/ltran/ltran_forward.c b/src/ltran/ltran_forward.c +index 8629acb..b41e1e2 100644 +--- a/src/ltran/ltran_forward.c ++++ b/src/ltran/ltran_forward.c +@@ -690,7 +690,6 @@ static __rte_always_inline void downstream_forward_one(struct gazelle_stack *sta + /* send packets anyway. */ + tx_pkts = 0; + +- pthread_mutex_lock(get_kni_mutex()); + while (tx_pkts < used_cnt) { + tx_pkts += rte_eth_tx_burst(port_id, queue_id, + (struct rte_mbuf **)(&dst_bufs[tx_pkts]), +@@ -702,7 +701,6 @@ static __rte_always_inline void downstream_forward_one(struct gazelle_stack *sta + } + } + } +- pthread_mutex_unlock(get_kni_mutex()); + + get_statistics()->port_stats[g_port_index].tx_bytes += tx_bytes; + get_statistics()->port_stats[g_port_index].tx += tx_pkts; +@@ -737,7 +735,8 @@ int32_t downstream_forward(uint16_t *port) + + while (get_ltran_stop_flag() != GAZELLE_TRUE) { + /* kni rx means read from kni and send to nic */ +- if (get_ltran_config()->dpdk.kni_switch == GAZELLE_ON) { ++ if (get_ltran_config()->dpdk.kni_switch == GAZELLE_ON && ++ get_kni_started()) { + kni_process_rx(g_port_index); + } + +-- +2.33.0 + diff --git a/0232-fix-rpc-msg-alloc-failed.patch b/0232-fix-rpc-msg-alloc-failed.patch new file mode 100644 index 0000000..f4c3855 --- /dev/null +++ b/0232-fix-rpc-msg-alloc-failed.patch @@ -0,0 +1,79 @@ +From 9ae296b6fbda03781799251ba8801e166cded656 Mon Sep 17 00:00:00 2001 +From: jiangheng12 +Date: Thu, 6 Apr 2023 19:42:59 +0800 +Subject: [PATCH] fix rpc msg alloc failed fix process_numa args error coredump + fix sock->conn not free when fd is kernel mode + +--- + src/lstack/api/lstack_wrap.c | 9 ++++++++- + src/lstack/core/lstack_cfg.c | 4 ++-- + src/lstack/core/lstack_protocol_stack.c | 4 ++++ + src/lstack/include/lstack_thread_rpc.h | 2 +- + 4 files changed, 15 insertions(+), 4 deletions(-) + +diff --git a/src/lstack/api/lstack_wrap.c b/src/lstack/api/lstack_wrap.c +index b8a6590..90b2252 100644 +--- a/src/lstack/api/lstack_wrap.c ++++ b/src/lstack/api/lstack_wrap.c +@@ -528,7 +528,14 @@ static inline int32_t do_close(int32_t s) + { + struct lwip_sock *sock = NULL; + if (select_path(s, &sock) == PATH_KERNEL) { +- return posix_api->close_fn(s); ++ /* we called lwip_socket, even if kernel fd */ ++ if (posix_api != NULL && !posix_api->ues_posix && ++ /* contain posix_api->close_fn if success */ ++ stack_broadcast_close(s) == 0) { ++ return 0; ++ } else { ++ return posix_api->close_fn(s); ++ } + } + if (sock && sock->wakeup && sock->wakeup->epollfd == s) { + return lstack_epoll_close(s); +diff --git a/src/lstack/core/lstack_cfg.c b/src/lstack/core/lstack_cfg.c +index 8558121..9430dd7 100644 +--- a/src/lstack/core/lstack_cfg.c ++++ b/src/lstack/core/lstack_cfg.c +@@ -998,8 +998,8 @@ static int32_t parse_process_numa(void) + return 0; + + args = config_setting_get_string(cfg_args); +- if (cfg_args == NULL) { +- return 0; ++ if (args == NULL) { ++ return -EINVAL; + } + + ret = separate_str_to_array((char *)args, g_config_params.process_numa, PROTOCOL_STACK_MAX, GAZELLE_MAX_NUMA_NODES); +diff --git a/src/lstack/core/lstack_protocol_stack.c b/src/lstack/core/lstack_protocol_stack.c +index a858b37..061582f 100644 +--- a/src/lstack/core/lstack_protocol_stack.c ++++ b/src/lstack/core/lstack_protocol_stack.c +@@ -819,6 +819,10 @@ int32_t stack_broadcast_close(int32_t fd) + struct lwip_sock *sock = get_socket(fd); + int32_t ret = 0; + ++ if (sock == NULL) { ++ return -1; ++ } ++ + do { + sock = sock->listen_next; + if (rpc_call_close(fd)) { +diff --git a/src/lstack/include/lstack_thread_rpc.h b/src/lstack/include/lstack_thread_rpc.h +index ed111fb..657ffa9 100644 +--- a/src/lstack/include/lstack_thread_rpc.h ++++ b/src/lstack/include/lstack_thread_rpc.h +@@ -25,7 +25,7 @@ + #define MSG_ARG_4 (4) + #define RPM_MSG_ARG_SIZE (5) + +-#define RPC_MSG_MAX 512 ++#define RPC_MSG_MAX 2048 + #define RPC_MSG_MASK (RPC_MSG_MAX - 1) + + struct rpc_msg; +-- +2.33.0 + diff --git a/0233-add-parentheses-to-fix-build-error.patch b/0233-add-parentheses-to-fix-build-error.patch new file mode 100644 index 0000000..7e54f36 --- /dev/null +++ b/0233-add-parentheses-to-fix-build-error.patch @@ -0,0 +1,27 @@ +From ace78c329b02e69162c1ec4b9b3260d276694ece Mon Sep 17 00:00:00 2001 +From: jiangheng12 +Date: Fri, 7 Apr 2023 09:42:39 +0800 +Subject: [PATCH] add parentheses to fix build error + +--- + src/lstack/core/lstack_cfg.c | 4 ++-- + 1 file changed, 2 insertions(+), 2 deletions(-) + +diff --git a/src/lstack/core/lstack_cfg.c b/src/lstack/core/lstack_cfg.c +index 9430dd7..394cb13 100644 +--- a/src/lstack/core/lstack_cfg.c ++++ b/src/lstack/core/lstack_cfg.c +@@ -1026,8 +1026,8 @@ static int parse_process_index(void) + } + } else { + g_config_params.process_idx = (uint8_t)config_setting_get_int(process_idx); +- if (g_config_params.is_primary && g_config_params.process_idx != 0 || +- !g_config_params.is_primary && g_config_params.process_idx == 0) { ++ if ((g_config_params.is_primary && g_config_params.process_idx != 0) || ++ (!g_config_params.is_primary && g_config_params.process_idx == 0)) { + return -EINVAL; + } + } +-- +2.33.0 + diff --git a/0234-fix-bond_ports-parse-error.patch b/0234-fix-bond_ports-parse-error.patch new file mode 100644 index 0000000..34fb362 --- /dev/null +++ b/0234-fix-bond_ports-parse-error.patch @@ -0,0 +1,446 @@ +From 2188bdbdfbe6ebe2bc091c87bfd9932747a7e9a4 Mon Sep 17 00:00:00 2001 +From: jiangheng12 +Date: Fri, 7 Apr 2023 14:28:53 +0800 +Subject: [PATCH] fix bond_ports parse error fix socket_mem parse error when + the value exceeds 65536 exit if create_rxtx_pktmbuf failed build.sh build + failed return 1 clean code + +--- + build/build.sh | 8 +++ + src/common/gazelle_opt.h | 2 - + src/common/gazelle_parse_config.c | 17 +++--- + src/lstack/api/lstack_wrap.c | 8 +-- + src/lstack/core/lstack_cfg.c | 3 +- + src/lstack/core/lstack_init.c | 3 +- + src/lstack/core/lstack_lwip.c | 6 +-- + src/lstack/core/lstack_protocol_stack.c | 20 ++++--- + src/lstack/netif/lstack_ethdev.c | 4 +- + src/ltran/ltran_dfx.c | 14 ++--- + src/ltran/ltran_param.c | 69 +++++++++++++------------ + 11 files changed, 86 insertions(+), 68 deletions(-) + +diff --git a/build/build.sh b/build/build.sh +index ab25d88..4464f8c 100755 +--- a/build/build.sh ++++ b/build/build.sh +@@ -14,6 +14,10 @@ cd $CURRENT_PATH + + make -C ../src/lstack clean + make -C ../src/lstack ++if [ $? -ne 0 ]; then ++ echo "build lstack failed" ++ exit 1 ++fi + + cd ../src/ltran + rm -f CMakeCache.txt +@@ -21,5 +25,9 @@ rm -f ltran gazellectl + rm -rf CMakeFiles + cmake . + make ++if [ $? -ne 0 ]; then ++ echo "build ltran failed" ++ exit 1 ++fi + + cd - +diff --git a/src/common/gazelle_opt.h b/src/common/gazelle_opt.h +index 4420ba4..fe0483b 100644 +--- a/src/common/gazelle_opt.h ++++ b/src/common/gazelle_opt.h +@@ -88,8 +88,6 @@ + #define GAZELLE_PRIMARY_START_PATH "/var/run/gazelle/gazelle_primary" + #define GAZELLE_FILE_PERMISSION 0700 + +-#define GAZELLE_MAX_PORTS_VALUE (UINT16_MAX - 1) +- + #define SEND_TIME_WAIT_NS 20000 + #define SECOND_NSECOND 1000000000 + +diff --git a/src/common/gazelle_parse_config.c b/src/common/gazelle_parse_config.c +index a2bdde1..ce6a3f8 100644 +--- a/src/common/gazelle_parse_config.c ++++ b/src/common/gazelle_parse_config.c +@@ -55,7 +55,12 @@ int32_t separate_str_to_array(char *args, uint32_t *array, int32_t array_size, i + return -1; + } + errno = 0; +- idx = strtol(args, &end, 10); /* 10: decimal */ ++ /* prefix 0x,0X indicate hexdecimal */ ++ if (strncmp(args, "0x", 2) == 0 || strncmp(args, "0X", 2) == 0) { ++ idx = strtol(args, &end, 16); /* 16: hexdecimal */ ++ } else { ++ idx = strtol(args, &end, 10); /* 10: decimal */ ++ } + if (errno || end == NULL) { + return -1; + } +@@ -107,18 +112,18 @@ int32_t check_and_set_run_dir(void) + int32_t filename_check(const char* args) + { + if (args == NULL) { +- return 1; ++ return 1; + } + + if (strlen(args) <= 0 || strlen(args) > GAZELLE_SOCK_FILENAME_MAXLEN - 1) { +- COMMON_ERR("socket_filename_check: invalid unix sock name %s, filename exceeds the limit %d.\n", args, GAZELLE_SOCK_FILENAME_MAXLEN); +- return 1; ++ COMMON_ERR("socket_filename_check: invalid unix sock name %s, filename exceeds the limit %d.\n", args, GAZELLE_SOCK_FILENAME_MAXLEN); ++ return 1; + } + + char* sensitive_chars = strpbrk(args, "|;&$><`\\!\n"); + if (sensitive_chars != NULL) { +- COMMON_ERR("socket_filename_check: invalid unix sock name %s, filename contains sensitive characters.\n", args); +- return 1; ++ COMMON_ERR("socket_filename_check: invalid unix sock name %s, filename contains sensitive characters.\n", args); ++ return 1; + } + + return 0; +diff --git a/src/lstack/api/lstack_wrap.c b/src/lstack/api/lstack_wrap.c +index 90b2252..1b12822 100644 +--- a/src/lstack/api/lstack_wrap.c ++++ b/src/lstack/api/lstack_wrap.c +@@ -205,7 +205,7 @@ static int get_addr(struct sockaddr_in *sin, char *interface) + } + posix_api->close_fn(sockfd); + +- memcpy(sin, &ifr.ifr_addr, sizeof(struct sockaddr_in)); ++ memcpy_s(sin, sizeof(struct sockaddr_in), &ifr.ifr_addr, sizeof(struct sockaddr_in)); + + return 0; + } +@@ -250,8 +250,8 @@ bool is_dst_ip_localhost(const struct sockaddr *addr) + ++p; + int n = strcspn(p, ": \t"); + +- char interface[20] = {0}; +- strncpy(interface, p, n); ++ char interface[20] = {0}; /* 20: nic name len */ ++ strncpy_s(interface, sizeof(interface), p, n); + + memset_s(sin, sizeof(struct sockaddr_in), 0, sizeof(struct sockaddr_in)); + int ret = get_addr(sin, interface); +@@ -289,7 +289,7 @@ static int32_t do_connect(int32_t s, const struct sockaddr *name, socklen_t name + int32_t ret = 0; + char listen_ring_name[RING_NAME_LEN]; + int remote_port = htons(((struct sockaddr_in *)name)->sin_port); +- snprintf_s(listen_ring_name, sizeof(listen_ring_name), sizeof(listen_ring_name) - 1, "listen_rx_ring_%u", remote_port); ++ snprintf_s(listen_ring_name, sizeof(listen_ring_name), sizeof(listen_ring_name) - 1, "listen_rx_ring_%d", remote_port); + if (is_dst_ip_localhost(name) && rte_ring_lookup(listen_ring_name) == NULL) { + ret = posix_api->connect_fn(s, name, namelen); + SET_CONN_TYPE_HOST(sock->conn); +diff --git a/src/lstack/core/lstack_cfg.c b/src/lstack/core/lstack_cfg.c +index 394cb13..cdb0200 100644 +--- a/src/lstack/core/lstack_cfg.c ++++ b/src/lstack/core/lstack_cfg.c +@@ -492,7 +492,8 @@ static int32_t gazelle_parse_socket_mem(const char *arg, struct secondary_attach + return -1; + } + +- int32_t count = separate_str_to_array(socket_mem, sec_attach_arg->socket_per_size, GAZELLE_MAX_NUMA_NODES, GAZELLE_MAX_PORTS_VALUE); ++ int32_t count = separate_str_to_array(socket_mem, sec_attach_arg->socket_per_size, ++ GAZELLE_MAX_NUMA_NODES, INT32_MAX); + + if (count < 0) { + return -1; +diff --git a/src/lstack/core/lstack_init.c b/src/lstack/core/lstack_init.c +index 76dd384..f17e4d2 100644 +--- a/src/lstack/core/lstack_init.c ++++ b/src/lstack/core/lstack_init.c +@@ -240,7 +240,8 @@ static void gazelle_signal_init(void) + lstack_signal_init(); + } + +-static void set_kni_ip_mac() { ++static void set_kni_ip_mac() ++{ + struct cfg_params *cfg = get_global_cfg_params(); + + int32_t fd = posix_api->socket_fn(AF_INET, SOCK_DGRAM, IPPROTO_IP); +diff --git a/src/lstack/core/lstack_lwip.c b/src/lstack/core/lstack_lwip.c +index ebdb469..0535428 100644 +--- a/src/lstack/core/lstack_lwip.c ++++ b/src/lstack/core/lstack_lwip.c +@@ -1392,7 +1392,7 @@ err_t netif_loop_output(struct netif *netif, struct pbuf *p) + return ERR_MEM; + } + head->ol_flags = p->ol_flags; +- memcpy(head->payload, p->payload, p->len); ++ memcpy_s(head->payload, head->len, p->payload, p->len); + + if ((flags & TCP_SYN) && !(flags & TCP_ACK)) { + /* SYN packet, send to listen_ring */ +@@ -1461,7 +1461,7 @@ err_t find_same_node_memzone(struct tcp_pcb *pcb, struct lwip_sock *nsock) + err_t same_node_memzone_create(const struct rte_memzone **zone, int size, int port, char *name, char *rx) + { + char mem_name[RING_NAME_LEN] = {0}; +- snprintf_s(mem_name, sizeof(mem_name), sizeof(mem_name) - 1, "%s_%s_%u", name, rx, port); ++ snprintf_s(mem_name, sizeof(mem_name), sizeof(mem_name) - 1, "%s_%s_%d", name, rx, port); + + *zone = rte_memzone_reserve_aligned(mem_name, size, rte_socket_id(), 0, RTE_CACHE_LINE_SIZE); + if (*zone == NULL) { +@@ -1484,7 +1484,7 @@ err_t same_node_ring_create(struct rte_ring **ring, int size, int port, char *na + flags = RING_F_SP_ENQ | RING_F_SC_DEQ; + } + +- snprintf_s(ring_name, sizeof(ring_name), sizeof(ring_name) - 1, "%s_%s_ring_%u", name, rx, port); ++ snprintf_s(ring_name, sizeof(ring_name), sizeof(ring_name) - 1, "%s_%s_ring_%d", name, rx, port); + *ring = rte_ring_create(ring_name, size, rte_socket_id(), flags); + if (*ring == NULL) { + LSTACK_LOG(ERR, LSTACK, "cannot create rte_ring %s, errno is %d\n", ring_name, rte_errno); +diff --git a/src/lstack/core/lstack_protocol_stack.c b/src/lstack/core/lstack_protocol_stack.c +index 061582f..7b1b994 100644 +--- a/src/lstack/core/lstack_protocol_stack.c ++++ b/src/lstack/core/lstack_protocol_stack.c +@@ -81,7 +81,8 @@ struct protocol_stack_group *get_protocol_stack_group(void) + return &g_stack_group; + } + +-int get_min_conn_stack(struct protocol_stack_group *stack_group){ ++int get_min_conn_stack(struct protocol_stack_group *stack_group) ++{ + int min_conn_stk_idx = 0; + int min_conn_num = GAZELLE_MAX_CLIENTS; + for (int i = 0; i < stack_group->stack_num; i++) { +@@ -91,7 +92,7 @@ int get_min_conn_stack(struct protocol_stack_group *stack_group){ + min_conn_stk_idx = i; + min_conn_num = stack->conn_num; + } +- }else { ++ } else { + if (stack->conn_num < min_conn_num) { + min_conn_stk_idx = i; + min_conn_num = stack->conn_num; +@@ -100,7 +101,6 @@ int get_min_conn_stack(struct protocol_stack_group *stack_group){ + + } + return min_conn_stk_idx; +- + } + + struct protocol_stack *get_protocol_stack(void) +@@ -230,14 +230,14 @@ static int32_t create_thread(void *arg, char *thread_name, stack_thread_func fun + return -1; + } + +- if (get_global_cfg_params()->seperate_send_recv){ ++ if (get_global_cfg_params()->seperate_send_recv) { + ret = sprintf_s(name, sizeof(name), "%s", thread_name); + if (ret < 0) { + LSTACK_LOG(ERR, LSTACK, "set name failed\n"); + return -1; + } + +- }else { ++ } else { + ret = sprintf_s(name, sizeof(name), "%s%02hu", thread_name, t_params->queue_id); + if (ret < 0) { + LSTACK_LOG(ERR, LSTACK, "set name failed\n"); +@@ -500,7 +500,8 @@ static void* gazelle_stack_thread(void *arg) + return NULL; + } + +-static void libnet_listen_thread(void *arg){ ++static void libnet_listen_thread(void *arg) ++{ + struct cfg_params * cfg_param = get_global_cfg_params(); + recv_pkts_from_other_process(cfg_param->process_idx, arg); + } +@@ -561,6 +562,9 @@ int32_t init_protocol_stack(void) + for (uint16_t idx = 0; idx < get_global_cfg_params()->tot_queue_num; idx++) { + struct rte_mempool* rxtx_mbuf = create_pktmbuf_mempool("rxtx_mbuf", + get_global_cfg_params()->mbuf_count_per_conn * get_global_cfg_params()->tcp_conn_count / stack_group->stack_num, RXTX_CACHE_SZ, idx); ++ if (rxtx_mbuf == NULL) { ++ return -1; ++ } + get_protocol_stack_group()->total_rxtx_pktmbuf_pool[idx] = rxtx_mbuf; + } + } +@@ -572,13 +576,13 @@ int32_t init_protocol_stack(void) + if (ret < 0) { + return -1; + } +- }else { ++ } else { + ret = sprintf_s(name, sizeof(name), "%s_%d_%d", LSTACK_SEND_THREAD_NAME, process_index, i/2); + if (ret < 0) { + return -1; + } + } +- }else { ++ } else { + ret = sprintf_s(name, sizeof(name), "%s", LSTACK_THREAD_NAME); + if (ret < 0) { + return -1; +diff --git a/src/lstack/netif/lstack_ethdev.c b/src/lstack/netif/lstack_ethdev.c +index 4103f22..0a91f79 100644 +--- a/src/lstack/netif/lstack_ethdev.c ++++ b/src/lstack/netif/lstack_ethdev.c +@@ -323,7 +323,7 @@ void config_flow_director(uint16_t queue_id, uint32_t src_ip, uint32_t dst_ip, u + + uint16_t port_id = get_port_id(); + char rule_key[RULE_KEY_LEN] = {0}; +- sprintf(rule_key,"%u_%u_%u",src_ip,src_port,dst_port); ++ sprintf_s(rule_key, sizeof(rule_key), "%u_%u_%u", src_ip, src_port, dst_port); + struct flow_rule *fl_exist = find_rule(rule_key); + if(fl_exist != NULL){ + return; +@@ -347,7 +347,7 @@ void delete_flow_director(uint32_t dst_ip, uint16_t src_port, uint16_t dst_port) + { + uint16_t port_id = get_port_id(); + char rule_key[RULE_KEY_LEN] = {0}; +- sprintf(rule_key,"%u_%u_%u",dst_ip,dst_port,src_port); ++ sprintf_s(rule_key, RULE_KEY_LEN, "%u_%u_%u",dst_ip, dst_port, src_port); + struct flow_rule *fl = find_rule(rule_key); + + if(fl != NULL){ +diff --git a/src/ltran/ltran_dfx.c b/src/ltran/ltran_dfx.c +index 761bbb7..3513125 100644 +--- a/src/ltran/ltran_dfx.c ++++ b/src/ltran/ltran_dfx.c +@@ -209,17 +209,17 @@ static int32_t dfx_connect_ltran(bool use_ltran, bool probe) + } + + ret = strncpy_s(addr.sun_path, sizeof(addr.sun_path), GAZELLE_RUN_DIR, +- strlen(GAZELLE_RUN_DIR) + 1); ++ strlen(GAZELLE_RUN_DIR) + 1); + if (ret != EOK) { +- printf("%s:%d strncpy_s fail ret=%d\n", __FUNCTION__, __LINE__, ret); ++ printf("%s:%d strncpy_s fail ret=%d\n", __FUNCTION__, __LINE__, ret); + } + + if (g_unix_prefix) { +- ret = strncat_s(addr.sun_path, sizeof(addr.sun_path), g_unix_prefix, +- strlen(g_unix_prefix) + 1); +- if (ret != EOK) { +- printf("%s:%d strncat_s fail ret=%d\n", __FUNCTION__, __LINE__, ret); +- } ++ ret = strncat_s(addr.sun_path, sizeof(addr.sun_path), g_unix_prefix, ++ strlen(g_unix_prefix) + 1); ++ if (ret != EOK) { ++ printf("%s:%d strncat_s fail ret=%d\n", __FUNCTION__, __LINE__, ret); ++ } + } + + addr.sun_family = AF_UNIX; +diff --git a/src/ltran/ltran_param.c b/src/ltran/ltran_param.c +index 7cf3423..1eb4f48 100644 +--- a/src/ltran/ltran_param.c ++++ b/src/ltran/ltran_param.c +@@ -339,7 +339,8 @@ static int32_t parse_bond_ports(const config_t *config, const char *key, struct + return GAZELLE_ERR; + } + +- ltran_config->bond.port_num = separate_str_to_array(port_str, ltran_config->bond.portmask, GAZELLE_MAX_BOND_NUM, GAZELLE_MAX_PORTS_VALUE); ++ ltran_config->bond.port_num = separate_str_to_array(port_str, ltran_config->bond.portmask, ++ GAZELLE_MAX_BOND_NUM, UINT16_MAX); + + if (ltran_config->bond.port_num > GAZELLE_MAX_BOND_NUM) { + free(port_str); +@@ -547,67 +548,67 @@ static int32_t parse_unix_prefix(const config_t *config, const char *key, struct + int32_t ret = 0; + + ret = memset_s(ltran_config->unix_socket_filename, sizeof(ltran_config->unix_socket_filename), +- 0, sizeof(ltran_config->unix_socket_filename)); ++ 0, sizeof(ltran_config->unix_socket_filename)); + if (ret != EOK) { +- gazelle_set_errno(GAZELLE_EINETATON); +- return GAZELLE_ERR; ++ gazelle_set_errno(GAZELLE_EINETATON); ++ return GAZELLE_ERR; + } + + ret = memset_s(ltran_config->dfx_socket_filename, sizeof(ltran_config->dfx_socket_filename), +- 0, sizeof(ltran_config->dfx_socket_filename)); ++ 0, sizeof(ltran_config->dfx_socket_filename)); + if (ret != EOK) { +- gazelle_set_errno(GAZELLE_EINETATON); +- return GAZELLE_ERR; ++ gazelle_set_errno(GAZELLE_EINETATON); ++ return GAZELLE_ERR; + } + + ret = strncpy_s(ltran_config->unix_socket_filename, sizeof(ltran_config->unix_socket_filename), +- GAZELLE_RUN_DIR, strlen(GAZELLE_RUN_DIR) + 1); ++ GAZELLE_RUN_DIR, strlen(GAZELLE_RUN_DIR) + 1); + if (ret != EOK) { +- gazelle_set_errno(GAZELLE_EINETATON); +- return GAZELLE_ERR; ++ gazelle_set_errno(GAZELLE_EINETATON); ++ return GAZELLE_ERR; + } + + ret = strncpy_s(ltran_config->dfx_socket_filename, sizeof(ltran_config->dfx_socket_filename), +- GAZELLE_RUN_DIR, strlen(GAZELLE_RUN_DIR) + 1); ++ GAZELLE_RUN_DIR, strlen(GAZELLE_RUN_DIR) + 1); + if (ret != EOK) { +- gazelle_set_errno(GAZELLE_EINETATON); +- return GAZELLE_ERR; ++ gazelle_set_errno(GAZELLE_EINETATON); ++ return GAZELLE_ERR; + } + + ret = config_lookup_string(config, key, &prefix); + if (ret) { + if (filename_check(prefix)) { +- gazelle_set_errno(GAZELLE_EINETATON); +- return GAZELLE_ERR; +- } ++ gazelle_set_errno(GAZELLE_EINETATON); ++ return GAZELLE_ERR; ++ } + +- ret = strncat_s(ltran_config->unix_socket_filename, sizeof(ltran_config->unix_socket_filename), +- prefix, strlen(prefix) + 1); +- if (ret != EOK) { +- gazelle_set_errno(GAZELLE_EINETATON); +- return GAZELLE_ERR; +- } ++ ret = strncat_s(ltran_config->unix_socket_filename, sizeof(ltran_config->unix_socket_filename), ++ prefix, strlen(prefix) + 1); ++ if (ret != EOK) { ++ gazelle_set_errno(GAZELLE_EINETATON); ++ return GAZELLE_ERR; ++ } + +- ret = strncat_s(ltran_config->dfx_socket_filename, sizeof(ltran_config->dfx_socket_filename), +- prefix, strlen(prefix) + 1); +- if (ret != EOK) { +- gazelle_set_errno(GAZELLE_EINETATON); +- return GAZELLE_ERR; +- } ++ ret = strncat_s(ltran_config->dfx_socket_filename, sizeof(ltran_config->dfx_socket_filename), ++ prefix, strlen(prefix) + 1); ++ if (ret != EOK) { ++ gazelle_set_errno(GAZELLE_EINETATON); ++ return GAZELLE_ERR; ++ } + } + + ret = strncat_s(ltran_config->unix_socket_filename, sizeof(ltran_config->unix_socket_filename), +- GAZELLE_REG_SOCK_FILENAME, strlen(GAZELLE_REG_SOCK_FILENAME) + 1); ++ GAZELLE_REG_SOCK_FILENAME, strlen(GAZELLE_REG_SOCK_FILENAME) + 1); + if (ret != EOK) { +- gazelle_set_errno(GAZELLE_EINETATON); +- return GAZELLE_ERR; ++ gazelle_set_errno(GAZELLE_EINETATON); ++ return GAZELLE_ERR; + } + + ret = strncat_s(ltran_config->dfx_socket_filename, sizeof(ltran_config->dfx_socket_filename), +- GAZELLE_DFX_SOCK_FILENAME, strlen(GAZELLE_DFX_SOCK_FILENAME) + 1); ++ GAZELLE_DFX_SOCK_FILENAME, strlen(GAZELLE_DFX_SOCK_FILENAME) + 1); + if (ret != EOK) { +- gazelle_set_errno(GAZELLE_EINETATON); +- return GAZELLE_ERR; ++ gazelle_set_errno(GAZELLE_EINETATON); ++ return GAZELLE_ERR; + } + + return GAZELLE_OK; +-- +2.33.0 + diff --git a/0235-dfx-set-g_unix_fd-to-1-after-abnormal-close-fd-to-av.patch b/0235-dfx-set-g_unix_fd-to-1-after-abnormal-close-fd-to-av.patch new file mode 100644 index 0000000..7859eba --- /dev/null +++ b/0235-dfx-set-g_unix_fd-to-1-after-abnormal-close-fd-to-av.patch @@ -0,0 +1,60 @@ +From 1cdbc7c47200b9ffc1911656567b48f53f4107b8 Mon Sep 17 00:00:00 2001 +From: jiangheng12 +Date: Wed, 26 Apr 2023 19:34:58 +0800 +Subject: [PATCH] dfx: set g_unix_fd to -1 after abnormal close fd to avoid + double close + +--- + src/ltran/ltran_dfx.c | 22 ++++++++++++---------- + 1 file changed, 12 insertions(+), 10 deletions(-) + +diff --git a/src/ltran/ltran_dfx.c b/src/ltran/ltran_dfx.c +index 3513125..4baca26 100644 +--- a/src/ltran/ltran_dfx.c ++++ b/src/ltran/ltran_dfx.c +@@ -289,6 +289,7 @@ static int32_t dfx_stat_read_from_ltran(char *buf, uint32_t len, enum GAZELLE_ST + dfx = find_dfx_node(mode); + if (dfx == NULL) { + close(fd); ++ g_unix_fd = -1; + return GAZELLE_ERR; + } + +@@ -297,6 +298,7 @@ static int32_t dfx_stat_read_from_ltran(char *buf, uint32_t len, enum GAZELLE_ST + if (ret == -1) { + printf("read stat response msg failed ret=%d\n", ret); + close(fd); ++ g_unix_fd = -1; + return GAZELLE_ERR; + } + } +@@ -1368,16 +1370,16 @@ int32_t main(int32_t argc, char *argv[]) + + int unix_arg = 0; + for (int32_t i = 1; i < argc; i++) { +- if (unix_arg == 0) { +- if (!strcmp(argv[i], "-u")) { +- unix_arg++; +- } +- } else if (unix_arg == 1) { +- g_unix_prefix = argv[i]; +- unix_arg++; +- } else { +- argv[i - unix_arg] = argv[i]; +- } ++ if (unix_arg == 0) { ++ if (!strcmp(argv[i], "-u")) { ++ unix_arg++; ++ } ++ } else if (unix_arg == 1) { ++ g_unix_prefix = argv[i]; ++ unix_arg++; ++ } else { ++ argv[i - unix_arg] = argv[i]; ++ } + } + + argv[argc - unix_arg] = argv[argc]; +-- +2.33.0 + diff --git a/0236-dfx-security-function-failed-return-error-directly.patch b/0236-dfx-security-function-failed-return-error-directly.patch new file mode 100644 index 0000000..473f39c --- /dev/null +++ b/0236-dfx-security-function-failed-return-error-directly.patch @@ -0,0 +1,63 @@ +From b3b492c64bab47d4bb898422fb9a57a2536e0d61 Mon Sep 17 00:00:00 2001 +From: jiangheng12 +Date: Wed, 26 Apr 2023 20:06:42 +0800 +Subject: [PATCH] dfx: security function failed, return error directly + +--- + src/ltran/ltran_dfx.c | 10 ++++++++-- + 1 file changed, 8 insertions(+), 2 deletions(-) + +diff --git a/src/ltran/ltran_dfx.c b/src/ltran/ltran_dfx.c +index 4baca26..944675e 100644 +--- a/src/ltran/ltran_dfx.c ++++ b/src/ltran/ltran_dfx.c +@@ -206,12 +206,14 @@ static int32_t dfx_connect_ltran(bool use_ltran, bool probe) + ret = memset_s(&addr, sizeof(addr), 0, sizeof(struct sockaddr_un)); + if (ret != EOK) { + printf("%s:%d memset_s fail ret=%d\n", __FUNCTION__, __LINE__, ret); ++ goto END; + } + + ret = strncpy_s(addr.sun_path, sizeof(addr.sun_path), GAZELLE_RUN_DIR, + strlen(GAZELLE_RUN_DIR) + 1); + if (ret != EOK) { + printf("%s:%d strncpy_s fail ret=%d\n", __FUNCTION__, __LINE__, ret); ++ goto END; + } + + if (g_unix_prefix) { +@@ -219,6 +221,7 @@ static int32_t dfx_connect_ltran(bool use_ltran, bool probe) + strlen(g_unix_prefix) + 1); + if (ret != EOK) { + printf("%s:%d strncat_s fail ret=%d\n", __FUNCTION__, __LINE__, ret); ++ goto END; + } + } + +@@ -234,6 +237,7 @@ static int32_t dfx_connect_ltran(bool use_ltran, bool probe) + strlen(GAZELLE_REG_SOCK_FILENAME) + 1); + if (ret != EOK) { + printf("%s:%d strncat_s fail ret=%d\n", __FUNCTION__, __LINE__, ret); ++ goto END; + } + } + +@@ -243,11 +247,13 @@ static int32_t dfx_connect_ltran(bool use_ltran, bool probe) + printf("connect ltran failed. errno: %d ret=%d\n", errno, ret); + printf("You may need to use the -u parameter to specify the UNIX_PREFIX that matches the configuration.\n"); + } +- close(fd); +- return GAZELLE_ERR; ++ goto END; + } + + return fd; ++END: ++ close(fd); ++ return GAZELLE_ERR; + } + + static int32_t dfx_stat_conn_to_ltran(struct gazelle_stat_msg_request *req_msg) +-- +2.33.0 + diff --git a/0237-add-bond4-suport.patch b/0237-add-bond4-suport.patch new file mode 100644 index 0000000..4a50ea5 --- /dev/null +++ b/0237-add-bond4-suport.patch @@ -0,0 +1,277 @@ +From 97e31b514dde519ac5b83a3b7519c98d9805d746 Mon Sep 17 00:00:00 2001 +From: compile_success <980965867@qq.com> +Date: Fri, 5 May 2023 11:18:59 +0000 +Subject: [PATCH] add bond4 suport + +--- + src/lstack/Makefile | 3 +- + src/lstack/core/lstack_cfg.c | 55 +++++++++++++++++++ + src/lstack/core/lstack_dpdk.c | 90 +++++++++++++++++++++++++++++--- + src/lstack/include/lstack_cfg.h | 3 ++ + src/lstack/include/lstack_dpdk.h | 2 +- + 5 files changed, 143 insertions(+), 10 deletions(-) + +diff --git a/src/lstack/Makefile b/src/lstack/Makefile +index 9fb1f4c..ab039ec 100644 +--- a/src/lstack/Makefile ++++ b/src/lstack/Makefile +@@ -85,7 +85,8 @@ LIBRTE_LIB = $(LIB_PATH)/librte_bus_pci.so \ + $(LIB_PATH)/librte_pcapng.so \ + $(LIB_PATH)/librte_security.so \ + $(LIB_PATH)/librte_cryptodev.so \ +- $(LIB_PATH)/librte_net_pcap.so ++ $(LIB_PATH)/librte_net_pcap.so \ ++ $(LIB_PATH)/librte_net_bond.so + + + DEP_LIBS = $(LWIP_LIB) $(LIBRTE_LIB) +diff --git a/src/lstack/core/lstack_cfg.c b/src/lstack/core/lstack_cfg.c +index cdb0200..c4919d3 100644 +--- a/src/lstack/core/lstack_cfg.c ++++ b/src/lstack/core/lstack_cfg.c +@@ -69,6 +69,8 @@ static int32_t parse_process_numa(void); + static int32_t parse_process_index(void); + static int32_t parse_seperate_sendrecv_args(void); + static int32_t parse_tuple_filter(void); ++static int32_t parse_use_bond4(void); ++static int32_t parse_bond4_slave_mac(void); + + #define PARSE_ARG(_arg, _arg_string, _default_val, _min_val, _max_val, _ret) \ + do { \ +@@ -121,6 +123,8 @@ static struct config_vector_t g_config_tbl[] = { + { "process_numa", parse_process_numa }, + { "process_idx", parse_process_index }, + { "tuple_filter", parse_tuple_filter }, ++ { "use_bond4", parse_use_bond4 }, ++ { "bond4_slave_mac", parse_bond4_slave_mac }, + { NULL, NULL } + }; + +@@ -1052,3 +1056,54 @@ static int parse_tuple_filter(void) + + return 0; + } ++ ++static int32_t parse_use_bond4(void) ++{ ++ int32_t ret; ++ PARSE_ARG(g_config_params.use_bond4, "use_bond4", 0, 0, 1, ret); ++ return ret; ++} ++ ++static int32_t parse_bond4_slave_mac(void) ++{ ++ if (g_config_params.use_bond4 == 0) { ++ return 0; ++ } ++ ++ int32_t ret; ++ const char *slave_mac1 = NULL; ++ const char *slave_mac2 = NULL; ++ const config_setting_t *devs = NULL; ++ ++ devs = config_lookup(&g_config, "slave_mac1"); ++ if (devs == NULL) { ++ return -EINVAL; ++ } ++ slave_mac1 = config_setting_get_string(devs); ++ if (slave_mac1 == NULL) { ++ return 0; ++ } ++ ++ devs = config_lookup(&g_config, "slave_mac2"); ++ if (devs == NULL) { ++ return -EINVAL; ++ } ++ slave_mac2 = config_setting_get_string(devs); ++ if (slave_mac2 == NULL) { ++ return 0; ++ } ++ ++ /* add dev */ ++ ret = str_to_eth_addr(slave_mac1, g_config_params.bond4_slave1_mac_addr); ++ if (ret != 0) { ++ LSTACK_PRE_LOG(LSTACK_ERR, "cfg: invalid device name %s ret=%d.\n", slave_mac1, ret); ++ return ret; ++ } ++ ++ ret = str_to_eth_addr(slave_mac2, g_config_params.bond4_slave2_mac_addr); ++ if (ret != 0) { ++ LSTACK_PRE_LOG(LSTACK_ERR, "cfg: invalid device name %s ret=%d.\n", slave_mac2, ret); ++ } ++ return ret; ++} ++ +diff --git a/src/lstack/core/lstack_dpdk.c b/src/lstack/core/lstack_dpdk.c +index e386dfc..297dd7b 100644 +--- a/src/lstack/core/lstack_dpdk.c ++++ b/src/lstack/core/lstack_dpdk.c +@@ -35,6 +35,9 @@ + #include + #include + #include ++#include ++#include ++#include + + #include "lstack_log.h" + #include "dpdk_common.h" +@@ -427,9 +430,10 @@ static void rss_setup(const int port_id, const uint16_t nb_queues) + free(reta_conf); + } + +-int32_t dpdk_ethdev_init(void) ++int32_t dpdk_ethdev_init(int port_id, bool bond_port) + { + uint16_t nb_queues = get_global_cfg_params()->num_cpu; ++ int32_t use_bond4 = get_global_cfg_params()->use_bond4; + if (get_global_cfg_params()->seperate_send_recv) { + nb_queues = get_global_cfg_params()->num_cpu * 2; + } +@@ -440,10 +444,13 @@ int32_t dpdk_ethdev_init(void) + + struct protocol_stack_group *stack_group = get_protocol_stack_group(); + +- int32_t port_id = ethdev_port_id(get_global_cfg_params()->mac_addr); +- if (port_id < 0) { +- return port_id; ++ if (!use_bond4) { ++ port_id = ethdev_port_id(get_global_cfg_params()->mac_addr); ++ if (port_id < 0) { ++ return port_id; ++ } + } ++ + get_global_cfg_params()->port_id = port_id; + + struct rte_eth_dev_info dev_info; +@@ -459,10 +466,38 @@ int32_t dpdk_ethdev_init(void) + return -EINVAL; + } + ++ if (bond_port) { ++ int slave_num = 2; ++ int32_t slave_port_id[2]; ++ slave_port_id[0] = ethdev_port_id(get_global_cfg_params()->bond4_slave1_mac_addr); ++ slave_port_id[1] = ethdev_port_id(get_global_cfg_params()->bond4_slave2_mac_addr); ++ ++ for (int i = 0; i < slave_num; i++) { ++ ret = dpdk_ethdev_init(slave_port_id[i], 0); ++ if (ret != 0) { ++ LSTACK_LOG(ERR, LSTACK, "dpdk_ethdev_init failed\n"); ++ return -1; ++ } ++ ret = rte_eth_promiscuous_enable(slave_port_id[i]); ++ rte_eth_allmulticast_enable(slave_port_id[i]); ++ ret = rte_eth_bond_slave_add(port_id, slave_port_id[i]); ++ ret = rte_eth_dev_start(slave_port_id[i]); ++ } ++ } ++ + struct eth_params *eth_params = alloc_eth_params(port_id, nb_queues); + if (eth_params == NULL) { + return -ENOMEM; + } ++ ++ if (bond_port) { ++ struct rte_eth_dev_info slave_dev_info; ++ int slave_id = rte_eth_bond_primary_get(port_id); ++ rte_eth_dev_info_get(slave_id, &slave_dev_info); ++ dev_info.rx_offload_capa = slave_dev_info.rx_offload_capa; ++ dev_info.tx_offload_capa = slave_dev_info.tx_offload_capa; ++ } ++ + eth_params_checksum(ð_params->conf, &dev_info); + int32_t rss_enable = 0; + if (!get_global_cfg_params()->tuple_filter) { +@@ -556,6 +591,10 @@ int32_t dpdk_ethdev_start(void) + } + } + ++ if (get_global_cfg_params()->use_bond4) { ++ return 0; ++ } ++ + ret = rte_eth_dev_start(stack_group->eth_params->port_id); + if (ret < 0) { + LSTACK_LOG(ERR, LSTACK, "cannot start ethdev: %d\n", (-ret)); +@@ -603,10 +642,45 @@ int32_t init_dpdk_ethdev(void) + { + int32_t ret; + +- ret = dpdk_ethdev_init(); +- if (ret != 0) { +- LSTACK_LOG(ERR, LSTACK, "dpdk_ethdev_init failed\n"); +- return -1; ++ if (get_global_cfg_params()->use_bond4) { ++ int bond_port_id = rte_eth_bond_create("net_bonding0", 4, (uint8_t)rte_socket_id()); ++ if (bond_port_id < 0) { ++ LSTACK_LOG(ERR, LSTACK, "get bond port id failed ret=%d\n", bond_port_id); ++ return bond_port_id; ++ } ++ ++ ret = dpdk_ethdev_init(bond_port_id, 1); ++ ret = rte_eth_bond_xmit_policy_set(bond_port_id, BALANCE_XMIT_POLICY_LAYER34); ++ if (ret < 0) { ++ return -1; ++ } ++ ++ ret = rte_eth_bond_8023ad_dedicated_queues_enable(bond_port_id); ++ if (ret < 0) { ++ return -1; ++ } ++ ++ ret = rte_eth_promiscuous_enable(bond_port_id); ++ if (ret < 0) { ++ return -1; ++ } ++ ++ ret = rte_eth_allmulticast_enable(bond_port_id); ++ if (ret < 0) { ++ return -1; ++ } ++ ++ ret = rte_eth_dev_start(bond_port_id); ++ /* 20: sleep for lacp ,this is a temp plan, it will be changed in future */ ++ int wait_lacp = 20; ++ sleep(wait_lacp); ++ ++ } else { ++ ret = dpdk_ethdev_init(0, 0); ++ if (ret != 0) { ++ LSTACK_LOG(ERR, LSTACK, "dpdk_ethdev_init failed\n"); ++ return -1; ++ } + } + + if (get_global_cfg_params()->kni_switch && get_global_cfg_params()->is_primary) { +diff --git a/src/lstack/include/lstack_cfg.h b/src/lstack/include/lstack_cfg.h +index a4170ca..4d0f611 100644 +--- a/src/lstack/include/lstack_cfg.h ++++ b/src/lstack/include/lstack_cfg.h +@@ -103,6 +103,9 @@ struct cfg_params { + uint16_t send_ring_size; + bool expand_send_ring; + bool tuple_filter; ++ bool use_bond4; ++ uint8_t bond4_slave1_mac_addr[ETHER_ADDR_LEN]; ++ uint8_t bond4_slave2_mac_addr[ETHER_ADDR_LEN]; + }; + + struct cfg_params *get_global_cfg_params(void); +diff --git a/src/lstack/include/lstack_dpdk.h b/src/lstack/include/lstack_dpdk.h +index a896903..1047c44 100644 +--- a/src/lstack/include/lstack_dpdk.h ++++ b/src/lstack/include/lstack_dpdk.h +@@ -46,7 +46,7 @@ int32_t pktmbuf_pool_init(struct protocol_stack *stack, uint16_t stack_num); + struct rte_ring *create_ring(const char *name, uint32_t count, uint32_t flags, int32_t queue_id); + int32_t create_shared_ring(struct protocol_stack *stack); + void lstack_log_level_init(void); +-int dpdk_ethdev_init(void); ++int dpdk_ethdev_init(int port_id, bool bond_port); + int dpdk_ethdev_start(void); + void dpdk_skip_nic_init(void); + int32_t dpdk_init_lstack_kni(void); +-- +2.33.0 + diff --git a/0238-clean-code.patch b/0238-clean-code.patch new file mode 100644 index 0000000..d85dbe7 --- /dev/null +++ b/0238-clean-code.patch @@ -0,0 +1,2187 @@ +From dd9a270402cf8fe7f2adf9d6bad3a3ac1a2b7289 Mon Sep 17 00:00:00 2001 +From: jiangheng12 +Date: Fri, 5 May 2023 16:42:35 +0800 +Subject: [PATCH] clean code + +--- + src/common/gazelle_dfx_msg.h | 1 - + src/common/gazelle_opt.h | 3 +- + src/common/gazelle_parse_config.c | 8 +- + src/lstack/api/lstack_epoll.c | 1 - + src/lstack/api/lstack_wrap.c | 85 +++--- + src/lstack/core/lstack_cfg.c | 31 +- + src/lstack/core/lstack_control_plane.c | 9 +- + src/lstack/core/lstack_dpdk.c | 21 +- + src/lstack/core/lstack_init.c | 63 ++-- + src/lstack/core/lstack_lwip.c | 310 ++++++++++---------- + src/lstack/core/lstack_protocol_stack.c | 42 +-- + src/lstack/core/lstack_stack_stat.c | 10 +- + src/lstack/include/lstack_dpdk.h | 3 +- + src/lstack/include/lstack_ethdev.h | 4 +- + src/lstack/include/lstack_lockless_queue.h | 6 +- + src/lstack/include/lstack_protocol_stack.h | 2 +- + src/lstack/include/lstack_vdev.h | 3 +- + src/lstack/netif/lstack_ethdev.c | 318 +++++++++++---------- + src/lstack/netif/lstack_vdev.c | 16 +- + src/ltran/ltran_dfx.c | 10 +- + src/ltran/ltran_monitor.c | 2 +- + src/ltran/ltran_param.c | 2 +- + src/ltran/main.c | 4 +- + 23 files changed, 489 insertions(+), 465 deletions(-) + +diff --git a/src/common/gazelle_dfx_msg.h b/src/common/gazelle_dfx_msg.h +index e4da687..a89b82c 100644 +--- a/src/common/gazelle_dfx_msg.h ++++ b/src/common/gazelle_dfx_msg.h +@@ -15,7 +15,6 @@ + + #include + #include +-#include + + #define GAZELLE_CLIENT_NUM_MIN 1 + #define GAZELLE_LOG_LEVEL_MAX 10 +diff --git a/src/common/gazelle_opt.h b/src/common/gazelle_opt.h +index fe0483b..7b6aa19 100644 +--- a/src/common/gazelle_opt.h ++++ b/src/common/gazelle_opt.h +@@ -47,7 +47,8 @@ + + #define TCP_CONN_COUNT 1500 + #define MBUF_COUNT_PER_CONN 170 +-#define RXTX_NB_MBUF_DEFAULT (MBUF_COUNT_PER_CONN * TCP_CONN_COUNT) /* mbuf per connect * connect num. size of mbuf is 2536 Byte */ ++/* mbuf per connect * connect num. size of mbuf is 2536 Byte */ ++#define RXTX_NB_MBUF_DEFAULT (MBUF_COUNT_PER_CONN * TCP_CONN_COUNT) + #define STACK_THREAD_DEFAULT 4 + #define STACK_NIC_READ_DEFAULT 128 + +diff --git a/src/common/gazelle_parse_config.c b/src/common/gazelle_parse_config.c +index ce6a3f8..42e12b5 100644 +--- a/src/common/gazelle_parse_config.c ++++ b/src/common/gazelle_parse_config.c +@@ -55,7 +55,7 @@ int32_t separate_str_to_array(char *args, uint32_t *array, int32_t array_size, i + return -1; + } + errno = 0; +- /* prefix 0x,0X indicate hexdecimal */ ++ /* 2: prefix 0x,0X indicate hexdecimal */ + if (strncmp(args, "0x", 2) == 0 || strncmp(args, "0X", 2) == 0) { + idx = strtol(args, &end, 16); /* 16: hexdecimal */ + } else { +@@ -116,13 +116,15 @@ int32_t filename_check(const char* args) + } + + if (strlen(args) <= 0 || strlen(args) > GAZELLE_SOCK_FILENAME_MAXLEN - 1) { +- COMMON_ERR("socket_filename_check: invalid unix sock name %s, filename exceeds the limit %d.\n", args, GAZELLE_SOCK_FILENAME_MAXLEN); ++ COMMON_ERR("socket_filename_check: invalid unix sock name %s, filename exceeds the limit %d.\n", ++ args, GAZELLE_SOCK_FILENAME_MAXLEN); + return 1; + } + + char* sensitive_chars = strpbrk(args, "|;&$><`\\!\n"); + if (sensitive_chars != NULL) { +- COMMON_ERR("socket_filename_check: invalid unix sock name %s, filename contains sensitive characters.\n", args); ++ COMMON_ERR("socket_filename_check: invalid unix sock name %s, filename contains sensitive characters.\n", ++ args); + return 1; + } + +diff --git a/src/lstack/api/lstack_epoll.c b/src/lstack/api/lstack_epoll.c +index d4b4be7..06bd71b 100644 +--- a/src/lstack/api/lstack_epoll.c ++++ b/src/lstack/api/lstack_epoll.c +@@ -320,7 +320,6 @@ int32_t lstack_epoll_ctl(int32_t epfd, int32_t op, int32_t fd, struct epoll_even + return posix_api->epoll_ctl_fn(epfd, op, fd, event); + } + +- + struct wakeup_poll *wakeup = epoll_sock->wakeup; + struct lwip_sock *sock = get_socket(fd); + if (sock == NULL) { +diff --git a/src/lstack/api/lstack_wrap.c b/src/lstack/api/lstack_wrap.c +index 1b12822..98632c0 100644 +--- a/src/lstack/api/lstack_wrap.c ++++ b/src/lstack/api/lstack_wrap.c +@@ -199,7 +199,7 @@ static int get_addr(struct sockaddr_in *sin, char *interface) + memset_s(&ifr, sizeof(ifr), 0, sizeof(ifr)); + snprintf_s(ifr.ifr_name, sizeof(ifr.ifr_name), (sizeof(ifr.ifr_name) - 1), "%s", interface); + +- if(posix_api->ioctl_fn(sockfd, SIOCGIFADDR, &ifr) < 0){ ++ if (posix_api->ioctl_fn(sockfd, SIOCGIFADDR, &ifr) < 0) { + posix_api->close_fn(sockfd); + return -1; + } +@@ -243,11 +243,15 @@ bool is_dst_ip_localhost(const struct sockaddr *addr) + struct sockaddr_in* sin = malloc(sizeof(struct sockaddr_in)); + + while (getdelim(&line, &linel, '\n', ifh) > 0) { +- if (linenum++ < 2) continue; ++ /* 2: skip the first two lines, which are not nic name */ ++ if (linenum++ < 2) { ++ continue; ++ } + + p = line; +- while (isspace(*p)) +- ++p; ++ while (isspace(*p)) { ++ ++p; ++ } + int n = strcspn(p, ": \t"); + + char interface[20] = {0}; /* 20: nic name len */ +@@ -256,7 +260,7 @@ bool is_dst_ip_localhost(const struct sockaddr *addr) + memset_s(sin, sizeof(struct sockaddr_in), 0, sizeof(struct sockaddr_in)); + int ret = get_addr(sin, interface); + if (ret == 0) { +- if(sin->sin_addr.s_addr == servaddr->sin_addr.s_addr){ ++ if (sin->sin_addr.s_addr == servaddr->sin_addr.s_addr) { + return 1; + } + } +@@ -289,13 +293,14 @@ static int32_t do_connect(int32_t s, const struct sockaddr *name, socklen_t name + int32_t ret = 0; + char listen_ring_name[RING_NAME_LEN]; + int remote_port = htons(((struct sockaddr_in *)name)->sin_port); +- snprintf_s(listen_ring_name, sizeof(listen_ring_name), sizeof(listen_ring_name) - 1, "listen_rx_ring_%d", remote_port); ++ snprintf_s(listen_ring_name, sizeof(listen_ring_name), sizeof(listen_ring_name) - 1, ++ "listen_rx_ring_%d", remote_port); + if (is_dst_ip_localhost(name) && rte_ring_lookup(listen_ring_name) == NULL) { + ret = posix_api->connect_fn(s, name, namelen); +- SET_CONN_TYPE_HOST(sock->conn); ++ SET_CONN_TYPE_HOST(sock->conn); + } else { + ret = rpc_call_connect(s, name, namelen); +- SET_CONN_TYPE_LIBOS(sock->conn); ++ SET_CONN_TYPE_LIBOS(sock->conn); + } + + return ret; +@@ -435,26 +440,26 @@ static inline ssize_t do_read(int32_t s, void *mem, size_t len) + + static inline ssize_t do_readv(int32_t s, const struct iovec *iov, int iovcnt) + { +- struct lwip_sock *sock = NULL; +- if (select_path(s, &sock) != PATH_LWIP) { ++ struct lwip_sock *sock = NULL; ++ if (select_path(s, &sock) != PATH_LWIP) { + return posix_api->readv_fn(s, iov, iovcnt); +- } +- +- struct msghdr msg; +- +- msg.msg_name = NULL; +- msg.msg_namelen = 0; +- msg.msg_iov = LWIP_CONST_CAST(struct iovec *, iov); +- msg.msg_iovlen = iovcnt; +- msg.msg_control = NULL; +- msg.msg_controllen = 0; +- msg.msg_flags = 0; +- ssize_t result = recvmsg_from_stack(s, &msg, 0); +- if(result == -1 && errno == EAGAIN){ ++ } ++ ++ struct msghdr msg; ++ ++ msg.msg_name = NULL; ++ msg.msg_namelen = 0; ++ msg.msg_iov = LWIP_CONST_CAST(struct iovec *, iov); ++ msg.msg_iovlen = iovcnt; ++ msg.msg_control = NULL; ++ msg.msg_controllen = 0; ++ msg.msg_flags = 0; ++ ssize_t result = recvmsg_from_stack(s, &msg, 0); ++ if (result == -1 && errno == EAGAIN) { + errno = 0; +- return 0; +- } +- return result; ++ return 0; ++ } ++ return result; + } + + static inline ssize_t do_send(int32_t sockfd, const void *buf, size_t len, int32_t flags) +@@ -479,21 +484,21 @@ static inline ssize_t do_write(int32_t s, const void *mem, size_t size) + + static inline ssize_t do_writev(int32_t s, const struct iovec *iov, int iovcnt) + { +- struct lwip_sock *sock = NULL; +- if (select_path(s, &sock) != PATH_LWIP) { ++ struct lwip_sock *sock = NULL; ++ if (select_path(s, &sock) != PATH_LWIP) { + return posix_api->writev_fn(s, iov, iovcnt); +- } +- +- struct msghdr msg; +- +- msg.msg_name = NULL; +- msg.msg_namelen = 0; +- msg.msg_iov = LWIP_CONST_CAST(struct iovec *, iov); +- msg.msg_iovlen = iovcnt; +- msg.msg_control = NULL; +- msg.msg_controllen = 0; +- msg.msg_flags = 0; +- return sendmsg_to_stack(sock, s, &msg, 0); ++ } ++ ++ struct msghdr msg; ++ ++ msg.msg_name = NULL; ++ msg.msg_namelen = 0; ++ msg.msg_iov = LWIP_CONST_CAST(struct iovec *, iov); ++ msg.msg_iovlen = iovcnt; ++ msg.msg_control = NULL; ++ msg.msg_controllen = 0; ++ msg.msg_flags = 0; ++ return sendmsg_to_stack(sock, s, &msg, 0); + } + + static inline ssize_t do_recvmsg(int32_t s, struct msghdr *message, int32_t flags) +diff --git a/src/lstack/core/lstack_cfg.c b/src/lstack/core/lstack_cfg.c +index cdb0200..db35110 100644 +--- a/src/lstack/core/lstack_cfg.c ++++ b/src/lstack/core/lstack_cfg.c +@@ -75,19 +75,19 @@ static int32_t parse_tuple_filter(void); + const config_setting_t *_config_arg = NULL; \ + _config_arg = config_lookup(&g_config, _arg_string); \ + if (_config_arg == NULL) { \ +- _arg = _default_val; \ +- _ret = 0; \ ++ (_arg) = (_default_val); \ ++ (_ret) = 0; \ + break; \ + } \ + int32_t _val = config_setting_get_int(_config_arg); \ +- if (_val < _min_val || _val > _max_val) { \ ++ if (_val < (_min_val) || _val > (_max_val)) { \ + LSTACK_PRE_LOG(LSTACK_ERR, "cfg %s %d invaild, range is [%d, %d].\n", \ +- _arg_string, _val, _min_val, _max_val); \ +- _ret = -EINVAL; \ ++ (_arg_string), _val, (_min_val), (_max_val)); \ ++ (_ret) = -EINVAL; \ + break; \ + } \ +- _arg = _val; \ +- _ret = 0; \ ++ (_arg) = _val; \ ++ (_ret) = 0; \ + } while (0) + + struct config_vector_t { +@@ -342,7 +342,8 @@ static int32_t parse_stack_cpu_number(void) + } + + char *tmp_arg_send = strdup(args); +- int32_t send_cpu_cnt = separate_str_to_array(tmp_arg_send, g_config_params.send_cpus, CFG_MAX_CPUS, CFG_MAX_CPUS); ++ int32_t send_cpu_cnt = separate_str_to_array(tmp_arg_send, g_config_params.send_cpus, ++ CFG_MAX_CPUS, CFG_MAX_CPUS); + free(tmp_arg_send); + + // recv_num_cpus +@@ -368,7 +369,8 @@ static int32_t parse_stack_cpu_number(void) + } + + char *tmp_arg_recv = strdup(args); +- int32_t recv_cpu_cnt = separate_str_to_array(tmp_arg_recv, g_config_params.recv_cpus, CFG_MAX_CPUS, CFG_MAX_CPUS); ++ int32_t recv_cpu_cnt = separate_str_to_array(tmp_arg_recv, g_config_params.recv_cpus, ++ CFG_MAX_CPUS, CFG_MAX_CPUS); + free(tmp_arg_recv); + + if (send_cpu_cnt <= 0 || send_cpu_cnt > CFG_MAX_CPUS / 2 || send_cpu_cnt != recv_cpu_cnt) { +@@ -444,7 +446,7 @@ int32_t init_stack_numa_cpuset(struct protocol_stack *stack) + for (int32_t idx = 0; idx < cfg->num_cpu; ++idx) { + if (!cfg->seperate_send_recv) { + CPU_SET(cfg->cpus[idx], &stack_cpuset); +- }else { ++ } else { + CPU_SET(cfg->send_cpus[idx], &stack_cpuset); + CPU_SET(cfg->recv_cpus[idx], &stack_cpuset); + } +@@ -494,7 +496,6 @@ static int32_t gazelle_parse_socket_mem(const char *arg, struct secondary_attach + + int32_t count = separate_str_to_array(socket_mem, sec_attach_arg->socket_per_size, + GAZELLE_MAX_NUMA_NODES, INT32_MAX); +- + if (count < 0) { + return -1; + } +@@ -726,7 +727,7 @@ static int32_t parse_dpdk_args(void) + g_config_params.dpdk_argv[start_index + i] = p; + + const char *primary = "primary"; +- if(strcmp(p, primary) == 0){ ++ if (strcmp(p, primary) == 0) { + struct cfg_params *global_params = get_global_cfg_params(); + global_params->is_primary = 1; + } +@@ -937,10 +938,8 @@ static int32_t parse_unix_prefix(void) + } + + unix_prefix = config_lookup(&g_config, "unix_prefix"); +- + if (unix_prefix) { + args = config_setting_get_string(unix_prefix); +- + if (filename_check(args)) { + return -EINVAL; + } +@@ -979,7 +978,7 @@ static int32_t parse_num_process(void) + num_process = config_lookup(&g_config, "num_process"); + if (num_process == NULL) { + g_config_params.num_process = 1; +- }else { ++ } else { + g_config_params.num_process = (uint8_t)config_setting_get_int(num_process); + } + +@@ -1022,7 +1021,7 @@ static int parse_process_index(void) + if (process_idx == NULL) { + if (g_config_params.num_process == 1) { + g_config_params.process_idx = 0; +- }else { ++ } else { + return -EINVAL; + } + } else { +diff --git a/src/lstack/core/lstack_control_plane.c b/src/lstack/core/lstack_control_plane.c +index e705cae..be156dc 100644 +--- a/src/lstack/core/lstack_control_plane.c ++++ b/src/lstack/core/lstack_control_plane.c +@@ -466,14 +466,15 @@ void control_fd_close(void) + + struct cfg_params *global_params = get_global_cfg_params(); + if (!global_params->use_ltran) { +- int ret = unlink(global_params->unix_socket_filename); +- if (ret == -1) { ++ int ret = unlink(global_params->unix_socket_filename); ++ if (ret == -1) { + LSTACK_LOG(ERR, LSTACK, "unlink failed, just skip it\n"); +- } ++ } + } + } + +-void delete_primary_path(void) { ++void delete_primary_path(void) ++{ + if (!remove(GAZELLE_PRIMARY_START_PATH)) { + LSTACK_LOG(ERR, LSTACK, "delete %s failed\n", GAZELLE_PRIMARY_START_PATH); + } +diff --git a/src/lstack/core/lstack_dpdk.c b/src/lstack/core/lstack_dpdk.c +index e386dfc..79d13ee 100644 +--- a/src/lstack/core/lstack_dpdk.c ++++ b/src/lstack/core/lstack_dpdk.c +@@ -38,12 +38,12 @@ + + #include "lstack_log.h" + #include "dpdk_common.h" +-#include "lstack_dpdk.h" + #include "lstack_lockless_queue.h" + #include "lstack_protocol_stack.h" + #include "lstack_thread_rpc.h" + #include "lstack_lwip.h" + #include "lstack_cfg.h" ++#include "lstack_dpdk.h" + + struct eth_params { + uint16_t port_id; +@@ -116,7 +116,7 @@ int32_t dpdk_eal_init(void) + ret = 0; + } else { + LSTACK_PRE_LOG(LSTACK_ERR, "rte_eal_init failed init, rte_errno %d\n", rte_errno); +- return ret; ++ return ret; + } + } else { + LSTACK_PRE_LOG(LSTACK_INFO, "dpdk_eal_init success\n"); +@@ -155,7 +155,8 @@ struct rte_mempool *create_pktmbuf_mempool(const char *name, uint32_t nb_mbuf, + return pool; + } + +-static struct rte_mempool* get_pktmbuf_mempool(const char *name, uint16_t queue_id){ ++static struct rte_mempool* get_pktmbuf_mempool(const char *name, uint16_t queue_id) ++{ + int32_t ret; + char pool_name[PATH_MAX]; + struct rte_mempool *pool; +@@ -169,9 +170,7 @@ static struct rte_mempool* get_pktmbuf_mempool(const char *name, uint16_t queue_ + LSTACK_LOG(ERR, LSTACK, "look up %s pool rte_err=%d\n", pool_name, rte_errno); + } + +- // rte_mempool_dump(stdout, pool) ; + return pool; +- + } + + static struct reg_ring_msg *create_reg_mempool(const char *name, uint16_t queue_id) +@@ -220,7 +219,8 @@ struct rte_ring *create_ring(const char *name, uint32_t count, uint32_t flags, i + 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); ++ 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) { + return NULL; + } +@@ -521,10 +521,10 @@ static int32_t dpdk_ethdev_setup(const struct eth_params *eth_params, uint16_t i + struct rte_mempool *rxtx_pktmbuf_pool = get_protocol_stack_group()->total_rxtx_pktmbuf_pool[idx]; + + uint16_t socket_id = 0; +- struct cfg_params * cfg = get_global_cfg_params(); ++ struct cfg_params *cfg = get_global_cfg_params(); + if (!cfg->use_ltran && cfg->num_process == 1) { + socket_id = numa_node_of_cpu(cfg->cpus[idx]); +- }else { ++ } else { + socket_id = cfg->process_numa[idx]; + } + ret = rte_eth_rx_queue_setup(eth_params->port_id, idx, eth_params->nb_rx_desc, socket_id, +@@ -568,8 +568,6 @@ int32_t dpdk_ethdev_start(void) + int32_t dpdk_init_lstack_kni(void) + { + struct protocol_stack_group *stack_group = get_protocol_stack_group(); +- +- + stack_group->kni_pktmbuf_pool = create_pktmbuf_mempool("kni_mbuf", KNI_NB_MBUF, 0, 0); + if (stack_group->kni_pktmbuf_pool == NULL) { + return -1; +@@ -651,7 +649,8 @@ void dpdk_nic_xstats_get(struct gazelle_stack_dfx_data *dfx, uint16_t port_id) + if (len < 0) { + return; + } +- if (len != rte_eth_xstats_get_names_by_id(port_id, (struct rte_eth_xstat_name *)dfx->data.nic_xstats.xstats_name, len, NULL)) { ++ if (len != rte_eth_xstats_get_names_by_id(port_id, ++ (struct rte_eth_xstat_name *)dfx->data.nic_xstats.xstats_name, len, NULL)) { + dfx->data.nic_xstats.len = -1; + return; + } +diff --git a/src/lstack/core/lstack_init.c b/src/lstack/core/lstack_init.c +index f17e4d2..4fbbc14 100644 +--- a/src/lstack/core/lstack_init.c ++++ b/src/lstack/core/lstack_init.c +@@ -98,18 +98,20 @@ static int32_t preload_info_init(void) + return 0; + } + +-static void check_process_start(void) { ++static void check_process_start(void) ++{ + if (get_global_cfg_params()->is_primary) { + return; + } + + while (!fopen(GAZELLE_PRIMARY_START_PATH, "r")) { +- printf("please make sure the primary process start already!\n"); +- sleep(1); ++ LSTACK_LOG(INFO, LSTACK, "please make sure the primary process start already!\n"); ++ sleep(1); + } + } + +-static int32_t set_process_start_flag(void) { ++static int32_t set_process_start_flag(void) ++{ + if (!get_global_cfg_params()->is_primary) { + return 0; + } +@@ -118,7 +120,7 @@ static int32_t set_process_start_flag(void) { + fp = fopen(GAZELLE_PRIMARY_START_PATH, "w"); + if (fp == NULL) { + LSTACK_PRE_LOG(LSTACK_ERR, "set primary proceaa start flag failed!\n"); +- return -1; ++ return -1; + } + (void)fclose(fp); + return 0; +@@ -199,9 +201,7 @@ static void create_control_thread(void) + + pthread_t tid; + if (use_ltran()) { +- /* +- * The function call here should be in strict order. +- */ ++ /* The function call here should be in strict order. */ + dpdk_skip_nic_init(); + if (control_init_client(false) != 0) { + LSTACK_EXIT(1, "control_init_client failed\n"); +@@ -235,8 +235,7 @@ static void gazelle_signal_init(void) + LSTACK_EXIT(1, "signal SIGPIPE SIG_IGN\n"); + } + +- /* +- * register core sig handler func to dumped stack */ ++ /* register core sig handler func to dumped stack */ + lstack_signal_init(); + } + +@@ -264,15 +263,16 @@ static void set_kni_ip_mac() + } + + set_ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER; ++ /* 6: mac addr len */ + for (int i = 0; i < 6; i++) { + set_ifr.ifr_hwaddr.sa_data[i] = cfg->mac_addr[i]; + } + + if (posix_api->ioctl_fn(fd, SIOCSIFHWADDR, &set_ifr) < 0) { + LSTACK_LOG(ERR, LSTACK, "set kni macaddr=%hhx:%hhx:%hhx:%hhx:%hhx:%hhx fail\n", +- cfg->mac_addr[0], cfg->mac_addr[1], +- cfg->mac_addr[2], cfg->mac_addr[3], +- cfg->mac_addr[4], cfg->mac_addr[5]); ++ cfg->mac_addr[0], cfg->mac_addr[1], ++ cfg->mac_addr[2], cfg->mac_addr[3], ++ cfg->mac_addr[4], cfg->mac_addr[5]); + } + + if (posix_api->ioctl_fn(fd, SIOCGIFFLAGS, &set_ifr) < 0) { +@@ -280,7 +280,7 @@ static void set_kni_ip_mac() + } + + set_ifr.ifr_flags |= (IFF_RUNNING | IFF_UP); +- if (posix_api->ioctl_fn(fd, SIOCSIFFLAGS, &set_ifr) < 0){ ++ if (posix_api->ioctl_fn(fd, SIOCSIFFLAGS, &set_ifr) < 0) { + LSTACK_LOG(ERR, LSTACK, "set kni state fail\n"); + } + +@@ -289,16 +289,14 @@ static void set_kni_ip_mac() + + __attribute__((constructor)) void gazelle_network_init(void) + { +- /* +- * Init POSXI API and prelog */ ++ /* Init POSXI API and prelog */ + lstack_prelog_init("LSTACK"); + if (posix_api_init() != 0) { + LSTACK_PRE_LOG(LSTACK_ERR, "posix_api_init failed\n"); + LSTACK_EXIT(1, "failed\n"); + } + +- /* +- * Init LD_PRELOAD */ ++ /* Init LD_PRELOAD */ + if (preload_info_init() < 0) { + return; + } +@@ -306,35 +304,29 @@ __attribute__((constructor)) void gazelle_network_init(void) + return; + } + +- /* +- * Read configure from lstack.cfg */ ++ /* Read configure from lstack.cfg */ + if (cfg_init() != 0) { + LSTACK_PRE_LOG(LSTACK_ERR, "cfg_init failed\n"); + LSTACK_EXIT(1, "cfg_init failed\n"); + } + LSTACK_PRE_LOG(LSTACK_INFO, "cfg_init success\n"); + +- /* +- * check primary process start */ ++ /* check primary process start */ + check_process_start(); + +- /* +- * check conflict */ ++ /* check conflict */ + if (check_process_conflict() < 0) { + LSTACK_PRE_LOG(LSTACK_INFO, "Have another same primary process. WARNING: Posix API will use kernel mode!\n"); + return; + } + +- /** +- * check lstack num, and get process idx +- */ ++ /* check lstack num, and get process idx */ + if (check_params_from_primary() < 0) { + LSTACK_PRE_LOG(LSTACK_ERR, "lstack num error, not same to primary process!\n"); + LSTACK_EXIT(1, "lstack num error, not same to primary process!\n"); + } + +- /* +- * save initial affinity */ ++ /* save initial affinity */ + if (!get_global_cfg_params()->main_thread_affinity) { + if (thread_affinity_default() < 0) { + LSTACK_PRE_LOG(LSTACK_ERR, "pthread_getaffinity_np failed\n"); +@@ -342,17 +334,13 @@ __attribute__((constructor)) void gazelle_network_init(void) + } + } + +- // @todo, check process 2 dumped, resorce need to release. +- + gazelle_signal_init(); + +- /* +- * Init control plane and dpdk init */ ++ /* Init control plane and dpdk init */ + create_control_thread(); + dpdk_restore_pci(); + +- /* +- * cancel the core binding from DPDK initialization */ ++ /* cancel the core binding from DPDK initialization */ + if (!get_global_cfg_params()->main_thread_affinity) { + if (thread_affinity_default() < 0) { + LSTACK_EXIT(1, "pthread_setaffinity_np failed\n"); +@@ -366,16 +354,13 @@ __attribute__((constructor)) void gazelle_network_init(void) + LSTACK_EXIT(1, "init_protocol_stack failed\n"); + } + +- /* +- * nic */ + if (!use_ltran()) { + if (init_dpdk_ethdev() != 0) { + LSTACK_EXIT(1, "init_dpdk_ethdev failed\n"); + } + } + +- /* +- * lwip initialization */ ++ /* lwip initialization */ + lwip_sock_init(); + + /* wait stack thread and kernel_event thread init finish */ +diff --git a/src/lstack/core/lstack_lwip.c b/src/lstack/core/lstack_lwip.c +index 0535428..b6c5813 100644 +--- a/src/lstack/core/lstack_lwip.c ++++ b/src/lstack/core/lstack_lwip.c +@@ -33,8 +33,8 @@ + #include "posix/lstack_epoll.h" + #include "lstack_thread_rpc.h" + #include "dpdk_common.h" +-#include "lstack_lwip.h" + #include "lstack_cfg.h" ++#include "lstack_lwip.h" + + static void free_ring_pbuf(struct rte_ring *ring) + { +@@ -263,7 +263,7 @@ struct pbuf *write_lwip_data(struct lwip_sock *sock, uint16_t remain_size, uint8 + return NULL; + } + if (pbuf->allow_in == 1) { +- __sync_fetch_and_sub(&pbuf->allow_in, 1); ++ __sync_fetch_and_sub(&pbuf->allow_in, 1); + } + pthread_spin_unlock(&pbuf->pbuf_lock); + +@@ -300,7 +300,7 @@ struct pbuf *write_lwip_data(struct lwip_sock *sock, uint16_t remain_size, uint8 + pbuf->head = 1; + return NULL; + } +- if(pbuf->allow_in == 1){ ++ if (pbuf->allow_in == 1) { + __sync_fetch_and_sub(&pbuf->allow_in, 1); + } + pthread_spin_unlock(&pbuf->pbuf_lock); +@@ -374,6 +374,9 @@ static ssize_t do_app_write(struct pbuf *pbufs[], void *buf, size_t len, uint32_ + static inline ssize_t app_direct_write(struct protocol_stack *stack, struct lwip_sock *sock, void *buf, + size_t len, uint32_t write_num) + { ++ if (write_num == 0) { ++ return 0; ++ } + struct pbuf **pbufs = (struct pbuf **)malloc(write_num * sizeof(struct pbuf *)); + if (pbufs == NULL) { + return 0; +@@ -410,6 +413,9 @@ static inline ssize_t app_direct_write(struct protocol_stack *stack, struct lwip + static inline ssize_t app_direct_attach(struct protocol_stack *stack, struct pbuf *attach_pbuf, void *buf, + size_t len, uint32_t write_num) + { ++ if (write_num == 0) { ++ return 0; ++ } + struct pbuf **pbufs = (struct pbuf **)malloc(write_num * sizeof(struct pbuf *)); + if (pbufs == NULL) { + return 0; +@@ -469,7 +475,7 @@ static inline struct pbuf *gazelle_ring_readlast(struct rte_ring *r) + __rte_ring_dequeue_elems(r, last, (void **)&last_pbuf, sizeof(void *), 1); + + if (pthread_spin_trylock(&last_pbuf->pbuf_lock) != 0) { +- return NULL; ++ return NULL; + } + if (last_pbuf->allow_in != 1) { + pthread_spin_unlock(&last_pbuf->pbuf_lock); +@@ -587,7 +593,8 @@ ssize_t write_stack_data(struct lwip_sock *sock, const void *buf, size_t len) + + /* send_ring have idle */ + if (get_global_cfg_params()->expand_send_ring) { +- send_len += (write_num <= write_avail) ? app_buff_write(sock, (char *)buf + send_len, len - send_len, write_num) : ++ send_len += (write_num <= write_avail) ? ++ app_buff_write(sock, (char *)buf + send_len, len - send_len, write_num) : + app_direct_write(stack, sock, (char *)buf + send_len, len - send_len, write_num); + } else { + if (write_num > write_avail) { +@@ -662,7 +669,7 @@ void stack_send(struct rpc_msg *msg) + rpc_msg_free(msg); + return; + } else { +- if(__atomic_load_n(&sock->call_num, __ATOMIC_ACQUIRE) == 0){ ++ if (__atomic_load_n(&sock->call_num, __ATOMIC_ACQUIRE) == 0) { + rpc_call(&stack->rpc_queue, msg); + __sync_fetch_and_add(&sock->call_num, 1); + } else { +@@ -741,7 +748,7 @@ ssize_t read_lwip_data(struct lwip_sock *sock, int32_t flags, u8_t apiflags) + + sock->stack->stats.read_lwip_cnt += read_count; + if (recv_len == 0) { +- GAZELLE_RETURN(EAGAIN); ++ GAZELLE_RETURN(EAGAIN); + } + return recv_len; + } +@@ -797,11 +804,10 @@ ssize_t recvmsg_from_stack(int32_t s, struct msghdr *message, int32_t flags) + + static inline void notice_stack_send(struct lwip_sock *sock, int32_t fd, int32_t len, int32_t flags) + { +- if(__atomic_load_n(&sock->call_num, __ATOMIC_ACQUIRE) >= 2){ +- ;; +- } else { ++ // 2: call_num >= 2, don't need add new rpc send ++ if (__atomic_load_n(&sock->call_num, __ATOMIC_ACQUIRE) < 2) { + while (rpc_call_send(fd, NULL, len, flags) < 0) { +- usleep(1000); // wait 1ms to exec again ++ usleep(1000); // 1000: wait 1ms to exec again + LSTACK_LOG(INFO, LSTACK, "rpc_call_send failed, try again\n"); + } + __sync_fetch_and_add(&sock->call_num, 1); +@@ -838,10 +844,8 @@ ssize_t gazelle_same_node_ring_recv(struct lwip_sock *sock, const void *buf, siz + act_len = -1; + goto END; + } +- + act_len = cur_end - index + 1; + act_len = RTE_MIN(act_len, len); +- + if ((index & SAME_NODE_RING_MASK) + act_len > SAME_NODE_RING_LEN) { + size_t act_len1 = SAME_NODE_RING_LEN - (index & SAME_NODE_RING_MASK); + size_t act_len2 = act_len - act_len1; +@@ -898,7 +902,8 @@ ssize_t gazelle_same_node_ring_send(struct lwip_sock *sock, const void *buf, siz + PER_THREAD uint16_t stack_sock_num[GAZELLE_MAX_STACK_NUM] = {0}; + PER_THREAD uint16_t max_sock_stack = 0; + +-static inline void thread_bind_stack(struct lwip_sock *sock) { ++static inline void thread_bind_stack(struct lwip_sock *sock) ++{ + if (likely(sock->already_bind_numa || !sock->stack)) { + return; + } +@@ -906,7 +911,7 @@ static inline void thread_bind_stack(struct lwip_sock *sock) { + stack_sock_num[sock->stack->stack_idx]++; + if (stack_sock_num[sock->stack->stack_idx] > max_sock_stack) { + max_sock_stack = stack_sock_num[sock->stack->stack_idx]; +- bind_to_stack_numa(sock->stack); ++ bind_to_stack_numa(sock->stack); + } + } + +@@ -960,7 +965,7 @@ ssize_t sendmsg_to_stack(struct lwip_sock *sock, int32_t s, const struct msghdr + buflen += ret; + + if (ret < message->msg_iov[i].iov_len) { +- break; ++ break; + } + } + +@@ -1104,7 +1109,6 @@ void read_recv_list(struct protocol_stack *stack, uint32_t max_num) + + ssize_t len = lwip_recv(sock->conn->socket, NULL, 0, 0); + if (len == 0) { +- /* FIXME: should use POLLRDHUP, when connection be closed. lwip event-callback no POLLRDHUP */ + sock->errevent = 1; + add_sock_event(sock, EPOLLERR); + } else if (len > 0) { +@@ -1125,7 +1129,7 @@ void gazelle_connected_callback(struct netconn *conn) + return; + } + +- if (sock->wakeup != NULL && sock->wakeup->epollfd > 0){ ++ if (sock->wakeup != NULL && sock->wakeup->epollfd > 0) { + posix_api->epoll_ctl_fn(sock->wakeup->epollfd, EPOLL_CTL_DEL, fd, NULL); + } + +@@ -1186,7 +1190,7 @@ static inline void clone_lwip_socket_opt(struct lwip_sock *dst_sock, struct lwip + + int32_t gazelle_socket(int domain, int type, int protocol) + { +- if (((type & SOCK_TYPE_MASK) & ~SOCK_STREAM) != 0){ ++ if (((type & SOCK_TYPE_MASK) & ~SOCK_STREAM) != 0) { + LSTACK_LOG(ERR, LSTACK, "sock type error:%d, only support SOCK_STREAM \n", type); + return -1; + } +@@ -1337,125 +1341,125 @@ void stack_recvlist_count(struct rpc_msg *msg) + + void netif_poll(struct netif *netif) + { +- struct tcp_pcb *pcb = NULL; +- struct tcp_pcb_listen *pcbl = NULL; ++ struct tcp_pcb *pcb = NULL; ++ struct tcp_pcb_listen *pcbl = NULL; + +- for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { ++ for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { + #define NETIF_POLL_READ_COUNT 32 +- struct pbuf *pbufs[NETIF_POLL_READ_COUNT]; +- int ret; +- +- if (pcb->client_rx_ring != NULL) { +- ret = rte_ring_sc_dequeue_burst(pcb->client_rx_ring, (void **)pbufs, NETIF_POLL_READ_COUNT, NULL); +- for (int i = 0; i < ret; i++) { +- if (ip_input(pbufs[i], netif) != 0) { +- LSTACK_LOG(INFO, LSTACK, "netif_poll: ip_input return err\n"); +- pbuf_free(pbufs[i]); ++ struct pbuf *pbufs[NETIF_POLL_READ_COUNT]; ++ int ret; ++ ++ if (pcb->client_rx_ring != NULL) { ++ ret = rte_ring_sc_dequeue_burst(pcb->client_rx_ring, (void **)pbufs, NETIF_POLL_READ_COUNT, NULL); ++ for (int i = 0; i < ret; i++) { ++ if (ip_input(pbufs[i], netif) != 0) { ++ LSTACK_LOG(INFO, LSTACK, "ip_input return err\n"); ++ pbuf_free(pbufs[i]); ++ } ++ } + } +- } +- } +- } +- for (pcbl = tcp_listen_pcbs.listen_pcbs; pcbl != NULL; pcbl = pcbl->next) { +- if (pcbl->listen_rx_ring != NULL) { +- struct pbuf *pbuf; +- if (rte_ring_sc_dequeue(pcbl->listen_rx_ring, (void **)&pbuf) == 0) { +- if (ip_input(pbuf, netif) != ERR_OK) { +- pbuf_free(pbuf); ++ } ++ for (pcbl = tcp_listen_pcbs.listen_pcbs; pcbl != NULL; pcbl = pcbl->next) { ++ if (pcbl->listen_rx_ring != NULL) { ++ struct pbuf *pbuf; ++ if (rte_ring_sc_dequeue(pcbl->listen_rx_ring, (void **)&pbuf) == 0) { ++ if (ip_input(pbuf, netif) != ERR_OK) { ++ pbuf_free(pbuf); ++ } ++ } + } +- } + } +- } + } + + /* processes on same node handshake packet use this function */ + err_t netif_loop_output(struct netif *netif, struct pbuf *p) + { +- struct tcp_pcb *pcb = p->pcb; +- struct pbuf *head = NULL; +- +- if (pcb == NULL || pcb->client_tx_ring == NULL) { +- LSTACK_LOG(ERR, LSTACK, "netif_loop_output: pcb is null\n"); +- return ERR_ARG; +- } +- +- if (p->next != NULL) { +- LSTACK_LOG(ERR, LSTACK, "netif_loop_output: not support chained pbuf\n"); +- return ERR_ARG; +- } +- +- struct tcp_hdr *tcp_hdr = (struct tcp_hdr *)((char *)p->payload + sizeof(struct ip_hdr)); +- uint8_t flags = TCPH_FLAGS(tcp_hdr); +- +- head = pbuf_alloc(0, p->len, PBUF_RAM); +- if (head == NULL) { +- LSTACK_LOG(ERR, LSTACK, "netif_loop_output: pbuf_alloc failed\n"); +- return ERR_MEM; +- } +- head->ol_flags = p->ol_flags; +- memcpy_s(head->payload, head->len, p->payload, p->len); +- +- if ((flags & TCP_SYN) && !(flags & TCP_ACK)) { +- /* SYN packet, send to listen_ring */ +- char ring_name[RING_NAME_LEN] = {0}; +- snprintf_s(ring_name, sizeof(ring_name), sizeof(ring_name) - 1, "listen_rx_ring_%d", pcb->remote_port); +- struct rte_ring *ring = rte_ring_lookup(ring_name); +- if (ring == NULL) { +- LSTACK_LOG(INFO, LSTACK, "netif_loop_output: cant find listen_rx_ring %d\n", pcb->remote_port); +- pbuf_free(head); ++ struct tcp_pcb *pcb = p->pcb; ++ struct pbuf *head = NULL; ++ ++ if (pcb == NULL || pcb->client_tx_ring == NULL) { ++ LSTACK_LOG(ERR, LSTACK, "pcb is null\n"); ++ return ERR_ARG; ++ } ++ ++ if (p->next != NULL) { ++ LSTACK_LOG(ERR, LSTACK, "netif_loop_output: not support chained pbuf\n"); ++ return ERR_ARG; ++ } ++ ++ struct tcp_hdr *tcp_hdr = (struct tcp_hdr *)((char *)p->payload + sizeof(struct ip_hdr)); ++ uint8_t flags = TCPH_FLAGS(tcp_hdr); ++ ++ head = pbuf_alloc(0, p->len, PBUF_RAM); ++ if (head == NULL) { ++ LSTACK_LOG(ERR, LSTACK, "netif_loop_output: pbuf_alloc failed\n"); ++ return ERR_MEM; ++ } ++ head->ol_flags = p->ol_flags; ++ memcpy_s(head->payload, head->len, p->payload, p->len); ++ ++ if ((flags & TCP_SYN) && !(flags & TCP_ACK)) { ++ /* SYN packet, send to listen_ring */ ++ char ring_name[RING_NAME_LEN] = {0}; ++ snprintf_s(ring_name, sizeof(ring_name), sizeof(ring_name) - 1, "listen_rx_ring_%d", pcb->remote_port); ++ struct rte_ring *ring = rte_ring_lookup(ring_name); ++ if (ring == NULL) { ++ LSTACK_LOG(INFO, LSTACK, "netif_loop_output: cant find listen_rx_ring %d\n", pcb->remote_port); ++ pbuf_free(head); ++ } else { ++ if (rte_ring_mp_enqueue(ring, head) != 0) { ++ LSTACK_LOG(INFO, LSTACK, "enqueue sync packet failed\n"); ++ pbuf_free(head); ++ } ++ } + } else { +- if (rte_ring_mp_enqueue(ring, head) != 0) { +- LSTACK_LOG(INFO, LSTACK, "enqueue sync packet failed\n"); ++ /* send other type packet to tx_ring */ ++ if (rte_ring_sp_enqueue(pcb->client_tx_ring, head) != 0) { ++ LSTACK_LOG(INFO, LSTACK, "client tx ring full\n"); + pbuf_free(head); + } + } +- } else { +- /* send other type packet to tx_ring */ +- if (rte_ring_sp_enqueue(pcb->client_tx_ring, head) != 0) { +- LSTACK_LOG(INFO, LSTACK, "client tx ring full\n"); +- pbuf_free(head); +- } +- } + +- return ERR_OK; ++ return ERR_OK; + } + + err_t find_same_node_memzone(struct tcp_pcb *pcb, struct lwip_sock *nsock) + { +- char name[RING_NAME_LEN]; +- snprintf_s(name, sizeof(name), sizeof(name) - 1, "rte_mz_rx_%u", pcb->remote_port); +- if ((nsock->same_node_tx_ring_mz = rte_memzone_lookup(name)) == NULL) { +- LSTACK_LOG(INFO, LSTACK, "lwip_accept: can't find %s\n",name); +- return -1; +- } else { +- LSTACK_LOG(INFO, LSTACK, "lookup %s success\n", name); +- } +- nsock->same_node_tx_ring = (struct same_node_ring *)nsock->same_node_tx_ring_mz->addr; +- +- snprintf_s(name, sizeof(name), sizeof(name) - 1, "rte_mz_buf_rx_%u", pcb->remote_port); +- if ((nsock->same_node_tx_ring->mz = rte_memzone_lookup(name)) == NULL) { +- LSTACK_LOG(INFO, LSTACK, "lwip_accept: can't find %s\n",name); +- return -1; +- } +- +- snprintf_s(name, sizeof(name), sizeof(name) - 1, "rte_mz_tx_%u", pcb->remote_port); +- if ((nsock->same_node_rx_ring_mz = rte_memzone_lookup(name)) == NULL) { +- LSTACK_LOG(INFO, LSTACK, "lwip_accept: can't find %s\n",name); +- return -1; +- } else { +- LSTACK_LOG(INFO, LSTACK, "lookup %s success\n", name); +- } +- nsock->same_node_rx_ring = (struct same_node_ring *)nsock->same_node_rx_ring_mz->addr; +- +- snprintf_s(name, sizeof(name), sizeof(name) - 1,"rte_mz_buf_tx_%u", pcb->remote_port); +- if ((nsock->same_node_rx_ring->mz = rte_memzone_lookup(name)) == NULL) { +- LSTACK_LOG(INFO, LSTACK, "lwip_accept: can't find %s\n",name); +- return -1; +- } +- +- /* rcvlink init in alloc_socket() */ +- /* remove from g_rcv_process_list in free_socket */ +- list_add_node(&nsock->stack->same_node_recv_list, &nsock->recv_list); +- return 0; ++ char name[RING_NAME_LEN]; ++ snprintf_s(name, sizeof(name), sizeof(name) - 1, "rte_mz_rx_%u", pcb->remote_port); ++ if ((nsock->same_node_tx_ring_mz = rte_memzone_lookup(name)) == NULL) { ++ LSTACK_LOG(INFO, LSTACK, "lwip_accept: can't find %s\n",name); ++ return -1; ++ } else { ++ LSTACK_LOG(INFO, LSTACK, "lookup %s success\n", name); ++ } ++ nsock->same_node_tx_ring = (struct same_node_ring *)nsock->same_node_tx_ring_mz->addr; ++ ++ snprintf_s(name, sizeof(name), sizeof(name) - 1, "rte_mz_buf_rx_%u", pcb->remote_port); ++ if ((nsock->same_node_tx_ring->mz = rte_memzone_lookup(name)) == NULL) { ++ LSTACK_LOG(INFO, LSTACK, "lwip_accept: can't find %s\n",name); ++ return -1; ++ } ++ ++ snprintf_s(name, sizeof(name), sizeof(name) - 1, "rte_mz_tx_%u", pcb->remote_port); ++ if ((nsock->same_node_rx_ring_mz = rte_memzone_lookup(name)) == NULL) { ++ LSTACK_LOG(INFO, LSTACK, "lwip_accept: can't find %s\n",name); ++ return -1; ++ } else { ++ LSTACK_LOG(INFO, LSTACK, "lookup %s success\n", name); ++ } ++ nsock->same_node_rx_ring = (struct same_node_ring *)nsock->same_node_rx_ring_mz->addr; ++ ++ snprintf_s(name, sizeof(name), sizeof(name) - 1,"rte_mz_buf_tx_%u", pcb->remote_port); ++ if ((nsock->same_node_rx_ring->mz = rte_memzone_lookup(name)) == NULL) { ++ LSTACK_LOG(INFO, LSTACK, "lwip_accept: can't find %s\n",name); ++ return -1; ++ } ++ ++ /* rcvlink init in alloc_socket() */ ++ /* remove from g_rcv_process_list in free_socket */ ++ list_add_node(&nsock->stack->same_node_recv_list, &nsock->recv_list); ++ return 0; + } + + err_t same_node_memzone_create(const struct rte_memzone **zone, int size, int port, char *name, char *rx) +@@ -1469,7 +1473,8 @@ err_t same_node_memzone_create(const struct rte_memzone **zone, int size, int po + return ERR_MEM; + } + +- LSTACK_LOG(INFO, LSTACK, "lstack id %d, reserve %s(%p) success, addr is %p, size is %u\n", rte_socket_id(), mem_name, *zone, (*zone)->addr, size); ++ LSTACK_LOG(INFO, LSTACK, "lstack id %d, reserve %s(%p) success, addr is %p, size is %u\n", ++ rte_socket_id(), mem_name, *zone, (*zone)->addr, size); + + return ERR_OK; + } +@@ -1496,16 +1501,16 @@ err_t same_node_ring_create(struct rte_ring **ring, int size, int port, char *na + + static void init_same_node_ring(struct tcp_pcb *pcb) + { +- struct netconn *netconn = (struct netconn *)pcb->callback_arg; +- struct lwip_sock *sock = get_socket(netconn->socket); +- +- pcb->client_rx_ring = NULL; +- pcb->client_tx_ring = NULL; +- pcb->free_ring = 0; +- sock->same_node_rx_ring = NULL; +- sock->same_node_rx_ring_mz = NULL; +- sock->same_node_tx_ring = NULL; +- sock->same_node_tx_ring_mz = NULL; ++ struct netconn *netconn = (struct netconn *)pcb->callback_arg; ++ struct lwip_sock *sock = get_socket(netconn->socket); ++ ++ pcb->client_rx_ring = NULL; ++ pcb->client_tx_ring = NULL; ++ pcb->free_ring = 0; ++ sock->same_node_rx_ring = NULL; ++ sock->same_node_rx_ring_mz = NULL; ++ sock->same_node_tx_ring = NULL; ++ sock->same_node_tx_ring_mz = NULL; + } + + #define CLIENT_RING_SIZE 512 +@@ -1522,24 +1527,28 @@ err_t create_same_node_ring(struct tcp_pcb *pcb) + } + pcb->free_ring = 1; + +- if (same_node_memzone_create(&sock->same_node_rx_ring_mz, sizeof(struct same_node_ring), pcb->local_port, "rte_mz", "rx") != 0) { ++ if (same_node_memzone_create(&sock->same_node_rx_ring_mz, sizeof(struct same_node_ring), ++ pcb->local_port, "rte_mz", "rx") != 0) { + goto END; + } + sock->same_node_rx_ring = (struct same_node_ring*)sock->same_node_rx_ring_mz->addr; + +- if (same_node_memzone_create(&sock->same_node_rx_ring->mz, SAME_NODE_RING_LEN, pcb->local_port, "rte_mz_buf", "rx") != 0) { ++ if (same_node_memzone_create(&sock->same_node_rx_ring->mz, SAME_NODE_RING_LEN, ++ pcb->local_port, "rte_mz_buf", "rx") != 0) { + goto END; + } + + sock->same_node_rx_ring->sndbegin = 0; + sock->same_node_rx_ring->sndend = 0; + +- if (same_node_memzone_create(&sock->same_node_tx_ring_mz, sizeof(struct same_node_ring), pcb->local_port, "rte_mz", "tx") != 0) { ++ if (same_node_memzone_create(&sock->same_node_tx_ring_mz, sizeof(struct same_node_ring), ++ pcb->local_port, "rte_mz", "tx") != 0) { + goto END; + } + sock->same_node_tx_ring = (struct same_node_ring*)sock->same_node_tx_ring_mz->addr; + +- if (same_node_memzone_create(&sock->same_node_tx_ring->mz, SAME_NODE_RING_LEN, pcb->local_port, "rte_mz_buf", "tx") != 0) { ++ if (same_node_memzone_create(&sock->same_node_tx_ring->mz, SAME_NODE_RING_LEN, ++ pcb->local_port, "rte_mz_buf", "tx") != 0) { + goto END; + } + +@@ -1560,20 +1569,21 @@ END: + + err_t find_same_node_ring(struct tcp_pcb *npcb) + { +- char name[RING_NAME_LEN] = {0}; +- snprintf_s(name, sizeof(name), sizeof(name) - 1, "client_tx_ring_%u", npcb->remote_port); +- npcb->client_rx_ring = rte_ring_lookup(name); +- memset_s(name, sizeof(name), 0, sizeof(name)); +- snprintf_s(name, sizeof(name), sizeof(name) - 1, "client_rx_ring_%u", npcb->remote_port); +- npcb->client_tx_ring = rte_ring_lookup(name); +- npcb->free_ring = 0; +- if (npcb->client_tx_ring == NULL || +- npcb->client_rx_ring == NULL) { +- LSTACK_LOG(INFO, LSTACK, "lookup client rxtx ring failed, port is %d\n", npcb->remote_port); +- tcp_abandon(npcb, 0); +- return ERR_CONN; +- } else { +- LSTACK_LOG(INFO, LSTACK, "find client_tx_ring_%u and client_rx_ring_%u\n", npcb->remote_port, npcb->remote_port); +- } +- return 0; ++ char name[RING_NAME_LEN] = {0}; ++ snprintf_s(name, sizeof(name), sizeof(name) - 1, "client_tx_ring_%u", npcb->remote_port); ++ npcb->client_rx_ring = rte_ring_lookup(name); ++ memset_s(name, sizeof(name), 0, sizeof(name)); ++ snprintf_s(name, sizeof(name), sizeof(name) - 1, "client_rx_ring_%u", npcb->remote_port); ++ npcb->client_tx_ring = rte_ring_lookup(name); ++ npcb->free_ring = 0; ++ if (npcb->client_tx_ring == NULL || ++ npcb->client_rx_ring == NULL) { ++ LSTACK_LOG(INFO, LSTACK, "lookup client rxtx ring failed, port is %d\n", npcb->remote_port); ++ tcp_abandon(npcb, 0); ++ return ERR_CONN; ++ } else { ++ LSTACK_LOG(INFO, LSTACK, "find client_tx_ring_%u and client_rx_ring_%u\n", ++ npcb->remote_port, npcb->remote_port); ++ } ++ return 0; + } +diff --git a/src/lstack/core/lstack_protocol_stack.c b/src/lstack/core/lstack_protocol_stack.c +index 7b1b994..d5523df 100644 +--- a/src/lstack/core/lstack_protocol_stack.c ++++ b/src/lstack/core/lstack_protocol_stack.c +@@ -32,11 +32,11 @@ + #include "lstack_ethdev.h" + #include "lstack_vdev.h" + #include "lstack_lwip.h" +-#include "lstack_protocol_stack.h" + #include "lstack_cfg.h" + #include "lstack_control_plane.h" + #include "posix/lstack_epoll.h" + #include "lstack_stack_stat.h" ++#include "lstack_protocol_stack.h" + + #define KERNEL_EVENT_100us 100 + +@@ -98,7 +98,6 @@ int get_min_conn_stack(struct protocol_stack_group *stack_group) + min_conn_num = stack->conn_num; + } + } +- + } + return min_conn_stk_idx; + } +@@ -236,7 +235,6 @@ static int32_t create_thread(void *arg, char *thread_name, stack_thread_func fun + LSTACK_LOG(ERR, LSTACK, "set name failed\n"); + return -1; + } +- + } else { + ret = sprintf_s(name, sizeof(name), "%s%02hu", thread_name, t_params->queue_id); + if (ret < 0) { +@@ -307,15 +305,16 @@ static int32_t init_stack_value(struct protocol_stack *stack, void *arg) + + int idx = t_params->idx; + if (get_global_cfg_params()->seperate_send_recv) { ++ // 2: idx is even, stack is recv thread, idx is odd, stack is send thread + if (idx % 2 == 0) { +- stack->cpu_id = get_global_cfg_params()->recv_cpus[idx/2]; ++ stack->cpu_id = get_global_cfg_params()->recv_cpus[idx / 2]; + stack->is_send_thread = 0; +- }else { +- stack->cpu_id = get_global_cfg_params()->send_cpus[idx/2]; ++ } else { ++ stack->cpu_id = get_global_cfg_params()->send_cpus[idx / 2]; + stack->is_send_thread = 1; + } +- }else { +- stack->cpu_id = get_global_cfg_params()->cpus[idx]; ++ } else { ++ stack->cpu_id = get_global_cfg_params()->cpus[idx]; + } + + stack->socket_id = numa_node_of_cpu(stack->cpu_id); +@@ -355,7 +354,7 @@ static int32_t create_affiliate_thread(void *arg) + + static struct protocol_stack *stack_thread_init(void *arg) + { +- struct protocol_stack_group *stack_group = get_protocol_stack_group(); ++ struct protocol_stack_group *stack_group = get_protocol_stack_group(); + struct protocol_stack *stack = calloc(1, sizeof(*stack)); + if (stack == NULL) { + LSTACK_LOG(ERR, LSTACK, "malloc stack failed\n"); +@@ -437,7 +436,7 @@ static void* gazelle_stack_thread(void *arg) + + uint16_t queue_id = t_params->queue_id; + struct cfg_params *cfg = get_global_cfg_params(); +- uint8_t use_ltran_flag = cfg->use_ltran;; ++ uint8_t use_ltran_flag = cfg->use_ltran; + bool kni_switch = cfg->kni_switch; + uint32_t read_connect_number = cfg->read_connect_number; + uint32_t rpc_number = cfg->rpc_number; +@@ -502,7 +501,7 @@ static void* gazelle_stack_thread(void *arg) + + static void libnet_listen_thread(void *arg) + { +- struct cfg_params * cfg_param = get_global_cfg_params(); ++ struct cfg_params *cfg_param = get_global_cfg_params(); + recv_pkts_from_other_process(cfg_param->process_idx, arg); + } + +@@ -542,7 +541,7 @@ int32_t init_protocol_stack(void) + + if (!get_global_cfg_params()->seperate_send_recv) { + stack_group->stack_num = get_global_cfg_params()->num_cpu; +- }else { ++ } else { + stack_group->stack_num = get_global_cfg_params()->num_cpu * 2; + } + +@@ -550,7 +549,6 @@ int32_t init_protocol_stack(void) + pthread_spin_init(&stack_group->poll_list_lock, PTHREAD_PROCESS_PRIVATE); + pthread_spin_init(&stack_group->socket_lock, PTHREAD_PROCESS_PRIVATE); + +- + if (init_protocol_sem() != 0) { + return -1; + } +@@ -559,9 +557,10 @@ int32_t init_protocol_stack(void) + int process_index = get_global_cfg_params()->process_idx; + + if (get_global_cfg_params()->is_primary) { ++ uint32_t total_mbufs = get_global_cfg_params()->mbuf_count_per_conn * get_global_cfg_params()->tcp_conn_count; + for (uint16_t idx = 0; idx < get_global_cfg_params()->tot_queue_num; idx++) { + struct rte_mempool* rxtx_mbuf = create_pktmbuf_mempool("rxtx_mbuf", +- get_global_cfg_params()->mbuf_count_per_conn * get_global_cfg_params()->tcp_conn_count / stack_group->stack_num, RXTX_CACHE_SZ, idx); ++ total_mbufs / stack_group->stack_num, RXTX_CACHE_SZ, idx); + if (rxtx_mbuf == NULL) { + return -1; + } +@@ -572,12 +571,12 @@ int32_t init_protocol_stack(void) + for (uint32_t i = 0; i < queue_num; i++) { + if (get_global_cfg_params()->seperate_send_recv) { + if (i % 2 == 0) { +- ret = sprintf_s(name, sizeof(name), "%s_%d_%d", LSTACK_RECV_THREAD_NAME, process_index, i/2); ++ ret = sprintf_s(name, sizeof(name), "%s_%d_%d", LSTACK_RECV_THREAD_NAME, process_index, i / 2); + if (ret < 0) { + return -1; + } + } else { +- ret = sprintf_s(name, sizeof(name), "%s_%d_%d", LSTACK_SEND_THREAD_NAME, process_index, i/2); ++ ret = sprintf_s(name, sizeof(name), "%s_%d_%d", LSTACK_SEND_THREAD_NAME, process_index, i / 2); + if (ret < 0) { + return -1; + } +@@ -601,17 +600,18 @@ int32_t init_protocol_stack(void) + + wait_sem_value(&stack_group->thread_phase1, stack_group->stack_num); + +- for(int idx = 0; idx < queue_num; idx++){ ++ for (int idx = 0; idx < queue_num; idx++){ + free(t_params[idx]); + } + +- if (!use_ltran()) { ++ if (!use_ltran()) { + ret = sem_init(&stack_group->sem_listen_thread, 0, 0); + ret = sprintf_s(name, sizeof(name), "%s", "listen_thread"); +- struct sys_thread *thread = sys_thread_new(name, libnet_listen_thread, (void*)(&stack_group->sem_listen_thread), 0, 0); ++ struct sys_thread *thread = sys_thread_new(name, libnet_listen_thread, ++ (void*)(&stack_group->sem_listen_thread), 0, 0); + free(thread); + sem_wait(&stack_group->sem_listen_thread); +- } ++ } + + if (get_init_fail()) { + return -1; +@@ -888,7 +888,7 @@ int32_t stack_broadcast_listen(int32_t fd, int32_t backlog) + + if (min_conn_stk_idx == i) { + get_socket_by_fd(clone_fd)->conn->is_master_fd = 1; +- }else { ++ } else { + get_socket_by_fd(clone_fd)->conn->is_master_fd = 0; + } + +diff --git a/src/lstack/core/lstack_stack_stat.c b/src/lstack/core/lstack_stack_stat.c +index d1c749a..60832b4 100644 +--- a/src/lstack/core/lstack_stack_stat.c ++++ b/src/lstack/core/lstack_stack_stat.c +@@ -25,9 +25,9 @@ + #include "gazelle_dfx_msg.h" + #include "lstack_thread_rpc.h" + #include "lstack_protocol_stack.h" +-#include "lstack_stack_stat.h" + #include "posix/lstack_epoll.h" + #include "lstack_dpdk.h" ++#include "lstack_stack_stat.h" + + #define US_PER_SEC 1000000 + +@@ -55,7 +55,7 @@ void calculate_lstack_latency(struct gazelle_stack_latency *stack_latency, const + const struct latency_timestamp *lt; + + if (pbuf == NULL) { +- return; ++ return; + } + + lt = &pbuf_to_private(pbuf)->lt; +@@ -120,7 +120,8 @@ static void set_latency_start_flag(bool start) + stack->latency.start_time = get_current_time(); + stack->latency.lwip_latency.latency_min = ~((uint64_t)0); + stack->latency.read_latency.latency_min = ~((uint64_t)0); +- memset_s(&stack->aggregate_stats, sizeof(struct gazelle_stack_aggregate_stats), 0, sizeof(stack->aggregate_stats)); ++ memset_s(&stack->aggregate_stats, sizeof(struct gazelle_stack_aggregate_stats), ++ 0, sizeof(stack->aggregate_stats)); + } + } + +@@ -218,7 +219,8 @@ static void get_stack_dfx_data(struct gazelle_stack_dfx_data *dfx, struct protoc + } + break; + case GAZELLE_STAT_LSTACK_SHOW_AGGREGATE: +- ret = memcpy_s(&dfx->data.aggregate_stats, sizeof(dfx->data.aggregate_stats), &stack->aggregate_stats, sizeof(stack->aggregate_stats)); ++ ret = memcpy_s(&dfx->data.aggregate_stats, sizeof(dfx->data.aggregate_stats), ++ &stack->aggregate_stats, sizeof(stack->aggregate_stats)); + if (ret != EOK) { + LSTACK_LOG(ERR, LSTACK, "memcpy_s err ret=%d \n", ret); + } +diff --git a/src/lstack/include/lstack_dpdk.h b/src/lstack/include/lstack_dpdk.h +index a896903..a1262a5 100644 +--- a/src/lstack/include/lstack_dpdk.h ++++ b/src/lstack/include/lstack_dpdk.h +@@ -53,7 +53,8 @@ int32_t dpdk_init_lstack_kni(void); + void dpdk_restore_pci(void); + bool port_in_stack_queue(uint32_t src_ip, uint32_t dst_ip, uint16_t src_port, uint16_t dst_port); + uint16_t get_port_id(void); +-struct rte_mempool *create_pktmbuf_mempool(const char *name, uint32_t nb_mbuf,uint32_t mbuf_cache_size, uint16_t queue_id); ++struct rte_mempool *create_pktmbuf_mempool(const char *name, uint32_t nb_mbuf, ++ uint32_t mbuf_cache_size, uint16_t queue_id); + + void dpdk_nic_xstats_get(struct gazelle_stack_dfx_data *dfx, uint16_t port_id); + #endif /* GAZELLE_DPDK_H */ +diff --git a/src/lstack/include/lstack_ethdev.h b/src/lstack/include/lstack_ethdev.h +index 7f944eb..326bd1b 100644 +--- a/src/lstack/include/lstack_ethdev.h ++++ b/src/lstack/include/lstack_ethdev.h +@@ -20,10 +20,10 @@ enum port_type { + PORT_CONNECT, + }; + +-enum PACKET_TRANSFER_TYPE{ ++enum PACKET_TRANSFER_TYPE { + TRANSFER_KERNEL = -1, + TRANSFER_OTHER_THREAD, +- TRANSFER_CURRENT_THREAD, ++ TRANSFER_CURRENT_THREAD, + }; + + enum TRANSFER_MESSAGE_RESULT { +diff --git a/src/lstack/include/lstack_lockless_queue.h b/src/lstack/include/lstack_lockless_queue.h +index c70b56a..c6f6f32 100644 +--- a/src/lstack/include/lstack_lockless_queue.h ++++ b/src/lstack/include/lstack_lockless_queue.h +@@ -70,8 +70,9 @@ static inline lockless_queue_node* lockless_queue_mpsc_pop(lockless_queue* queue + lockless_queue_node *next = tail->next; + + if (tail == &queue->stub) { +- if (next == NULL) ++ if (next == NULL) { + return NULL; ++ } + queue->tail = next; + tail = next; + next = next->next; +@@ -83,8 +84,9 @@ static inline lockless_queue_node* lockless_queue_mpsc_pop(lockless_queue* queue + } + + lockless_queue_node *head = queue->head; +- if (tail != head) ++ if (tail != head) { + return NULL; ++ } + + lockless_queue_mpsc_push(queue, &queue->stub); + +diff --git a/src/lstack/include/lstack_protocol_stack.h b/src/lstack/include/lstack_protocol_stack.h +index c75161f..3a447dc 100644 +--- a/src/lstack/include/lstack_protocol_stack.h ++++ b/src/lstack/include/lstack_protocol_stack.h +@@ -138,7 +138,7 @@ void stack_broadcast_clean_epoll(struct wakeup_poll *wakeup); + void stack_send_pkts(struct protocol_stack *stack); + + struct rpc_msg; +-struct thread_params{ ++struct thread_params { + uint16_t queue_id; + uint16_t idx; + }; +diff --git a/src/lstack/include/lstack_vdev.h b/src/lstack/include/lstack_vdev.h +index 540a31a..007eec7 100644 +--- a/src/lstack/include/lstack_vdev.h ++++ b/src/lstack/include/lstack_vdev.h +@@ -23,7 +23,8 @@ int vdev_reg_xmit(enum reg_ring_type type, struct gazelle_quintuple *qtuple); + + int recv_pkts_from_other_process(int process_index, void* arg); + void transfer_delete_rule_info_to_process0(uint32_t dst_ip, uint16_t src_port, uint16_t dst_port); +-void transfer_create_rule_info_to_process0(uint16_t queue_id, uint32_t src_ip, uint32_t dst_ip, uint16_t src_port, uint16_t dst_port); ++void transfer_create_rule_info_to_process0(uint16_t queue_id, uint32_t src_ip, ++ uint32_t dst_ip, uint16_t src_port, uint16_t dst_port); + void transfer_add_or_delete_listen_port_to_process0(uint16_t listen_port, uint8_t process_idx, uint8_t is_add); + void init_listen_and_user_ports(); + +diff --git a/src/lstack/netif/lstack_ethdev.c b/src/lstack/netif/lstack_ethdev.c +index 0a91f79..2bae2f1 100644 +--- a/src/lstack/netif/lstack_ethdev.c ++++ b/src/lstack/netif/lstack_ethdev.c +@@ -21,7 +21,7 @@ + #include + #include + #include +-#include "lwip/tcp.h" ++#include + #include + + #include +@@ -36,37 +36,41 @@ + #include "lstack_lwip.h" + #include "dpdk_common.h" + #include "lstack_protocol_stack.h" +-#include "lstack_ethdev.h" + #include "lstack_thread_rpc.h" ++#include "lstack_ethdev.h" + + /* FRAME_MTU + 14byte header */ +-#define MBUF_MAX_LEN 1514 +-#define MAX_PATTERN_NUM 4 +-#define MAX_ACTION_NUM 2 +-#define FULL_MASK 0xffffffff /* full mask */ +-#define EMPTY_MASK 0x0 /* empty mask */ +-#define LSTACK_MBUF_LEN 64 +-#define TRANSFER_TCP_MUBF_LEN LSTACK_MBUF_LEN + 3 +-#define DELETE_FLOWS_PARAMS_NUM 3 +-#define DELETE_FLOWS_PARAMS_LENGTH 30 +-#define CREATE_FLOWS_PARAMS_NUM 6 +-#define CREATE_FLOWS_PARAMS_LENGTH 60 ++#define MBUF_MAX_LEN 1514 ++#define MAX_PATTERN_NUM 4 ++#define MAX_ACTION_NUM 2 ++#define FULL_MASK 0xffffffff /* full mask */ ++#define EMPTY_MASK 0x0 /* empty mask */ ++#define LSTACK_MBUF_LEN 64 ++#define TRANSFER_TCP_MUBF_LEN (LSTACK_MBUF_LEN + 3) ++#define DELETE_FLOWS_PARAMS_NUM 3 ++#define DELETE_FLOWS_PARAMS_LENGTH 30 ++#define CREATE_FLOWS_PARAMS_NUM 6 ++#define CREATE_FLOWS_PARAMS_LENGTH 60 + #define ADD_OR_DELETE_LISTEN_PORT_PARAMS_LENGTH 25 +-#define ADD_OR_DELETE_LISTEN_PORT_PARAMS_NUM 3 +-#define REPLY_LEN 10 +-#define SUCCESS_REPLY "success" +-#define ERROR_REPLY "error" +-#define PACKET_READ_SIZE 32 ++#define ADD_OR_DELETE_LISTEN_PORT_PARAMS_NUM 3 ++#define REPLY_LEN 10 ++#define SUCCESS_REPLY "success" ++#define ERROR_REPLY "error" ++#define PACKET_READ_SIZE 32 ++ ++#define GET_LSTACK_NUM 14 ++#define GET_LSTACK_NUM_STRING "get_lstack_num" ++ ++#define SERVER_PATH "/var/run/gazelle/server.socket" ++#define SPLIT_DELIM "," + +-#define GET_LSTACK_NUM 14 +-#define GET_LSTACK_NUM_STRING "get_lstack_num" ++#define UNIX_TCP_PORT_MAX 65535 + +-char *client_path = "/var/run/gazelle/client.socket"; +-char *server_path = "/var/run/gazelle/server.socket"; +-const char *split_delim = ","; ++#define IPV4_VERSION_OFFSET 4 ++#define IPV4_VERSION 4 + +-uint8_t g_user_ports[65535] = {INVAILD_PROCESS_IDX,}; +-uint8_t g_listen_ports[65535] = {INVAILD_PROCESS_IDX,}; ++static uint8_t g_user_ports[UNIX_TCP_PORT_MAX] = {INVAILD_PROCESS_IDX, }; ++static uint8_t g_listen_ports[UNIX_TCP_PORT_MAX] = {INVAILD_PROCESS_IDX, }; + + void eth_dev_recv(struct rte_mbuf *mbuf, struct protocol_stack *stack) + { +@@ -173,9 +177,9 @@ void add_rule(char* rule_key, struct rte_flow *flow) + struct flow_rule *rule; + HASH_FIND_STR(g_flow_rules, rule_key, rule); + if (rule == NULL) { +- rule = (struct flow_rule*)malloc(sizeof(struct flow_rule)); +- strcpy_s(rule->rule_key, RULE_KEY_LEN, rule_key); +- HASH_ADD_STR(g_flow_rules, rule_key, rule); ++ rule = (struct flow_rule*)malloc(sizeof(struct flow_rule)); ++ strcpy_s(rule->rule_key, RULE_KEY_LEN, rule_key); ++ HASH_ADD_STR(g_flow_rules, rule_key, rule); + } + rule->flow = flow; + } +@@ -185,8 +189,8 @@ void delete_rule(char* rule_key) + struct flow_rule *rule = NULL; + HASH_FIND_STR(g_flow_rules, rule_key, rule); + if (rule == NULL) { +- HASH_DEL(g_flow_rules, rule); +- free(rule); ++ HASH_DEL(g_flow_rules, rule); ++ free(rule); + } + } + +@@ -204,15 +208,14 @@ int transfer_pkt_to_other_process(char *buf, int process_index, int write_len, b + int ret = 0; + + sockfd = posix_api->socket_fn(AF_UNIX, SOCK_STREAM, 0); +- +- memset_s(&serun, sizeof(serun), 0, sizeof(serun)); ++ memset_s(&serun, sizeof(serun), 0, sizeof(serun)); + serun.sun_family = AF_UNIX; +- sprintf_s(serun.sun_path, PATH_MAX,"%s%d", server_path, process_index); ++ sprintf_s(serun.sun_path, PATH_MAX, "%s%d", SERVER_PATH, process_index); + int32_t len = offsetof(struct sockaddr_un, sun_path) + strlen(serun.sun_path); +- if (posix_api->connect_fn(sockfd, (struct sockaddr *)&serun, len) < 0){ +- return CONNECT_ERROR; ++ if (posix_api->connect_fn(sockfd, (struct sockaddr *)&serun, len) < 0) { ++ return CONNECT_ERROR; + } +- posix_api->write_fn(sockfd, buf, write_len); ++ posix_api->write_fn(sockfd, buf, write_len); + if (need_reply) { + char reply_message[REPLY_LEN]; + int32_t read_result = posix_api->read_fn(sockfd, reply_message, REPLY_LEN); +@@ -233,7 +236,8 @@ int transfer_pkt_to_other_process(char *buf, int process_index, int write_len, b + return ret; + } + +-int32_t check_params_from_primary(void){ ++int32_t check_params_from_primary(void) ++{ + struct cfg_params *cfg = get_global_cfg_params(); + if (cfg->is_primary) { + return 0; +@@ -248,39 +252,40 @@ int32_t check_params_from_primary(void){ + return 0; + } + +-struct rte_flow * +-create_flow_director(uint16_t port_id, uint16_t queue_id, uint32_t src_ip, uint32_t dst_ip, +- uint16_t src_port, uint16_t dst_port, struct rte_flow_error *error) ++struct rte_flow *create_flow_director(uint16_t port_id, uint16_t queue_id, ++ uint32_t src_ip, uint32_t dst_ip, ++ uint16_t src_port, uint16_t dst_port, ++ struct rte_flow_error *error) + { + struct rte_flow_attr attr; +- struct rte_flow_item pattern[MAX_PATTERN_NUM]; +- struct rte_flow_action action[MAX_ACTION_NUM]; +- struct rte_flow *flow = NULL; +- struct rte_flow_action_queue queue = { .index = queue_id }; +- struct rte_flow_item_ipv4 ip_spec; +- struct rte_flow_item_ipv4 ip_mask; ++ struct rte_flow_item pattern[MAX_PATTERN_NUM]; ++ struct rte_flow_action action[MAX_ACTION_NUM]; ++ struct rte_flow *flow = NULL; ++ struct rte_flow_action_queue queue = { .index = queue_id }; ++ struct rte_flow_item_ipv4 ip_spec; ++ struct rte_flow_item_ipv4 ip_mask; + + struct rte_flow_item_tcp tcp_spec; + struct rte_flow_item_tcp tcp_mask; +- int res; ++ int res; + +- memset_s(pattern, sizeof(pattern), 0, sizeof(pattern)); +- memset_s(action, sizeof(action), 0, sizeof(action)); ++ memset_s(pattern, sizeof(pattern), 0, sizeof(pattern)); ++ memset_s(action, sizeof(action), 0, sizeof(action)); + + /* +- * set the rule attribute. +- * in this case only ingress packets will be checked. +- */ +- memset_s(&attr, sizeof(struct rte_flow_attr), 0, sizeof(struct rte_flow_attr)); +- attr.ingress = 1; ++ * set the rule attribute. ++ * in this case only ingress packets will be checked. ++ */ ++ memset_s(&attr, sizeof(struct rte_flow_attr), 0, sizeof(struct rte_flow_attr)); ++ attr.ingress = 1; + + /* +- * create the action sequence. +- * one action only, move packet to queue +- */ +- action[0].type = RTE_FLOW_ACTION_TYPE_QUEUE; +- action[0].conf = &queue; +- action[1].type = RTE_FLOW_ACTION_TYPE_END; ++ * create the action sequence. ++ * one action only, move packet to queue ++ */ ++ action[0].type = RTE_FLOW_ACTION_TYPE_QUEUE; ++ action[0].conf = &queue; ++ action[1].type = RTE_FLOW_ACTION_TYPE_END; + + // not limit eth header + pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH; +@@ -299,7 +304,7 @@ create_flow_director(uint16_t port_id, uint16_t queue_id, uint32_t src_ip, uint3 + // tcp header, full mask 0xffff + memset_s(&tcp_spec, sizeof(struct rte_flow_item_tcp), 0, sizeof(struct rte_flow_item_tcp)); + memset_s(&tcp_mask, sizeof(struct rte_flow_item_tcp), 0, sizeof(struct rte_flow_item_tcp)); +- pattern[2].type = RTE_FLOW_ITEM_TYPE_TCP; ++ pattern[2].type = RTE_FLOW_ITEM_TYPE_TCP; // 2: pattern 2 is tcp header + tcp_spec.hdr.src_port = src_port; + tcp_spec.hdr.dst_port = dst_port; + tcp_mask.hdr.src_port = rte_flow_item_tcp_mask.hdr.src_port; +@@ -308,35 +313,39 @@ create_flow_director(uint16_t port_id, uint16_t queue_id, uint32_t src_ip, uint3 + pattern[2].mask = &tcp_mask; + + /* the final level must be always type end */ +- pattern[3].type = RTE_FLOW_ITEM_TYPE_END; ++ pattern[3].type = RTE_FLOW_ITEM_TYPE_END; + res = rte_flow_validate(port_id, &attr, pattern, action, error); +- if (!res){ ++ if (!res) { + flow = rte_flow_create(port_id, &attr, pattern, action, error); +- }else { +- LSTACK_LOG(ERR, PORT,"rte_flow_create.rte_flow_validate error, res %d \n", res); ++ } else { ++ LSTACK_LOG(ERR, PORT, "rte_flow_create.rte_flow_validate error, res %d \n", res); + } + +- return flow; ++ return flow; + } + +-void config_flow_director(uint16_t queue_id, uint32_t src_ip, uint32_t dst_ip, uint16_t src_port, uint16_t dst_port){ +- ++void config_flow_director(uint16_t queue_id, uint32_t src_ip, ++ uint32_t dst_ip, uint16_t src_port, uint16_t dst_port) ++{ + uint16_t port_id = get_port_id(); + char rule_key[RULE_KEY_LEN] = {0}; + sprintf_s(rule_key, sizeof(rule_key), "%u_%u_%u", src_ip, src_port, dst_port); + struct flow_rule *fl_exist = find_rule(rule_key); +- if(fl_exist != NULL){ ++ if (fl_exist != NULL) { + return; + } + +- LSTACK_LOG(INFO, LSTACK, "config_flow_director, flow queue_id %u, src_ip %u,src_port_ntohs:%u, dst_port_ntohs :%u \n", +- queue_id, src_ip,ntohs(src_port), ntohs(dst_port) ); ++ LSTACK_LOG(INFO, LSTACK, ++ "config_flow_director, flow queue_id %u, src_ip %u,src_port_ntohs:%u, dst_port_ntohs:%u\n", ++ queue_id, src_ip, ntohs(src_port), ntohs(dst_port)); + + struct rte_flow_error error; + struct rte_flow *flow = create_flow_director(port_id, queue_id, src_ip, dst_ip, src_port, dst_port, &error); + if (!flow) { +- LSTACK_LOG(ERR, LSTACK,"config_flow_director, flow can not be created. queue_id %u, src_ip %u, src_port %u, dst_port %u, dst_port_ntohs :%u, type %d. message: %s\n", +- queue_id, src_ip,src_port,dst_port,ntohs(dst_port), error.type, error.message ? error.message : "(no stated reason)"); ++ LSTACK_LOG(ERR, LSTACK,"flow can not be created. queue_id %u, src_ip %u, src_port %u," ++ "dst_port %u, dst_port_ntohs :%u, type %d. message: %s\n", ++ queue_id, src_ip, src_port, dst_port, ntohs(dst_port), ++ error.type, error.message ? error.message : "(no stated reason)"); + return; + } + __sync_fetch_and_add(&g_flow_num, 1); +@@ -354,59 +363,61 @@ void delete_flow_director(uint32_t dst_ip, uint16_t src_port, uint16_t dst_port) + struct rte_flow_error error; + int ret = rte_flow_destroy(port_id, fl->flow, &error); + if(ret != 0){ +- LSTACK_LOG(ERR, PORT,"Flow can't be delete %d message: %s\n",error.type,error.message ? error.message : "(no stated reason)"); ++ LSTACK_LOG(ERR, PORT, "Flow can't be delete %d message: %s\n", ++ error.type, error.message ? error.message : "(no stated reason)"); + } + delete_rule(rule_key); + __sync_fetch_and_sub(&g_flow_num, 1); + } + } + +-/* +- * delete flows +- * if process 0, delete directly, else transfer 'dst_ip,src_port,dst_port' to process 0. +- */ ++/* if process 0, delete directly, else transfer 'dst_ip,src_port,dst_port' to process 0. */ + void transfer_delete_rule_info_to_process0(uint32_t dst_ip, uint16_t src_port, uint16_t dst_port) + { +- if (get_global_cfg_params()->is_primary){ ++ if (get_global_cfg_params()->is_primary) { + delete_flow_director(dst_ip, src_port, dst_port); +- }else { ++ } else { + char process_server_path[DELETE_FLOWS_PARAMS_LENGTH]; +- sprintf_s(process_server_path, DELETE_FLOWS_PARAMS_LENGTH, "%u%s%u%s%u", dst_ip,split_delim, src_port,split_delim,dst_port); ++ sprintf_s(process_server_path, DELETE_FLOWS_PARAMS_LENGTH, "%u%s%u%s%u", ++ dst_ip, SPLIT_DELIM, src_port, SPLIT_DELIM, dst_port); + int ret = transfer_pkt_to_other_process(process_server_path, 0, DELETE_FLOWS_PARAMS_LENGTH, false); + if(ret != TRANSFER_SUCESS){ +- LSTACK_LOG(ERR, LSTACK,"transfer_delete_rule_info_to_process0 error. tid %d. dst_ip %u, src_port: %u, dst_port %u\n", ++ LSTACK_LOG(ERR, LSTACK, "error. tid %d. dst_ip %u, src_port: %u, dst_port %u\n", + rte_gettid(), dst_ip, src_port, dst_port); + } + } + } + +-/* +- * add flows +- * if process 0, add directly, else transfer 'src_ip,dst_ip,src_port,dst_port,queue_id' to process 0. +- */ +-void transfer_create_rule_info_to_process0(uint16_t queue_id, uint32_t src_ip, uint32_t dst_ip, uint16_t src_port, uint16_t dst_port) ++// if process 0, add directly, else transfer 'src_ip,dst_ip,src_port,dst_port,queue_id' to process 0. ++void transfer_create_rule_info_to_process0(uint16_t queue_id, uint32_t src_ip, ++ uint32_t dst_ip, uint16_t src_port, ++ uint16_t dst_port) + { + char process_server_path[CREATE_FLOWS_PARAMS_LENGTH]; + /* exchage src_ip and dst_ip, src_port and dst_port */ + uint8_t process_idx = get_global_cfg_params()->process_idx; +- sprintf_s(process_server_path, CREATE_FLOWS_PARAMS_LENGTH, "%u%s%u%s%u%s%u%s%u%s%u", +- dst_ip,split_delim,src_ip,split_delim, dst_port,split_delim,src_port, split_delim,queue_id,split_delim,process_idx); ++ sprintf_s(process_server_path, CREATE_FLOWS_PARAMS_LENGTH, "%u%s%u%s%u%s%u%s%u%s%u", ++ dst_ip, SPLIT_DELIM, src_ip, SPLIT_DELIM, ++ dst_port, SPLIT_DELIM, src_port, SPLIT_DELIM, ++ queue_id, SPLIT_DELIM, process_idx); + int ret = transfer_pkt_to_other_process(process_server_path, 0, CREATE_FLOWS_PARAMS_LENGTH, true); +- if(ret != TRANSFER_SUCESS){ +- LSTACK_LOG(ERR, LSTACK,"transfer_create_rule_info_to_process0 error. tid %d. src_ip %u, dst_ip %u, src_port: %u, dst_port %u, queue_id %u, process_idx %u\n", +- rte_gettid(), src_ip, dst_ip, src_port, dst_port, queue_id, process_idx); ++ if (ret != TRANSFER_SUCESS) { ++ LSTACK_LOG(ERR, LSTACK, "error. tid %d. src_ip %u, dst_ip %u, src_port: %u, dst_port %u," ++ "queue_id %u, process_idx %u\n", ++ rte_gettid(), src_ip, dst_ip, src_port, dst_port, queue_id, process_idx); + } + } + + void transfer_add_or_delete_listen_port_to_process0(uint16_t listen_port, uint8_t process_idx, uint8_t is_add) + { + char process_server_path[ADD_OR_DELETE_LISTEN_PORT_PARAMS_LENGTH]; +- sprintf_s(process_server_path, ADD_OR_DELETE_LISTEN_PORT_PARAMS_LENGTH, "%u%s%u%s%u", listen_port,split_delim,process_idx, split_delim, is_add); ++ sprintf_s(process_server_path, ADD_OR_DELETE_LISTEN_PORT_PARAMS_LENGTH, ++ "%u%s%u%s%u", listen_port, SPLIT_DELIM, process_idx, SPLIT_DELIM, is_add); + int ret = transfer_pkt_to_other_process(process_server_path, 0, ADD_OR_DELETE_LISTEN_PORT_PARAMS_LENGTH, true); + if(ret != TRANSFER_SUCESS) { +- LSTACK_LOG(ERR, LSTACK,"transfer_add_or_delete_listen_port_to_process0 error. tid %d. listen_port %u, process_idx %u\n", +- rte_gettid(), listen_port, process_idx); +- } ++ LSTACK_LOG(ERR, LSTACK, "error. tid %d. listen_port %u, process_idx %u\n", ++ rte_gettid(), listen_port, process_idx); ++ } + } + + static int str_to_array(char *args, uint32_t *array, int size) +@@ -417,7 +428,7 @@ static int str_to_array(char *args, uint32_t *array, int size) + char *next_token = NULL; + + memset_s(array, sizeof(*array) * size, 0, sizeof(*array) * size); +- elem = strtok_s((char *)args, split_delim, &next_token); ++ elem = strtok_s((char *)args, SPLIT_DELIM, &next_token); + while (elem != NULL) { + if (cnt >= size) { + return -1; +@@ -429,7 +440,7 @@ static int str_to_array(char *args, uint32_t *array, int size) + array[cnt] = (uint32_t)val; + cnt++; + +- elem = strtok_s(NULL, split_delim, &next_token); ++ elem = strtok_s(NULL, SPLIT_DELIM, &next_token); + } + + return cnt; +@@ -445,18 +456,20 @@ void parse_and_delete_rule(char* buf) + delete_flow_director(dst_ip, src_port, dst_port); + } + +-void add_user_process_port(uint16_t dst_port, uint8_t process_idx, enum port_type type){ ++void add_user_process_port(uint16_t dst_port, uint8_t process_idx, enum port_type type) ++{ + if (type == PORT_LISTEN) { +- g_listen_ports[dst_port] = process_idx; +- }else { ++ g_listen_ports[dst_port] = process_idx; ++ } else { + g_user_ports[dst_port] = process_idx; + } + } + +-void delete_user_process_port(uint16_t dst_port, enum port_type type){ ++void delete_user_process_port(uint16_t dst_port, enum port_type type) ++{ + if (type == PORT_LISTEN) { + g_listen_ports[dst_port] = INVAILD_PROCESS_IDX; +- }else { ++ } else { + g_user_ports[dst_port] = INVAILD_PROCESS_IDX; + } + } +@@ -483,8 +496,8 @@ void parse_and_add_or_delete_listen_port(char* buf) + uint8_t process_idx = array[1]; + uint8_t is_add = array[2]; + if (is_add == 1) { +- add_user_process_port(listen_port,process_idx, PORT_LISTEN); +- }else { ++ add_user_process_port(listen_port, process_idx, PORT_LISTEN); ++ } else { + delete_user_process_port(listen_port, PORT_LISTEN); + } + +@@ -498,9 +511,9 @@ void transfer_arp_to_other_process(struct rte_mbuf *mbuf) + char arp_mbuf[LSTACK_MBUF_LEN] = {0}; + sprintf_s(arp_mbuf, sizeof(arp_mbuf), "%lu", mbuf); + int result = transfer_pkt_to_other_process(arp_mbuf, i, LSTACK_MBUF_LEN, false); +- if(result == CONNECT_ERROR){ ++ if (result == CONNECT_ERROR) { + LSTACK_LOG(INFO, LSTACK,"connect process %d failed, ensure the process is started.\n", i); +- }else if (result == REPLY_ERROR) { ++ } else if (result == REPLY_ERROR) { + LSTACK_LOG(ERR, LSTACK,"transfer arp pakages to process %d error. %m\n", i); + } + } +@@ -544,9 +557,9 @@ void parse_arp_and_transefer(char* buf) + void parse_tcp_and_transefer(char* buf) + { + char *next_token = NULL; +- char *elem = strtok_s(buf, split_delim, &next_token); ++ char *elem = strtok_s(buf, SPLIT_DELIM, &next_token); + struct rte_mbuf *mbuf = (struct rte_mbuf *) atoll(elem); +- elem = strtok_s(NULL, split_delim, &next_token); ++ elem = strtok_s(NULL, SPLIT_DELIM, &next_token); + uint16_t queue_id = atoll(elem); + + struct protocol_stack_group *stack_group = get_protocol_stack_group(); +@@ -566,8 +579,9 @@ void parse_tcp_and_transefer(char* buf) + transfer_tcp_to_thread(mbuf_copy, stk_index); + } + +-int recv_pkts_from_other_process(int process_index, void* arg){ +- struct sockaddr_un serun, cliun; ++int recv_pkts_from_other_process(int process_index, void* arg) ++{ ++ struct sockaddr_un serun, cliun; + socklen_t cliun_len; + int listenfd, connfd, size; + char buf[132]; +@@ -580,23 +594,23 @@ int recv_pkts_from_other_process(int process_index, void* arg){ + memset_s(&serun, sizeof(serun), 0, sizeof(serun)); + serun.sun_family = AF_UNIX; + char process_server_path[PATH_MAX]; +- sprintf_s(process_server_path, sizeof(process_server_path), "%s%d", server_path, process_index); +- strcpy_s(serun.sun_path, sizeof(serun.sun_path),process_server_path); ++ sprintf_s(process_server_path, sizeof(process_server_path), "%s%d", SERVER_PATH, process_index); ++ strcpy_s(serun.sun_path, sizeof(serun.sun_path), process_server_path); + size = offsetof(struct sockaddr_un, sun_path) + strlen(serun.sun_path); + unlink(process_server_path); +- if (posix_api->bind_fn(listenfd, (struct sockaddr *)&serun, size) < 0) { ++ if (posix_api->bind_fn(listenfd, (struct sockaddr *)&serun, size) < 0) { + perror("bind error"); + return -1; + } + if (posix_api->listen_fn(listenfd, 20) < 0) { /* 20: max backlog */ +- perror("listen error"); ++ perror("listen error"); + return -1; +- } ++ } + sem_post((sem_t *)arg); + /* block */ +- while(1) { ++ while(1) { + cliun_len = sizeof(cliun); +- if ((connfd = posix_api->accept_fn(listenfd, (struct sockaddr *)&cliun, &cliun_len)) < 0){ ++ if ((connfd = posix_api->accept_fn(listenfd, (struct sockaddr *)&cliun, &cliun_len)) < 0) { + perror("accept error"); + continue; + } +@@ -605,11 +619,11 @@ int recv_pkts_from_other_process(int process_index, void* arg){ + if (n < 0) { + perror("read error"); + break; +- } else if(n == 0) { ++ } else if (n == 0) { + break; + } + +- if(n == LSTACK_MBUF_LEN){ ++ if(n == LSTACK_MBUF_LEN) { + /* arp */ + parse_arp_and_transefer(buf); + } else if (n == TRANSFER_TCP_MUBF_LEN) { +@@ -618,17 +632,17 @@ int recv_pkts_from_other_process(int process_index, void* arg){ + } else if (n == DELETE_FLOWS_PARAMS_LENGTH) { + /* delete rule */ + parse_and_delete_rule(buf); +- }else if(n == CREATE_FLOWS_PARAMS_LENGTH){ ++ } else if(n == CREATE_FLOWS_PARAMS_LENGTH) { + /* add rule */ + parse_and_create_rule(buf); + char reply_buf[REPLY_LEN]; + sprintf_s(reply_buf, sizeof(reply_buf), "%s", SUCCESS_REPLY); + posix_api->write_fn(connfd, reply_buf, REPLY_LEN); +- }else if (n == GET_LSTACK_NUM) { ++ } else if (n == GET_LSTACK_NUM) { + char reply_buf[REPLY_LEN]; + sprintf_s(reply_buf, sizeof(reply_buf), "%d", get_global_cfg_params()->num_cpu); + posix_api->write_fn(connfd, reply_buf, REPLY_LEN); +- }else{ ++ } else { + /* add port */ + parse_and_add_or_delete_listen_port(buf); + char reply_buf[REPLY_LEN]; +@@ -643,59 +657,62 @@ int recv_pkts_from_other_process(int process_index, void* arg){ + return 0; + } + +-void concat_mbuf_and_queue_id(struct rte_mbuf *mbuf, uint16_t queue_id, char* mbuf_and_queue_id, int write_len){ +- +- sprintf_s(mbuf_and_queue_id, write_len, "%lu%s%u", mbuf,split_delim,queue_id); ++void concat_mbuf_and_queue_id(struct rte_mbuf *mbuf, uint16_t queue_id, ++ char* mbuf_and_queue_id, int write_len) ++{ ++ sprintf_s(mbuf_and_queue_id, write_len, "%lu%s%u", mbuf, SPLIT_DELIM, queue_id); + } + +-const int32_t ipv4_version_offset = 4; +-const int32_t ipv4_version = 4; +- + int distribute_pakages(struct rte_mbuf *mbuf) + { + struct rte_ipv4_hdr *iph = rte_pktmbuf_mtod_offset(mbuf, struct rte_ipv4_hdr *, sizeof(struct rte_ether_hdr)); +- uint8_t ip_version = (iph->version_ihl & 0xf0) >> ipv4_version_offset; +- if (likely(ip_version == ipv4_version)) { ++ uint8_t ip_version = (iph->version_ihl & 0xf0) >> IPV4_VERSION_OFFSET; ++ if (likely(ip_version == IPV4_VERSION)) { + if (likely(iph->next_proto_id == IPPROTO_TCP)) { + int each_process_queue_num = get_global_cfg_params()->num_queue; + +- struct rte_tcp_hdr *tcp_hdr = rte_pktmbuf_mtod_offset(mbuf, struct rte_tcp_hdr *, sizeof(struct rte_ether_hdr) + +- sizeof(struct rte_ipv4_hdr)); ++ struct rte_tcp_hdr *tcp_hdr = rte_pktmbuf_mtod_offset(mbuf, struct rte_tcp_hdr *, ++ sizeof(struct rte_ether_hdr) + sizeof(struct rte_ipv4_hdr)); + uint16_t dst_port = tcp_hdr->dst_port; ++ uint32_t user_process_idx; + +- int user_process_idx = (g_listen_ports[dst_port] != INVAILD_PROCESS_IDX) ? g_listen_ports[dst_port] : g_user_ports[dst_port]; ++ if (g_listen_ports[dst_port] != INVAILD_PROCESS_IDX) { ++ user_process_idx = g_listen_ports[dst_port]; ++ } else { ++ user_process_idx = g_user_ports[dst_port]; ++ } + + if (user_process_idx == INVAILD_PROCESS_IDX) { + return TRANSFER_KERNEL; + } +- if(unlikely(tcp_hdr->tcp_flags == TCP_SYN)){ ++ if (unlikely(tcp_hdr->tcp_flags == TCP_SYN)) { + uint32_t src_ip = iph->src_addr; + uint16_t src_port = tcp_hdr->src_port; +- uint32_t index = rte_jhash_3words(src_ip, src_port | (dst_port) << 16, 0, 0) % each_process_queue_num; ++ uint32_t index = rte_jhash_3words(src_ip, src_port | ((dst_port) << 16), 0, 0); ++ index = index % each_process_queue_num; + uint16_t queue_id = 0; + if (get_global_cfg_params()->seperate_send_recv) { +- queue_id = user_process_idx * each_process_queue_num + (index/2) * 2; +- }else { ++ queue_id = user_process_idx * each_process_queue_num + (index / 2) * 2; ++ } else { + queue_id = user_process_idx * each_process_queue_num + index; + } +- if(queue_id != 0){ +- if(user_process_idx == 0){ ++ if (queue_id != 0) { ++ if (user_process_idx == 0) { + transfer_tcp_to_thread(mbuf, queue_id); +- }else { ++ } else { + char mbuf_and_queue_id[TRANSFER_TCP_MUBF_LEN]; + concat_mbuf_and_queue_id(mbuf, queue_id, mbuf_and_queue_id, TRANSFER_TCP_MUBF_LEN); +- transfer_pkt_to_other_process(mbuf_and_queue_id, user_process_idx, TRANSFER_TCP_MUBF_LEN, false); ++ transfer_pkt_to_other_process(mbuf_and_queue_id, user_process_idx, ++ TRANSFER_TCP_MUBF_LEN, false); + } + return TRANSFER_OTHER_THREAD; +- }else { ++ } else { + return TRANSFER_CURRENT_THREAD; + } +- }else { ++ } else { + return TRANSFER_CURRENT_THREAD; +- } ++ } + } +- }else { +- return TRANSFER_KERNEL; + } + return TRANSFER_KERNEL; + } +@@ -706,7 +723,7 @@ void kni_handle_rx(uint16_t port_id) + struct rte_kni* kni = get_gazelle_kni(); + uint32_t nb_kni_rx = 0; + if (kni) { +- nb_kni_rx = rte_kni_rx_burst(kni, pkts_burst, PACKET_READ_SIZE); ++ nb_kni_rx = rte_kni_rx_burst(kni, pkts_burst, PACKET_READ_SIZE); + } + if (nb_kni_rx > 0) { + uint16_t nb_rx = rte_eth_tx_burst(port_id, 0, pkts_burst, nb_kni_rx); +@@ -777,12 +794,11 @@ int32_t gazelle_eth_dev_poll(struct protocol_stack *stack, uint8_t use_ltran_fla + + if (likely(transfer_type == TRANSFER_CURRENT_THREAD)) { + eth_dev_recv(stack->pkts[i], stack); +- + } else if (transfer_type == TRANSFER_KERNEL) { + kni_handle_tx(stack->pkts[i]); + } else { +- /*transfer to other thread*/ +- } ++ /* transfer to other thread */ ++ } + } + + stack->stats.rx += nr_pkts; +diff --git a/src/lstack/netif/lstack_vdev.c b/src/lstack/netif/lstack_vdev.c +index aef6035..5ea1f31 100644 +--- a/src/lstack/netif/lstack_vdev.c ++++ b/src/lstack/netif/lstack_vdev.c +@@ -43,7 +43,7 @@ + #define IPV4_MASK (0xf0) + #define IPV4_VERION (0x40) + +-#define TCP_HDR_LEN(tcp_hdr) ((tcp_hdr->data_off & 0xf0) >> 2) ++#define TCP_HDR_LEN(tcp_hdr) (((tcp_hdr)->data_off & 0xf0) >> 2) + + static uint32_t ltran_rx_poll(struct protocol_stack *stack, struct rte_mbuf **pkts, uint32_t max_mbuf) + { +@@ -156,7 +156,8 @@ int32_t vdev_reg_xmit(enum reg_ring_type type, struct gazelle_quintuple *qtuple) + if (get_global_cfg_params()->is_primary) { + delete_user_process_port(qtuple->src_port, PORT_LISTEN); + } else { +- transfer_add_or_delete_listen_port_to_process0(qtuple->src_port,get_global_cfg_params()->process_idx, 0); ++ transfer_add_or_delete_listen_port_to_process0(qtuple->src_port, ++ get_global_cfg_params()->process_idx, 0); + } + } + +@@ -174,13 +175,15 @@ int32_t vdev_reg_xmit(enum reg_ring_type type, struct gazelle_quintuple *qtuple) + + if (type == REG_RING_TCP_CONNECT) { + uint16_t queue_id = get_protocol_stack()->queue_id; +- if (get_global_cfg_params()->is_primary){ ++ if (get_global_cfg_params()->is_primary) { + add_user_process_port(qtuple->src_port, get_global_cfg_params()->process_idx, PORT_CONNECT); + if (queue_id != 0) { +- transfer_create_rule_info_to_process0(queue_id, qtuple->src_ip, qtuple->dst_ip, qtuple->src_port, qtuple->dst_port); ++ transfer_create_rule_info_to_process0(queue_id, qtuple->src_ip, qtuple->dst_ip, ++ qtuple->src_port, qtuple->dst_port); + } + } else { +- transfer_create_rule_info_to_process0(queue_id, qtuple->src_ip, qtuple->dst_ip, qtuple->src_port, qtuple->dst_port); ++ transfer_create_rule_info_to_process0(queue_id, qtuple->src_ip, qtuple->dst_ip, ++ qtuple->src_port, qtuple->dst_port); + } + } + +@@ -188,7 +191,8 @@ int32_t vdev_reg_xmit(enum reg_ring_type type, struct gazelle_quintuple *qtuple) + if (get_global_cfg_params()->is_primary) { + add_user_process_port(qtuple->src_port, get_global_cfg_params()->process_idx, PORT_LISTEN); + } else { +- transfer_add_or_delete_listen_port_to_process0(qtuple->src_port, get_global_cfg_params()->process_idx, 1); ++ transfer_add_or_delete_listen_port_to_process0(qtuple->src_port, ++ get_global_cfg_params()->process_idx, 1); + } + } + return 0; +diff --git a/src/ltran/ltran_dfx.c b/src/ltran/ltran_dfx.c +index 944675e..e41c6db 100644 +--- a/src/ltran/ltran_dfx.c ++++ b/src/ltran/ltran_dfx.c +@@ -138,19 +138,17 @@ static void gazelle_print_lstack_xstats(void *buf, const struct gazelle_stat_msg + static const char *nic_stats_border = "########################"; + + printf("###### NIC extended statistics for port %-2d #########\n", xstats->port_id); +- printf("%s############################\n",nic_stats_border); ++ printf("%s############################\n", nic_stats_border); + if (xstats->len <= 0 || xstats->len > RTE_ETH_XSTATS_MAX_LEN) { + printf("xstats item(%d) num error!\n", xstats->len); + return; + } + + for (uint32_t i = 0; i < xstats->len; i++) { +- printf("%s: %"PRIu64"\n", xstats->xstats_name[i].name, +- xstats->values[i]); ++ printf("%s: %"PRIu64"\n", xstats->xstats_name[i].name, xstats->values[i]); + } + +- printf("%s############################\n", +- nic_stats_border); ++ printf("%s############################\n", nic_stats_border); + } + + static void gazelle_print_ltran_conn(void *buf, const struct gazelle_stat_msg_request *req_msg) +@@ -1110,7 +1108,7 @@ static void gazelle_print_lstack_aggregate(void *buf, const struct gazelle_stat_ + break; + } + ret = dfx_stat_read_from_ltran(buf, sizeof(struct gazelle_stack_dfx_data), req_msg->stat_mode); +- } while(true); ++ } while (true); + } + + static int32_t parse_dfx_ltran_args(int32_t argc, char *argv[], struct gazelle_stat_msg_request *req_msg) +diff --git a/src/ltran/ltran_monitor.c b/src/ltran/ltran_monitor.c +index bd047fc..d163a47 100644 +--- a/src/ltran/ltran_monitor.c ++++ b/src/ltran/ltran_monitor.c +@@ -31,8 +31,8 @@ + #include "ltran_instance.h" + #include "gazelle_dfx_msg.h" + #include "gazelle_base_func.h" +-#include "ltran_monitor.h" + #include "ltran_param.h" ++#include "ltran_monitor.h" + + #define GAZELLE_LISTEN_BACKLOG 5 + +diff --git a/src/ltran/ltran_param.c b/src/ltran/ltran_param.c +index 1eb4f48..e16e648 100644 +--- a/src/ltran/ltran_param.c ++++ b/src/ltran/ltran_param.c +@@ -20,12 +20,12 @@ + #include + #include + +-#include "ltran_param.h" + #include "ltran_errno.h" + #include "ltran_base.h" + #include "ltran_log.h" + #include "gazelle_dfx_msg.h" + #include "gazelle_base_func.h" ++#include "ltran_param.h" + + #define HEX_BASE 16 + +diff --git a/src/ltran/main.c b/src/ltran/main.c +index 03b3ad5..87f1e14 100644 +--- a/src/ltran/main.c ++++ b/src/ltran/main.c +@@ -61,11 +61,11 @@ static void sig_default_handler(int32_t sig) + int ret = 0; + ret = unlink(get_ltran_config()->unix_socket_filename); + if (ret) { +- LTRAN_WARN("unlink %s ERROR. errn: %d. ret=%d\n", get_ltran_config()->unix_socket_filename, errno, ret); ++ LTRAN_WARN("unlink %s ERROR. errn: %d. ret=%d\n", get_ltran_config()->unix_socket_filename, errno, ret); + } + ret = unlink(get_ltran_config()->dfx_socket_filename); + if (ret) { +- LTRAN_WARN("unlink %s ERROR. errn: %d. ret=%d\n", get_ltran_config()->dfx_socket_filename, errno, ret); ++ LTRAN_WARN("unlink %s ERROR. errn: %d. ret=%d\n", get_ltran_config()->dfx_socket_filename, errno, ret); + } + kill(getpid(), sig); + } +-- +2.33.0 + diff --git a/0239-add-udp-multicast-support-in-gazelle.patch b/0239-add-udp-multicast-support-in-gazelle.patch new file mode 100644 index 0000000..07a46a1 --- /dev/null +++ b/0239-add-udp-multicast-support-in-gazelle.patch @@ -0,0 +1,518 @@ +From 2c82e9a9f745f7a959014f788059ac79181b9440 Mon Sep 17 00:00:00 2001 +From: kircher +Date: Sat, 13 May 2023 14:32:02 +0800 +Subject: [PATCH] add udp multicast support in gazelle + +--- + src/lstack/api/lstack_wrap.c | 75 ++++++++++++++++++++++--- + src/lstack/core/lstack_lwip.c | 94 ++++++++++++++++++++++++-------- + src/lstack/include/lstack_lwip.h | 9 ++- + src/lstack/lstack.Makefile | 4 +- + src/lstack/netif/lstack_ethdev.c | 6 +- + src/ltran/ltran_stat.c | 8 ++- + 6 files changed, 159 insertions(+), 37 deletions(-) + +diff --git a/src/lstack/api/lstack_wrap.c b/src/lstack/api/lstack_wrap.c +index 98632c0..505e33d 100644 +--- a/src/lstack/api/lstack_wrap.c ++++ b/src/lstack/api/lstack_wrap.c +@@ -81,14 +81,21 @@ static inline enum KERNEL_LWIP_PATH select_path(int fd, struct lwip_sock **socke + return PATH_LWIP; + } + +- struct tcp_pcb *pcb = sock->conn->pcb.tcp; +- /* after lwip connect, call send immediately, pcb->state is SYN_SENT, need return PATH_LWIP */ +- /* pcb->state default value is CLOSED when call socket, need return PATH_UNKNOW */ +- if (pcb != NULL && pcb->state <= ESTABLISHED && pcb->state >= LISTEN) { ++ if (NETCONN_IS_UDP(sock)) { + if (socket) { + *socket = sock; + } + return PATH_LWIP; ++ } else { ++ struct tcp_pcb *pcb = sock->conn->pcb.tcp; ++ /* after lwip connect, call send immediately, pcb->state is SYN_SENT, need return PATH_LWIP */ ++ /* pcb->state default value is CLOSED when call socket, need return PATH_UNKNOW */ ++ if (pcb != NULL && pcb->state <= ESTABLISHED && pcb->state >= LISTEN) { ++ if (socket) { ++ *socket = sock; ++ } ++ return PATH_LWIP; ++ } + } + + return PATH_UNKNOW; +@@ -396,7 +403,7 @@ static inline int32_t do_setsockopt(int32_t s, int32_t level, int32_t optname, c + static inline int32_t do_socket(int32_t domain, int32_t type, int32_t protocol) + { + if ((domain != AF_INET && domain != AF_UNSPEC) +- || posix_api->ues_posix || ((type & SOCK_TYPE_MASK) & ~SOCK_STREAM)) { ++ || posix_api->ues_posix) { + return posix_api->socket_fn(domain, type, protocol); + } + +@@ -415,7 +422,7 @@ static inline ssize_t do_recv(int32_t sockfd, void *buf, size_t len, int32_t fla + + struct lwip_sock *sock = NULL; + if (select_path(sockfd, &sock) == PATH_LWIP) { +- return read_stack_data(sockfd, buf, len, flags); ++ return read_stack_data(sockfd, buf, len, flags, NULL, NULL); + } + + return posix_api->recv_fn(sockfd, buf, len, flags); +@@ -433,7 +440,7 @@ static inline ssize_t do_read(int32_t s, void *mem, size_t len) + + struct lwip_sock *sock = NULL; + if (select_path(s, &sock) == PATH_LWIP) { +- return read_stack_data(s, mem, len, 0); ++ return read_stack_data(s, mem, len, 0, NULL, NULL); + } + return posix_api->read_fn(s, mem, len); + } +@@ -469,7 +476,7 @@ static inline ssize_t do_send(int32_t sockfd, const void *buf, size_t len, int32 + return posix_api->send_fn(sockfd, buf, len, flags); + } + +- return gazelle_send(sockfd, buf, len, flags); ++ return gazelle_send(sockfd, buf, len, flags, NULL, 0); + } + + static inline ssize_t do_write(int32_t s, const void *mem, size_t size) +@@ -479,7 +486,7 @@ static inline ssize_t do_write(int32_t s, const void *mem, size_t size) + return posix_api->write_fn(s, mem, size); + } + +- return gazelle_send(s, mem, size, 0); ++ return gazelle_send(s, mem, size, 0, NULL, 0); + } + + static inline ssize_t do_writev(int32_t s, const struct iovec *iov, int iovcnt) +@@ -529,6 +536,36 @@ static inline ssize_t do_sendmsg(int32_t s, const struct msghdr *message, int32_ + return posix_api->send_msg(s, message, flags); + } + ++static inline ssize_t do_recvfrom(int32_t sockfd, void *buf, size_t len, int32_t flags, ++ struct sockaddr *addr, socklen_t *addrlen) ++{ ++ if (buf == NULL) { ++ GAZELLE_RETURN(EINVAL); ++ } ++ ++ if (len == 0) { ++ return 0; ++ } ++ ++ struct lwip_sock *sock = NULL; ++ if (select_path(sockfd, &sock) == PATH_LWIP) { ++ return read_stack_data(sockfd, buf, len, flags, addr, addrlen); ++ } ++ ++ return posix_api->recv_from(sockfd, buf, len, flags, addr, addrlen); ++} ++ ++static inline ssize_t do_sendto(int32_t sockfd, const void *buf, size_t len, int32_t flags, ++ const struct sockaddr *addr, socklen_t addrlen) ++{ ++ struct lwip_sock *sock = NULL; ++ if (select_path(sockfd, &sock) != PATH_LWIP) { ++ return posix_api->send_to(sockfd, buf, len, flags, addr, addrlen); ++ } ++ ++ return gazelle_send(sockfd, buf, len, flags, addr, addrlen); ++} ++ + static inline int32_t do_close(int32_t s) + { + struct lwip_sock *sock = NULL; +@@ -716,6 +753,16 @@ ssize_t sendmsg(int32_t s, const struct msghdr *message, int32_t flags) + { + return do_sendmsg(s, message, flags); + } ++ssize_t recvfrom(int32_t sockfd, void *buf, size_t len, int32_t flags, ++ struct sockaddr *addr, socklen_t *addrlen) ++{ ++ return do_recvfrom(sockfd, buf, len, flags, addr, addrlen); ++} ++ssize_t sendto(int32_t sockfd, const void *buf, size_t len, int32_t flags, ++ const struct sockaddr *addr, socklen_t addrlen) ++{ ++ return do_sendto(sockfd, buf, len, flags, addr, addrlen); ++} + int32_t close(int32_t s) + { + return do_close(s); +@@ -835,6 +882,16 @@ ssize_t __wrap_sendmsg(int32_t s, const struct msghdr *message, int32_t flags) + { + return do_sendmsg(s, message, flags); + } ++ssize_t __wrap_recvfrom(int32_t sockfd, void *buf, size_t len, int32_t flags, ++ struct sockaddr *addr, socklen_t *addrlen) ++{ ++ return do_recvfrom(sockfd, buf, len, flags, addr, addrlen); ++} ++ssize_t __wrap_sendto(int32_t sockfd, const void *buf, size_t len, int32_t flags, ++ const struct sockaddr *addr, socklen_t addrlen) ++{ ++ return do_sendto(sockfd, buf, len, flags, addr, addrlen); ++} + int32_t __wrap_close(int32_t s) + { + return do_close(s); +diff --git a/src/lstack/core/lstack_lwip.c b/src/lstack/core/lstack_lwip.c +index b6c5813..12c2aa6 100644 +--- a/src/lstack/core/lstack_lwip.c ++++ b/src/lstack/core/lstack_lwip.c +@@ -14,11 +14,13 @@ + #include + #include + #include ++#include + #include + #include + #include + #include + #include ++#include + #include + #include + #include +@@ -121,6 +123,8 @@ static struct pbuf *init_mbuf_to_pbuf(struct rte_mbuf *mbuf, pbuf_layer layer, u + pbuf->allow_in = 1; + pbuf->head = 0; + pbuf->last = pbuf; ++ pbuf->addr.addr = 0; ++ pbuf->port = 0; + pthread_spin_init(&pbuf->pbuf_lock, PTHREAD_PROCESS_SHARED); + } + +@@ -449,7 +453,8 @@ static inline ssize_t app_direct_attach(struct protocol_stack *stack, struct pbu + return send_len; + } + +-static inline ssize_t app_buff_write(struct lwip_sock *sock, void *buf, size_t len, uint32_t write_num) ++static inline ssize_t app_buff_write(struct lwip_sock *sock, void *buf, size_t len, uint32_t write_num, ++ const struct sockaddr *addr, socklen_t addrlen) + { + struct pbuf *pbufs[SOCK_SEND_RING_SIZE_MAX]; + +@@ -457,6 +462,14 @@ static inline ssize_t app_buff_write(struct lwip_sock *sock, void *buf, size_t l + + ssize_t send_len = do_app_write(pbufs, buf, len, write_num); + ++ if (addr) { ++ struct sockaddr_in *saddr = (struct sockaddr_in *)addr; ++ for (int i = 0; i < write_num; i++) { ++ pbufs[i]->addr.addr = saddr->sin_addr.s_addr; ++ pbufs[i]->port = lwip_ntohs((saddr)->sin_port); ++ } ++ } ++ + gazelle_ring_read_over(sock->send_ring); + + sock->remain_len = MBUF_MAX_DATA_LEN - pbufs[write_num - 1]->len; +@@ -536,7 +549,8 @@ int sem_timedwait_nsecs(sem_t *sem) + return sem_timedwait(sem, &ts); + } + +-ssize_t write_stack_data(struct lwip_sock *sock, const void *buf, size_t len) ++ssize_t write_stack_data(struct lwip_sock *sock, const void *buf, size_t len, ++ const struct sockaddr *addr, socklen_t addrlen) + { + if (sock->errevent > 0) { + GAZELLE_RETURN(ENOTCONN); +@@ -581,6 +595,11 @@ ssize_t write_stack_data(struct lwip_sock *sock, const void *buf, size_t len) + if (wakeup) { + wakeup->stat.app_write_cnt += write_num; + } ++ if (addr) { ++ struct sockaddr_in *saddr = (struct sockaddr_in *)addr; ++ last_pbuf->addr.addr = saddr->sin_addr.s_addr; ++ last_pbuf->port = lwip_ntohs((saddr)->sin_port); ++ } + } else { + (void)rpc_call_replenish(stack, sock); + if (wakeup) { +@@ -594,14 +613,14 @@ ssize_t write_stack_data(struct lwip_sock *sock, const void *buf, size_t len) + /* send_ring have idle */ + if (get_global_cfg_params()->expand_send_ring) { + send_len += (write_num <= write_avail) ? +- app_buff_write(sock, (char *)buf + send_len, len - send_len, write_num) : ++ app_buff_write(sock, (char *)buf + send_len, len - send_len, write_num, addr, addrlen) : + app_direct_write(stack, sock, (char *)buf + send_len, len - send_len, write_num); + } else { + if (write_num > write_avail) { + write_num = write_avail; + len = write_num * MBUF_MAX_DATA_LEN; + } +- send_len += app_buff_write(sock, (char *)buf + send_len, len - send_len, write_num); ++ send_len += app_buff_write(sock, (char *)buf + send_len, len - send_len, write_num, addr, addrlen); + } + + if (wakeup) { +@@ -641,10 +660,15 @@ void rpc_replenish(struct rpc_msg *msg) + msg->result = replenish_send_ring(stack, sock); + } + +-static inline bool do_lwip_send(struct protocol_stack *stack, int32_t fd, struct lwip_sock *sock, int32_t flags) ++static inline bool do_lwip_send(struct protocol_stack *stack, int32_t fd, struct lwip_sock *sock, ++ size_t len, int32_t flags) + { + /* send all send_ring, so len set lwip send max. */ +- (void)lwip_send(fd, sock, UINT16_MAX, flags); ++ if (NETCONN_IS_UDP(sock)) { ++ (void)lwip_send(fd, sock, len, flags); ++ } else { ++ (void)lwip_send(fd, sock, UINT16_MAX, flags); ++ } + + return replenish_send_ring(stack, sock); + } +@@ -652,6 +676,7 @@ static inline bool do_lwip_send(struct protocol_stack *stack, int32_t fd, struct + void stack_send(struct rpc_msg *msg) + { + int32_t fd = msg->args[MSG_ARG_0].i; ++ size_t len = msg->args[MSG_ARG_1].size; + struct protocol_stack *stack = (struct protocol_stack *)msg->args[MSG_ARG_3].p; + bool replenish_again; + +@@ -663,7 +688,7 @@ void stack_send(struct rpc_msg *msg) + return; + } + +- replenish_again = do_lwip_send(stack, sock->conn->socket, sock, 0); ++ replenish_again = do_lwip_send(stack, sock->conn->socket, sock, len, 0); + __sync_fetch_and_sub(&sock->call_num, 1); + if (!NETCONN_IS_DATAOUT(sock) && !replenish_again) { + rpc_msg_free(msg); +@@ -712,11 +737,21 @@ ssize_t read_lwip_data(struct lwip_sock *sock, int32_t flags, u8_t apiflags) + uint32_t data_count = rte_ring_count(sock->conn->recvmbox->ring); + uint32_t read_num = LWIP_MIN(free_count, data_count); + struct pbuf *pbufs[SOCK_RECV_RING_SIZE]; ++ struct netbuf *netbufs[SOCK_RECV_RING_SIZE]; + uint32_t read_count = 0; + ssize_t recv_len = 0; + + for (uint32_t i = 0; i < read_num; i++) { +- err_t err = netconn_recv_tcp_pbuf_flags(sock->conn, &pbufs[i], apiflags); ++ ++ err_t err = ERR_OK; ++ if (NETCONN_IS_UDP(sock)) { ++ err = netconn_recv_udp_raw_netbuf_flags(sock->conn, &netbufs[i], apiflags); ++ pbufs[i] = netbufs[i]->p; ++ pbufs[i]->addr = netbufs[i]->addr; ++ pbufs[i]->port = netbufs[i]->port; ++ } else { ++ err = netconn_recv_tcp_pbuf_flags(sock->conn, &pbufs[i], apiflags); ++ } + if (err != ERR_OK) { + if (recv_len > 0) { + /* already received data, return that (this trusts in getting the same error from +@@ -735,10 +770,19 @@ ssize_t read_lwip_data(struct lwip_sock *sock, int32_t flags, u8_t apiflags) + } + + uint32_t enqueue_num = gazelle_ring_sp_enqueue(sock->recv_ring, (void **)pbufs, read_count); ++ if (NETCONN_IS_UDP(sock)) { ++ for (uint32_t i = 0; i < read_count; i++) { ++ memp_free(MEMP_NETBUF, netbufs[i]); ++ } ++ } + for (uint32_t i = enqueue_num; i < read_count; i++) { +- /* update receive window */ +- tcp_recved(sock->conn->pcb.tcp, pbufs[i]->tot_len); +- pbuf_free(pbufs[i]); ++ if (NETCONN_IS_UDP(sock)) { ++ netbuf_delete(netbufs[i]); ++ } else { ++ /* update receive window */ ++ tcp_recved(sock->conn->pcb.tcp, pbufs[i]->tot_len); ++ pbuf_free(pbufs[i]); ++ } + sock->stack->stats.read_lwip_drop++; + } + +@@ -786,7 +830,8 @@ ssize_t recvmsg_from_stack(int32_t s, struct msghdr *message, int32_t flags) + continue; + } + +- ssize_t recvd_local = read_stack_data(s, message->msg_iov[i].iov_base, message->msg_iov[i].iov_len, flags); ++ ssize_t recvd_local = read_stack_data(s, message->msg_iov[i].iov_base, message->msg_iov[i].iov_len, ++ flags, NULL, NULL); + if (recvd_local > 0) { + buflen += recvd_local; + } +@@ -915,7 +960,8 @@ static inline void thread_bind_stack(struct lwip_sock *sock) + } + } + +-ssize_t gazelle_send(int32_t fd, const void *buf, size_t len, int32_t flags) ++ssize_t gazelle_send(int32_t fd, const void *buf, size_t len, int32_t flags, ++ const struct sockaddr *addr, socklen_t addrlen) + { + if (buf == NULL) { + GAZELLE_RETURN(EINVAL); +@@ -932,7 +978,7 @@ ssize_t gazelle_send(int32_t fd, const void *buf, size_t len, int32_t flags) + if (sock->same_node_tx_ring != NULL) { + return gazelle_same_node_ring_send(sock, buf, len, flags); + } +- ssize_t send = write_stack_data(sock, buf, len); ++ ssize_t send = write_stack_data(sock, buf, len, addr, addrlen); + if (send <= 0) { + return send; + } +@@ -956,7 +1002,7 @@ ssize_t sendmsg_to_stack(struct lwip_sock *sock, int32_t s, const struct msghdr + continue; + } + +- ret = write_stack_data(sock, message->msg_iov[i].iov_base, message->msg_iov[i].iov_len); ++ ret = write_stack_data(sock, message->msg_iov[i].iov_base, message->msg_iov[i].iov_len, NULL, 0); + if (ret <= 0) { + buflen = (buflen == 0) ? ret : buflen; + break; +@@ -995,7 +1041,7 @@ static struct pbuf *pbuf_free_partial(struct pbuf *pbuf, uint16_t free_len) + return pbuf; + } + +-ssize_t read_stack_data(int32_t fd, void *buf, size_t len, int32_t flags) ++ssize_t read_stack_data(int32_t fd, void *buf, size_t len, int32_t flags, struct sockaddr *addr, socklen_t *addrlen) + { + size_t recv_left = len; + struct pbuf *pbuf = NULL; +@@ -1052,6 +1098,10 @@ ssize_t read_stack_data(int32_t fd, void *buf, size_t len, int32_t flags) + del_data_in_event(sock); + } + ++ if (addr && addrlen) { ++ lwip_sock_make_addr(sock->conn, &(pbuf->addr), pbuf->port, addr, addrlen); ++ } ++ + if (recvd == 0) { + if (sock->wakeup) { + sock->wakeup->stat.read_null++; +@@ -1107,7 +1157,12 @@ void read_recv_list(struct protocol_stack *stack, uint32_t max_num) + continue; + } + +- ssize_t len = lwip_recv(sock->conn->socket, NULL, 0, 0); ++ ssize_t len = 0; ++ if (NETCONN_IS_UDP(sock)) { ++ len = lwip_recv(sock->conn->socket, NULL, SSIZE_MAX, 0); ++ } else { ++ len = lwip_recv(sock->conn->socket, NULL, 0, 0); ++ } + if (len == 0) { + sock->errevent = 1; + add_sock_event(sock, EPOLLERR); +@@ -1190,11 +1245,6 @@ static inline void clone_lwip_socket_opt(struct lwip_sock *dst_sock, struct lwip + + int32_t gazelle_socket(int domain, int type, int protocol) + { +- if (((type & SOCK_TYPE_MASK) & ~SOCK_STREAM) != 0) { +- LSTACK_LOG(ERR, LSTACK, "sock type error:%d, only support SOCK_STREAM \n", type); +- return -1; +- } +- + int32_t fd = lwip_socket(AF_INET, type, 0); + if (fd < 0) { + return fd; +diff --git a/src/lstack/include/lstack_lwip.h b/src/lstack/include/lstack_lwip.h +index 0b29e71..223ff93 100644 +--- a/src/lstack/include/lstack_lwip.h ++++ b/src/lstack/include/lstack_lwip.h +@@ -17,6 +17,7 @@ + #define NETCONN_IS_DATAIN(sock) ((gazelle_ring_readable_count((sock)->recv_ring) || (sock)->recv_lastdata) || (sock->same_node_rx_ring != NULL && same_node_ring_count(sock))) + #define NETCONN_IS_DATAOUT(sock) (gazelle_ring_readover_count((sock)->send_ring) || (sock)->send_lastdata || (sock)->send_pre_del) + #define NETCONN_IS_OUTIDLE(sock) gazelle_ring_readable_count((sock)->send_ring) ++#define NETCONN_IS_UDP(sock) (NETCONNTYPE_GROUP(netconn_type((sock)->conn)) == NETCONN_UDP) + + struct lwip_sock; + struct rte_mempool; +@@ -29,8 +30,9 @@ int32_t gazelle_socket(int domain, int type, int protocol); + void gazelle_clean_sock(int32_t fd); + struct pbuf *write_lwip_data(struct lwip_sock *sock, uint16_t remain_size, uint8_t *apiflags); + void write_lwip_over(struct lwip_sock *sock); +-ssize_t write_stack_data(struct lwip_sock *sock, const void *buf, size_t len); +-ssize_t read_stack_data(int32_t fd, void *buf, size_t len, int32_t flags); ++ssize_t write_stack_data(struct lwip_sock *sock, const void *buf, size_t len, ++ const struct sockaddr *addr, socklen_t addrlen); ++ssize_t read_stack_data(int32_t fd, void *buf, size_t len, int32_t flags, struct sockaddr *addr, socklen_t *addrlen); + ssize_t read_lwip_data(struct lwip_sock *sock, int32_t flags, uint8_t apiflags); + void read_recv_list(struct protocol_stack *stack, uint32_t max_num); + void read_same_node_recv_list(struct protocol_stack *stack); +@@ -45,7 +47,8 @@ int32_t gazelle_alloc_pktmbuf(struct rte_mempool *pool, struct rte_mbuf **mbufs, + void gazelle_free_pbuf(struct pbuf *pbuf); + ssize_t sendmsg_to_stack(struct lwip_sock *sock, int32_t s, const struct msghdr *message, int32_t flags); + ssize_t recvmsg_from_stack(int32_t s, struct msghdr *message, int32_t flags); +-ssize_t gazelle_send(int32_t fd, const void *buf, size_t len, int32_t flags); ++ssize_t gazelle_send(int32_t fd, const void *buf, size_t len, int32_t flags, ++ const struct sockaddr *addr, socklen_t addrlen); + void rpc_replenish(struct rpc_msg *msg); + void stack_mempool_size(struct rpc_msg *msg); + +diff --git a/src/lstack/lstack.Makefile b/src/lstack/lstack.Makefile +index 7da439d..768c5ba 100644 +--- a/src/lstack/lstack.Makefile ++++ b/src/lstack/lstack.Makefile +@@ -45,7 +45,9 @@ WRAP_API := epoll_ctl \ + readv \ + writev \ + poll \ +- ppoll ++ ppoll \ ++ sendto \ ++ recvfrom + + WRAP_LDFLAGS = $(patsubst %, $(WRAP_PREFIX)%, $(WRAP_API)) + +diff --git a/src/lstack/netif/lstack_ethdev.c b/src/lstack/netif/lstack_ethdev.c +index 2bae2f1..e4e7ebd 100644 +--- a/src/lstack/netif/lstack_ethdev.c ++++ b/src/lstack/netif/lstack_ethdev.c +@@ -842,6 +842,10 @@ static err_t eth_dev_output(struct netif *netif, struct pbuf *pbuf) + } + } + ++ if (first_pbuf->l4_len == 8) { ++ mbuf->data_off += 12; ++ } ++ + if (likely(first_mbuf->pkt_len > MBUF_MAX_LEN)) { + mbuf->ol_flags |= RTE_MBUF_F_TX_TCP_SEG; + mbuf->tso_segsz = MBUF_MAX_DATA_LEN; +@@ -873,7 +877,7 @@ static err_t eth_dev_init(struct netif *netif) + + netif->name[0] = 'e'; + netif->name[1] = 't'; +- netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP; ++ netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP; + netif->mtu = FRAME_MTU; + netif->output = etharp_output; + netif->linkoutput = eth_dev_output; +diff --git a/src/ltran/ltran_stat.c b/src/ltran/ltran_stat.c +index 50f65e3..c17a5c1 100644 +--- a/src/ltran/ltran_stat.c ++++ b/src/ltran/ltran_stat.c +@@ -16,7 +16,6 @@ + #include + #include + #include +-#include + + #include "ltran_tcp_sock.h" + #include "ltran_tcp_conn.h" +@@ -30,6 +29,13 @@ + #include "dpdk_common.h" + #include "ltran_forward.h" + ++/* undefine lwip_ntohs in lwip/def.h */ ++#ifdef ntohs ++#undef ntohs ++#endif ++#include ++ ++ + static uint64_t g_start_time_stamp = 0; + static int32_t g_start_latency = GAZELLE_OFF; + volatile int32_t g_ltran_stop_flag = GAZELLE_FALSE; +-- +2.33.0 + diff --git a/gazelle.spec b/gazelle.spec index 12b01ce..e3f939e 100644 --- a/gazelle.spec +++ b/gazelle.spec @@ -2,7 +2,7 @@ Name: gazelle Version: 1.0.1 -Release: 56 +Release: 57 Summary: gazelle is a high performance user-mode stack License: MulanPSL-2.0 URL: https://gitee.com/openeuler/gazelle @@ -243,6 +243,17 @@ Patch9225: 0225-add-socket-check-before-write-it.patch Patch9226: 0226-update-lstack.Makefile.patch Patch9227: 0227-fix-config-flow-rule-race.patch Patch9228: 0228-fix-build-err-with-dpdk-21.11.patch +Patch9229: 0229-fix-gazellectl-x-error-when-multiplt-user-nic-config.patch +Patch9230: 0230-fix-client-connect-number-unbalance-on-lstack.patch +Patch9231: 0231-kni-down-not-stop-nic.patch +Patch9232: 0232-fix-rpc-msg-alloc-failed.patch +Patch9233: 0233-add-parentheses-to-fix-build-error.patch +Patch9234: 0234-fix-bond_ports-parse-error.patch +Patch9235: 0235-dfx-set-g_unix_fd-to-1-after-abnormal-close-fd-to-av.patch +Patch9236: 0236-dfx-security-function-failed-return-error-directly.patch +Patch9237: 0237-add-bond4-suport.patch +Patch9238: 0238-clean-code.patch +Patch9239: 0239-add-udp-multicast-support-in-gazelle.patch %description %{name} is a high performance user-mode stack. @@ -283,6 +294,19 @@ install -Dpm 0640 %{_builddir}/%{name}-%{version}/src/ltran/ltran.conf %{b %config(noreplace) %{conf_path}/ltran.conf %changelog +* Tue May 16 2023 kircher - 1.0.1-57 +- add udp multicast support in gazelle +- clean code +- add bond4 suport +- dfx: security function failed, return error directly +- dfx: set g_unix_fd to -1 after abnormal close fd to avoid double close +- fix bond_ports parse error fix socket_mem parse error when the value exceeds 65536 exit if create_rxtx_pktmbuf failed build.sh build failed return 1 clean code +- add parentheses to fix build error +- fix rpc msg alloc failed fix process_numa args error coredump fix sock->conn not free when fd is kernel mode +- kni down not stop nic +- fix client connect number unbalance on lstack +- fix gazellectl -x error when multiplt user nic config + * Sat May 13 2023 jiangheng12 - 1.0.1-56 - fix build err with dpdk-21.11 - fix config flow rule race