From cfe5641d29e425c9da54b270b7945d17ab5d594b Mon Sep 17 00:00:00 2001
From: zhangmengxuan <zhangmengxuan@kylinos.cn>
Date: Mon, 22 Jan 2024 13:52:50 +0800
Subject: [PATCH] add bond1 support

---
 src/lstack/core/lstack_cfg.c    | 28 ++++++++++++++++++++++++----
 src/lstack/core/lstack_dpdk.c   | 24 ++++++++++++++++++++++++
 src/lstack/include/lstack_cfg.h |  1 +
 3 files changed, 49 insertions(+), 4 deletions(-)

diff --git a/src/lstack/core/lstack_cfg.c b/src/lstack/core/lstack_cfg.c
index 7e4482f..b533a33 100644
--- a/src/lstack/core/lstack_cfg.c
+++ b/src/lstack/core/lstack_cfg.c
@@ -40,6 +40,8 @@
 #define NUMA_CPULIST_PATH "/sys/devices/system/node/node%u/cpulist"
 #define DEV_MAC_LEN 17
 #define CPUS_MAX_NUM 256
+#define BOND_MIIMON_MIN 1
+#define BOND_MIIMON_MAX INT_MAX
 
 static struct cfg_params g_config_params;
 
@@ -72,6 +74,7 @@ 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_bond_mode(void);
+static int32_t parse_bond_miimon(void);
 static int32_t parse_bond_slave_mac(void);
 static int32_t parse_use_sockmap(void);
 static int32_t parse_udp_enable(void);
@@ -134,6 +137,7 @@ static struct config_vector_t g_config_tbl[] = {
     { "process_idx", parse_process_index },
     { "tuple_filter", parse_tuple_filter },
     { "bond_mode", parse_bond_mode },
+    { "bond_miimon", parse_bond_miimon},
     { "bond_slave_mac", parse_bond_slave_mac },
     { "use_sockmap", parse_use_sockmap },
     { "udp_enable", parse_udp_enable },
@@ -1181,14 +1185,30 @@ static int32_t parse_bond_mode(void)
     if (g_config_params.bond_mode == -1) {
         return 0;
     }
-    if (g_config_params.bond_mode != BONDING_MODE_8023AD && g_config_params.bond_mode != BONDING_MODE_ALB) {
-        LSTACK_PRE_LOG(LSTACK_ERR, "cfg: invalid bond mode = %d. only supports bond mode = 4,6.\n",
-            g_config_params.bond_mode);
-        return -EINVAL;
+
+    switch (g_config_params.bond_mode) {
+        case BONDING_MODE_ACTIVE_BACKUP:
+        case BONDING_MODE_8023AD:
+        case BONDING_MODE_ALB:
+            break;
+        default:
+            LSTACK_PRE_LOG(LSTACK_ERR, "cfg: invalid bond mode = %d. only supports bond mode = 1,4,6.\n",
+                g_config_params.bond_mode);
+            return -EINVAL; // Invalid bond mode
     }
     return 0;
 }
 
+static int32_t parse_bond_miimon(void)
+{
+    int32_t ret;
+    if (g_config_params.bond_mode == -1) {
+        return 0;
+    }
+    PARSE_ARG(g_config_params.bond_miimon, "bond_miimon", 10, BOND_MIIMON_MIN, BOND_MIIMON_MAX, ret);
+    return ret;
+}
+
 static int32_t parse_bond_slave_mac(void)
 {
     if (g_config_params.bond_mode == -1) {
diff --git a/src/lstack/core/lstack_dpdk.c b/src/lstack/core/lstack_dpdk.c
index 7f1ceb8..e352850 100644
--- a/src/lstack/core/lstack_dpdk.c
+++ b/src/lstack/core/lstack_dpdk.c
@@ -462,6 +462,20 @@ static void rss_setup(const int port_id, const uint16_t nb_queues)
     free(reta_conf);
 }
 
+int32_t dpdk_bond_primary_set(int port_id, int slave_port_id)
+{
+    int32_t primary_port_id = ethdev_port_id(get_global_cfg_params()->mac_addr);
+    if (slave_port_id == primary_port_id) {
+        int32_t ret = rte_eth_bond_primary_set(port_id, primary_port_id);
+        if (ret != 0) {
+            LSTACK_LOG(ERR, LSTACK, "dpdk set bond primary port failed ret = %d\n", ret);
+            return -1;
+        }
+        return ret;
+    }
+    return 0;
+}
+
 int32_t dpdk_ethdev_init(int port_id, bool bond_port)
 {
     uint16_t nb_queues = get_global_cfg_params()->num_cpu;
@@ -533,6 +547,10 @@ int32_t dpdk_ethdev_init(int port_id, bool bond_port)
                 LSTACK_LOG(ERR, LSTACK, "dpdk add slave port failed ret = %d\n", ret);
                 return -1;
             }
+            
+            if (get_global_cfg_params()->bond_mode == BONDING_MODE_ACTIVE_BACKUP) {
+                dpdk_bond_primary_set(port_id, slave_port_id[i]);
+            }
 
             ret = rte_eth_dev_start(slave_port_id[i]);
             if (ret != 0) {
@@ -736,6 +754,12 @@ int32_t init_dpdk_ethdev(void)
             }
         }
 
+        ret = rte_eth_bond_link_monitoring_set(bond_port_id, get_global_cfg_params()->bond_miimon);
+        if (ret < 0) {
+            LSTACK_LOG(ERR, LSTACK, "dpdk set bond link monitoring failed ret = %d\n", ret);
+            return -1;
+        }
+
         ret = rte_eth_promiscuous_enable(bond_port_id);
         if (ret < 0) {
 	    LSTACK_LOG(ERR, LSTACK, "dpdk enable promiscuous failed ret = %d\n", ret);
diff --git a/src/lstack/include/lstack_cfg.h b/src/lstack/include/lstack_cfg.h
index 01d70fb..a00e47a 100644
--- a/src/lstack/include/lstack_cfg.h
+++ b/src/lstack/include/lstack_cfg.h
@@ -112,6 +112,7 @@ struct cfg_params {
     uint16_t send_ring_size;
     bool tuple_filter;
     int8_t bond_mode;
+    int32_t bond_miimon;
     struct rte_ether_addr bond_slave_mac_addr[GAZELLE_MAX_BOND_NUM];
     bool use_sockmap;
     bool udp_enable;
-- 
2.33.0

