From 819f74f014592b8af93eb45fd13681caa2cb662a Mon Sep 17 00:00:00 2001
From: zhangmengxuan <zhangmengxuan@kylinos.cn>
Date: Thu, 28 Mar 2024 21:24:05 +0800
Subject: [PATCH] dfx: support get nic bond status

---
 src/common/gazelle_dfx_msg.h  |  9 +++++++++
 src/lstack/core/lstack_dpdk.c | 27 +++++++++++++++++++++++++++
 src/ltran/ltran_dfx.c         | 14 ++++++++++++++
 3 files changed, 50 insertions(+)

diff --git a/src/common/gazelle_dfx_msg.h b/src/common/gazelle_dfx_msg.h
index d7ba80f..4929ae7 100644
--- a/src/common/gazelle_dfx_msg.h
+++ b/src/common/gazelle_dfx_msg.h
@@ -256,15 +256,24 @@ struct gazelle_stat_low_power_info {
 
 #define RTE_ETH_XSTATS_NAME_SIZE 64
 #define RTE_ETH_XSTATS_MAX_LEN 256
+#define RTE_MAX_ETHPORTS 32
 struct nic_eth_xstats_name {
     char name[RTE_ETH_XSTATS_NAME_SIZE];
 };
 
+struct bonding {
+    int8_t mode;
+    int32_t miimon;
+    uint16_t primary_port_id;
+    uint16_t slaves[RTE_MAX_ETHPORTS];
+};
+
 struct nic_eth_xstats {
     struct nic_eth_xstats_name xstats_name[RTE_ETH_XSTATS_MAX_LEN];
     uint64_t values[RTE_ETH_XSTATS_MAX_LEN];
     uint32_t len;
     uint16_t port_id;
+    struct bonding bonding;
 };
 
 struct nic_eth_features {
diff --git a/src/lstack/core/lstack_dpdk.c b/src/lstack/core/lstack_dpdk.c
index a774d45..cd87026 100644
--- a/src/lstack/core/lstack_dpdk.c
+++ b/src/lstack/core/lstack_dpdk.c
@@ -870,6 +870,31 @@ static int dpdk_nic_xstats_name_get(struct nic_eth_xstats_name *names, uint16_t
     return len;
 }
 
+void dpdk_nic_bond_xstats_get(struct gazelle_stack_dfx_data *dfx, uint16_t port_id, uint16_t *slaves, int count)
+{
+    dfx->data.nic_xstats.bonding.mode = rte_eth_bond_mode_get(port_id);
+    if (dfx->data.nic_xstats.bonding.mode < 0) {
+        LSTACK_LOG(ERR, LSTACK, "rte_eth_bond_mode_get failed.\n");
+        return;
+    }
+
+    dfx->data.nic_xstats.bonding.primary_port_id = rte_eth_bond_primary_get(port_id);
+    if (dfx->data.nic_xstats.bonding.primary_port_id < 0) {
+        LSTACK_LOG(ERR, LSTACK, "rte_eth_bond_primary_get failed.\n");
+        return;
+    }
+
+    dfx->data.nic_xstats.bonding.miimon = rte_eth_bond_link_monitoring_get(port_id);
+    if (dfx->data.nic_xstats.bonding.miimon <= 0) {
+        LSTACK_LOG(ERR, LSTACK, "rte_eth_bond_link_monitoring_get failed.\n");
+        return;
+    }
+
+    for (int i = 0; i < count; i++) {
+        dfx->data.nic_xstats.bonding.slaves[i] = slaves[i];
+    }
+}
+
 void dpdk_nic_xstats_get(struct gazelle_stack_dfx_data *dfx, uint16_t port_id)
 {
     struct rte_eth_dev_info dev_info;
@@ -878,6 +903,7 @@ void dpdk_nic_xstats_get(struct gazelle_stack_dfx_data *dfx, uint16_t port_id)
 
     dfx->data.nic_xstats.len = -1;
     dfx->data.nic_xstats.port_id = port_id;
+    dfx->data.nic_xstats.bonding.mode = -1;
     ret = rte_eth_dev_info_get(port_id, &dev_info);
     if (ret < 0) {
         LSTACK_LOG(ERR, LSTACK, "rte_eth_dev_info_get failed.\n");
@@ -904,6 +930,7 @@ void dpdk_nic_xstats_get(struct gazelle_stack_dfx_data *dfx, uint16_t port_id)
         if (dpdk_nic_xstats_value_get(dfx->data.nic_xstats.values, len, slaves, slave_count) != 0) {
             return;
         }
+        dpdk_nic_bond_xstats_get(dfx, port_id, slaves, slave_count);
     } else {
         len = dpdk_nic_xstats_name_get(dfx->data.nic_xstats.xstats_name, port_id);
         if (len <= 0) {
diff --git a/src/ltran/ltran_dfx.c b/src/ltran/ltran_dfx.c
index 073dfa7..88d11bd 100644
--- a/src/ltran/ltran_dfx.c
+++ b/src/ltran/ltran_dfx.c
@@ -200,6 +200,20 @@ 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 = "########################";
 
+    if (xstats->bonding.mode >= 0) {
+        printf("############# NIC bonding mode display #############\n");
+        printf("%s############################\n", nic_stats_border);
+        printf("Bonding mode： [%d]\n", xstats->bonding.mode);
+        printf("Bonding miimon: [%d]\n", xstats->bonding.miimon);
+        printf("Slaves(%d): [", xstats->port_id);
+        for (int i = 0; i < xstats->port_id - 1; i++) {
+            printf("%d ", xstats->bonding.slaves[i]);
+        }
+        printf("%d]\n", xstats->bonding.slaves[xstats->port_id - 1]);
+        printf("Primary: [%d]\n", xstats->bonding.primary_port_id);
+        printf("%s############################\n", nic_stats_border);
+    }
+
     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) {
-- 
2.33.0

