From 5e10c47492fef0eef886fa42651d3acef03f4196 Mon Sep 17 00:00:00 2001
From: zhengjiebing <zhengjiebing_yewu@cmss.chinamobile.com>
Date: Wed, 1 Nov 2023 07:50:16 +0800
Subject: [PATCH] slave mac addr

---
 src/lstack/core/lstack_cfg.c    | 50 ++++++++++++++++-----------------
 src/lstack/core/lstack_dpdk.c   | 47 ++++++++++++++-----------------
 src/lstack/include/lstack_cfg.h |  4 +--
 3 files changed, 48 insertions(+), 53 deletions(-)

diff --git a/src/lstack/core/lstack_cfg.c b/src/lstack/core/lstack_cfg.c
index c4278b5..5ac6aa7 100644
--- a/src/lstack/core/lstack_cfg.c
+++ b/src/lstack/core/lstack_cfg.c
@@ -1137,40 +1137,40 @@ static int32_t parse_bond_slave_mac(void)
         return 0;
     }
 
-    int32_t ret;
-    const char *slave_mac1 = NULL;
-    const char *slave_mac2 = NULL;
+    int32_t ret = 0;
+    const char *bond_slave_mac = 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");
+    devs = config_lookup(&g_config, "bond_slave_mac");
     if (devs == NULL) {
         return -EINVAL;
     }
-    slave_mac2 = config_setting_get_string(devs);
-    if (slave_mac2 == NULL) {
+    bond_slave_mac = config_setting_get_string(devs);
+    if (bond_slave_mac == NULL) {
         return 0;
     }
 
-    /* add dev */
-    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.bond_slave2_mac_addr);
-    if (ret != 0) {
-        LSTACK_PRE_LOG(LSTACK_ERR, "cfg: invalid device name %s ret=%d.\n", slave_mac2, ret);
+    int32_t k = 0;
+    char *bond_slave_mac_tmp = strdup(bond_slave_mac);
+    char *tmp = NULL;
+    const char *delim = ";";
+    
+    char *mac_addr = strtok_s(bond_slave_mac_tmp, delim, &tmp);
+    while (mac_addr != NULL) {
+        if (k >= GAZELLE_MAX_BOND_NUM) {
+            LSTACK_PRE_LOG(LSTACK_ERR, "cfg: too many slave mac address. The maximum number of mac address is %d.\n",
+                GAZELLE_MAX_BOND_NUM);
+            return -EINVAL;
+        }
+        ret = str_to_eth_addr(mac_addr, g_config_params.bond_slave_mac_addr[k].addr_bytes);
+        if (ret != 0) {
+            LSTACK_PRE_LOG(LSTACK_ERR, "cfg: invalid device name %s ret=%d.\n", mac_addr, ret);
+            return ret;
+        }
+        mac_addr = strtok_s(NULL, delim, &tmp);
+        k = k + 1;
     }
+    free(bond_slave_mac_tmp);
     return ret;
 }
 
