From c3cc06b64d2da294001289d7610f186a6af7a8c4 Mon Sep 17 00:00:00 2001
From: zhengjiebing <zhengjiebing_yewu@cmss.chinamobile.com>
Date: Mon, 30 Oct 2023 21:52:29 +0800
Subject: [PATCH] bond6

---
 src/lstack/core/lstack_cfg.c    | 34 +++++++++++++++++++++------------
 src/lstack/core/lstack_dpdk.c   | 33 +++++++++++++++++++-------------
 src/lstack/include/lstack_cfg.h |  6 +++---
 3 files changed, 45 insertions(+), 28 deletions(-)

diff --git a/src/lstack/core/lstack_cfg.c b/src/lstack/core/lstack_cfg.c
index ad89bca..c4278b5 100644
--- a/src/lstack/core/lstack_cfg.c
+++ b/src/lstack/core/lstack_cfg.c
@@ -24,6 +24,7 @@
 #include <unistd.h>
 #include <sched.h>
 
+#include <rte_eth_bond.h>
 #include <lwip/lwipsock.h>
 #include <lwip/posix_api.h>
 #include <lwip/inet.h>
@@ -70,8 +71,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);
+static int32_t parse_bond_mode(void);
+static int32_t parse_bond_slave_mac(void);
 static int32_t parse_use_sockmap(void);
 static int32_t parse_udp_enable(void);
 static int32_t parse_nic_rxqueue_size(void);
@@ -130,8 +131,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 },
+    { "bond_mode", parse_bond_mode },
+    { "bond_slave_mac", parse_bond_slave_mac },
     { "use_sockmap", parse_use_sockmap },
     { "udp_enable", parse_udp_enable },
     { "nic_rxqueue_size", parse_nic_rxqueue_size},
@@ -1114,16 +1115,25 @@ static int32_t parse_udp_enable(void)
     return ret;
 }
 
-static int32_t parse_use_bond4(void)
+static int32_t parse_bond_mode(void)
 {
-    int32_t ret;
-    PARSE_ARG(g_config_params.use_bond4, "use_bond4", 0, 0, 1, ret);
-    return ret;
+    const config_setting_t *bond_mode = NULL;
+    bond_mode = config_lookup(&g_config, "bond_mode");
+    g_config_params.bond_mode = config_setting_get_int(bond_mode);
+    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;
+    }
+    return 0;
 }
 
-static int32_t parse_bond4_slave_mac(void)
+static int32_t parse_bond_slave_mac(void)
 {
-    if (g_config_params.use_bond4 == 0) {
+    if (g_config_params.bond_mode == -1) {
         return 0;
     }
 
@@ -1151,13 +1161,13 @@ static int32_t parse_bond4_slave_mac(void)
     }
 
     /* add dev */
-    ret = str_to_eth_addr(slave_mac1, g_config_params.bond4_slave1_mac_addr);
+    ret = str_to_eth_addr(slave_mac1, g_config_params.bond_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);
+    ret = str_to_eth_addr(slave_mac2, g_config_params.bond_slave2_mac_addr);
     if (ret != 0) {
         LSTACK_PRE_LOG(LSTACK_ERR, "cfg: invalid device name %s ret=%d.\n", slave_mac2, ret);
     }
diff --git a/src/lstack/core/lstack_dpdk.c b/src/lstack/core/lstack_dpdk.c
index 48fa67d..1a5b568 100644
--- a/src/lstack/core/lstack_dpdk.c
+++ b/src/lstack/core/lstack_dpdk.c
@@ -463,7 +463,6 @@ static void rss_setup(const int port_id, const uint16_t nb_queues)
 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;
     }
@@ -474,7 +473,7 @@ int32_t dpdk_ethdev_init(int port_id, bool bond_port)
 
     struct protocol_stack_group *stack_group = get_protocol_stack_group();
 