diff --git a/src/lstack/core/lstack_dpdk.c b/src/lstack/core/lstack_dpdk.c
index 1a5b568..4318096 100644
--- a/src/lstack/core/lstack_dpdk.c
+++ b/src/lstack/core/lstack_dpdk.c
@@ -496,46 +496,41 @@ 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()->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()->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];
-        }
-
-        for (int i = 0; i < slave_num; i++) {
+        int32_t slave_port_id[GAZELLE_MAX_BOND_NUM];
+        for (int i = 0; i < GAZELLE_MAX_BOND_NUM; i++) {
+            if (rte_is_zero_ether_addr(&get_global_cfg_params()->bond_slave_mac_addr[i])) {
+                break;
+            }
+            slave_port_id[i] = ethdev_port_id(get_global_cfg_params()->bond_slave_mac_addr[i].addr_bytes);
+            if (slave_port_id[i] < 0) {
+                LSTACK_LOG(ERR, LSTACK, "get slave port id failed port = %d\n", slave_port_id[1]);
+                return slave_port_id[i];
+            }
             ret = dpdk_ethdev_init(slave_port_id[i], 0);
             if (ret != 0) {
                 LSTACK_LOG(ERR, LSTACK, "dpdk_ethdev_init failed ret = %d\n", ret);
                 return -1;
             }
             ret = rte_eth_promiscuous_enable(slave_port_id[i]);
-	    if (ret != 0) {
+            if (ret != 0) {
                 LSTACK_LOG(ERR, LSTACK, "dpdk slave enable promiscuous failed ret = %d\n", ret);
                 return -1;
             }
 
             ret = rte_eth_allmulticast_enable(slave_port_id[i]);
-	    if (ret != 0) {
+            if (ret != 0) {
                 LSTACK_LOG(ERR, LSTACK, "dpdk slave enable allmulticast failed ret = %d\n", ret);
                 return -1;
             }
 
             ret = rte_eth_bond_slave_add(port_id, slave_port_id[i]);
-	    if (ret != 0) {
+            if (ret != 0) {
                 LSTACK_LOG(ERR, LSTACK, "dpdk add slave port failed ret = %d\n", ret);
                 return -1;
             }
 
             ret = rte_eth_dev_start(slave_port_id[i]);
-	    if (ret != 0) {
+            if (ret != 0) {
                 LSTACK_LOG(ERR, LSTACK, "dpdk start slave port failed ret = %d\n", ret);
                 return -1;
             }
@@ -550,15 +545,15 @@ int32_t dpdk_ethdev_init(int port_id, bool bond_port)
     if (bond_port) {
         struct rte_eth_dev_info slave_dev_info;
         int slave_id = rte_eth_bond_primary_get(port_id);
-	if (slave_id < 0) {
-	    LSTACK_LOG(ERR, LSTACK, "dpdk get bond primary port failed port = %d\n", slave_id);
-	    return slave_id;
-	}
+        if (slave_id < 0) {
+            LSTACK_LOG(ERR, LSTACK, "dpdk get bond primary port failed port = %d\n", slave_id);
+            return slave_id;
+        }
         ret = rte_eth_dev_info_get(slave_id, &slave_dev_info);
-	if (ret != 0) {
-	    LSTACK_LOG(ERR, LSTACK, "dpdk get bond dev info failed ret = %d\n", ret);
+        if (ret != 0) {
+            LSTACK_LOG(ERR, LSTACK, "dpdk get bond dev info failed ret = %d\n", ret);
             return ret;
-	}
+        }
         dev_info.rx_offload_capa = slave_dev_info.rx_offload_capa;
         dev_info.tx_offload_capa = slave_dev_info.tx_offload_capa;
         dev_info.reta_size = slave_dev_info.reta_size;
diff --git a/src/lstack/include/lstack_cfg.h b/src/lstack/include/lstack_cfg.h
index fc627e3..c1a6464 100644
--- a/src/lstack/include/lstack_cfg.h
+++ b/src/lstack/include/lstack_cfg.h
@@ -15,6 +15,7 @@
 #include <stdbool.h>
 
 #include <lwip/ip_addr.h>
+#include <rte_ether.h>
 
 #include "lstack_protocol_stack.h"
 #include "gazelle_opt.h"
@@ -111,8 +112,7 @@ struct cfg_params {
     bool expand_send_ring;
     bool tuple_filter;
     int8_t bond_mode;
-    uint8_t bond_slave1_mac_addr[ETHER_ADDR_LEN];
-    uint8_t bond_slave2_mac_addr[ETHER_ADDR_LEN];
+    struct rte_ether_addr bond_slave_mac_addr[GAZELLE_MAX_BOND_NUM];
     bool use_sockmap;
     bool udp_enable;
     struct cfg_nic_params nic;
-- 
2.27.0