-    if (!use_bond4) {
+    if (get_global_cfg_params()->bond_mode < 0) {
         port_id = ethdev_port_id(get_global_cfg_params()->mac_addr);
         if (port_id < 0) {
             return port_id;
@@ -499,13 +498,13 @@ int32_t dpdk_ethdev_init(int port_id, bool bond_port)
     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[0] = ethdev_port_id(get_global_cfg_params()->bond_slave1_mac_addr);
 	if (slave_port_id[0] < 0) {
 	    LSTACK_LOG(ERR, LSTACK, "get slave port id failed port = %d\n", slave_port_id[0]);
             return slave_port_id[0];
         }
 
-        slave_port_id[1] = ethdev_port_id(get_global_cfg_params()->bond4_slave2_mac_addr);
+        slave_port_id[1] = ethdev_port_id(get_global_cfg_params()->bond_slave2_mac_addr);
 	if (slave_port_id[1] < 0) {
 	    LSTACK_LOG(ERR, LSTACK, "get slave port id failed port = %d\n", slave_port_id[1]);
             return slave_port_id[1];
@@ -648,7 +647,7 @@ int32_t dpdk_ethdev_start(void)
         }
     }
 
-    if (get_global_cfg_params()->use_bond4) {
+    if (get_global_cfg_params()->bond_mode >= 0) {
         return 0;
     }
 
@@ -697,15 +696,16 @@ int32_t init_dpdk_ethdev(void)
 {
     int32_t ret;
 
-    if (get_global_cfg_params()->use_bond4) {
-        int bond_port_id = rte_eth_bond_create("net_bonding0", 4, (uint8_t)rte_socket_id());
+    if (get_global_cfg_params()->bond_mode >= 0) {
+        uint8_t socket_id = rte_socket_id();
+        int bond_port_id = rte_eth_bond_create("net_bonding0", get_global_cfg_params()->bond_mode, 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);
-	if (ret != 0) {
+        if (ret != 0) {
             LSTACK_LOG(ERR, LSTACK, "dpdk_ethdev_init failed ret = %d\n", ret);
             return -1;
         }
@@ -716,10 +716,17 @@ int32_t init_dpdk_ethdev(void)
             return -1;
         }
 
-        ret = rte_eth_bond_8023ad_dedicated_queues_enable(bond_port_id);
-        if (ret < 0) {
-	    LSTACK_LOG(ERR, LSTACK, "dpdk enable 8023 dedicated queues failed ret = %d\n", ret);
-            return -1;
+	if (get_global_cfg_params()->bond_mode == BONDING_MODE_8023AD) {
+            ret = rte_eth_bond_8023ad_dedicated_queues_enable(bond_port_id);
+            if (ret < 0) {
+                LSTACK_LOG(ERR, LSTACK, "dpdk enable 8023 dedicated queues failed ret = %d\n", ret);
+                return -1;
+            }
+        } else {
+            ret = rte_eth_bond_mode_set(bond_port_id, get_global_cfg_params()->bond_mode);
+            if (ret < 0) {
+                LSTACK_LOG(ERR, LSTACK, "dpdk enable mode set failed ret = %d\n", ret);
+            }
         }
 
         ret = rte_eth_promiscuous_enable(bond_port_id);
@@ -735,7 +742,7 @@ int32_t init_dpdk_ethdev(void)
         }
 
         ret = rte_eth_dev_start(bond_port_id);
-	if (ret < 0) {
+        if (ret < 0) {
             LSTACK_LOG(ERR, LSTACK, "dpdk start bond port failed ret = %d\n", ret);
             return -1;
         }
diff --git a/src/lstack/include/lstack_cfg.h b/src/lstack/include/lstack_cfg.h
index 1d895ec..fc627e3 100644
--- a/src/lstack/include/lstack_cfg.h
+++ b/src/lstack/include/lstack_cfg.h
@@ -110,9 +110,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];
+    int8_t bond_mode;
+    uint8_t bond_slave1_mac_addr[ETHER_ADDR_LEN];
+    uint8_t bond_slave2_mac_addr[ETHER_ADDR_LEN];
     bool use_sockmap;
     bool udp_enable;
     struct cfg_nic_params nic;
-- 
2.27.0

