Skip to content

Commit

Permalink
Merge pull request #9 from liujunjie-cn/main
Browse files Browse the repository at this point in the history
bmsec bridge
  • Loading branch information
zetao-zhang-sophgo authored Dec 4, 2024
2 parents 8f50374 + 35f30c3 commit 317eecb
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 8 deletions.
214 changes: 214 additions & 0 deletions source/pbmsec/deb/opt/sophon/bmsec/commands/_confNetwork.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
#!/bin/bash
###############################################
############[email protected]##############
###############################################

# Some vars come from 1_network.sh
# config netplan of host
CORE_NETPLAN_FILE="$YAML_FILE"
CORE_NETPLAN_FILE_BAK="/etc/netplan/netcfg.yaml.bak"

SUB_NETPLAN_FILE="/etc/netplan/01-netcfg.yaml"
SUB_NETPLAN_FILE_BAK="/etc/netplan/01-netcfg.yaml.bak"
iptable_setup="/root/se_ctrl/iptable_setup.sh"

ret=$(sudo bash -c "source /root/se_ctrl/sectr.sh;sectr_get_auth")
if [ $ret -eq 0 ]; then
echo "开机初始化中,请等待几分钟再执行该命令"
exit 0
fi

if [[ ! -f "$CORE_NETPLAN_FILE" ]]; then
echo "error: file '$CORE_NETPLAN_FILE' does not exist"
exit 1
fi

usage() {
echo "usage: $1 [OPTIONS]"
echo "OPTIONS:"
echo " -h , HELP INFO"
echo " 0 , delete bridge config"
echo " 1 , config bridge mode"
exit 1
}

while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
usage
;;
0)
echo "delete bridges config..."
mode=0
;;
1)
echo "config bridges..."
mode=1
;;
*)
echo "Error: invalid args '$1'" >&2
usage
;;
esac
shift
done

function reset_network()
{
echo "reset network config of all cores"
for ((id = 1; id <= $seNCtrl_ALL_SUB_NUM; id++)); do
file_name=$(basename "$SUB_NETPLAN_FILE_BAK")
ret=$(${seNCtrl_PWD}/bmsec run $id "ls /etc/netplan/" | grep $file_name)
# echo $ret
if [[ -n "$ret" ]]; then
${seNCtrl_PWD}/bmsec run $id "sudo chmod 777 $SUB_NETPLAN_FILE_BAK" &> /dev/null
${seNCtrl_PWD}/bmsec run $id "sudo mv $SUB_NETPLAN_FILE_BAK $SUB_NETPLAN_FILE" &> /dev/null
${seNCtrl_PWD}/bmsec run $id "sudo netplan apply" &> /dev/null
else
echo "Warning: The file $SUB_NETPLAN_FILE_BAK in core $id does not exist. Please reboot core $id"
fi
done

sudo chmod 777 ${seNCtrl_PWD}/configs/sub/subInfo.12
sudo sed -i 's/Bridge_CONFIG_FLAG=1/Bridge_CONFIG_FLAG=0/' ${seNCtrl_PWD}/configs/sub/subInfo.12
sudo sed -i '/^Bridge_IP_HALF=/d' ${seNCtrl_PWD}/configs/sub/subInfo.12

if grep -q "bridges:" "$CORE_NETPLAN_FILE"; then
echo "reset host netplan config..."
if [[ -f "$CORE_NETPLAN_FILE_BAK" ]]; then
sudo chmod 777 $CORE_NETPLAN_FILE_BAK
sudo mv $CORE_NETPLAN_FILE_BAK $CORE_NETPLAN_FILE
sudo netplan apply
else
sudo cat $CORE_NETPLAN_FILE
echo "error: $CORE_NETPLAN_FILE_BAK in host does not exist!!! Please modify your $CORE_NETPLAN_FILE of host by yourself!"
fi
fi
# reset iptable_setup.sh
sudo chmod 777 $iptable_setup
sudo sed -i '/^wanname=br0/d; s/^#wanname=/wanname=/' "$iptable_setup"
if ! [ "$product" = "SE6-CTRL" ] && [ "$product" = "SE6 CTRL" ] && [ "$product" = "SM7 CTRL" ] && [ "$product" = "SE8 CTRL" ]; then
sudo $iptable_setup > /dev/null 2>&1
fi
echo "please reboot!"
}

function conf_bridge()
{
sudo chmod 777 ${seNCtrl_PWD}/configs/sub/subInfo.12
if grep -q "Bridge_IP_HALF=" "${seNCtrl_PWD}/configs/sub/subInfo.12"; then
sudo sed -i "s/^Bridge_IP_HALF=.*/Bridge_IP_HALF=$seNCtrl_SUB_IP_HALF/" "${seNCtrl_PWD}/configs/sub/subInfo.12"
else
sudo echo "Bridge_IP_HALF=$seNCtrl_SUB_IP_HALF" >> "${seNCtrl_PWD}/configs/sub/subInfo.12"
fi
sudo sed -i 's/Bridge_CONFIG_FLAG=0/Bridge_CONFIG_FLAG=1/' ${seNCtrl_PWD}/configs/sub/subInfo.12

# set all cores as DHCP mode while still retain original ip addr for bmsec
for ((id = 1; id <= $seNCtrl_ALL_SUB_NUM; id++)); do
file_name=$(basename "$SUB_NETPLAN_FILE_BAK")
ret=$(${seNCtrl_PWD}/bmsec run $id "ls /etc/netplan/" | grep $file_name)
if [[ -n "$ret" ]]; then
echo "The file $SUB_NETPLAN_FILE_BAK in core $id exists."
else
${seNCtrl_PWD}/bmsec run $id "sudo cp $SUB_NETPLAN_FILE $SUB_NETPLAN_FILE_BAK" &> /dev/null
fi
done

# ${seNCtrl_PWD}/bmsec getbi
echo "set all cores as DHCP MODE!"
${seNCtrl_PWD}/bmsec run all "sudo chmod 777 $SUB_NETPLAN_FILE" #&> /dev/null
${seNCtrl_PWD}/bmsec run all "sudo sed -i 's/dhcp4: no/dhcp4: yes/' $SUB_NETPLAN_FILE" &> /dev/null
${seNCtrl_PWD}/bmsec run all "sudo sed -i '/gateway4:/d' $SUB_NETPLAN_FILE" &> /dev/null
${seNCtrl_PWD}/bmsec run all "sudo netplan apply" &> /dev/null
# ${seNCtrl_PWD}/bmsec run all "sudo cat $SUB_NETPLAN_FILE"

# config host netplan
if ! grep -q "bridges:" "$CORE_NETPLAN_FILE"; then
echo "setting bridges in netplan yaml file..."
if [[ ! -f "$CORE_NETPLAN_FILE_BAK" ]]; then
echo "backup $CORE_NETPLAN_FILE"
sudo cp $CORE_NETPLAN_FILE $CORE_NETPLAN_FILE_BAK
fi
sudo chmod 777 $CORE_NETPLAN_FILE
if [[ "$WAN" == "eno5" ]]; then
#se8-288
BRIDGE_BLOCK=$(cat << EOF
bridges:
br0:
interfaces: [$WAN, $INTERFACE_0, $INTERFACE_1]
dhcp4: yes
addresses: [192.168.150.3/24, $seNCtrl_SUB_IP_0/24, $seNCtrl_SUB_IP_1/24]
nameservers:
addresses: [8.8.8.8]
EOF
)
else
BRIDGE_BLOCK=$(cat << EOF
bridges:
br0:
interfaces: [$WAN, $INTERFACE_0, $INTERFACE_1]
dhcp4: yes
addresses: [192.168.150.3/24, $seNCtrl_SUB_IP_0/24, $seNCtrl_SUB_IP_1/24]
nameservers:
addresses: [8.8.8.8]
EOF
)
fi
TEMP_FILE=$(mktemp /tmp/01-netcfg.yaml.tmp.XXXXXX) || { echo "can not create temp file"; exit 1; }

while IFS= read -r line; do
echo "$line" >> "$TEMP_FILE"
# insert bridges config after renderer
if [[ $line =~ renderer:[[:space:]]* ]]; then
# insert bridges config block
echo "$BRIDGE_BLOCK" >> "$TEMP_FILE"
fi
done < "$CORE_NETPLAN_FILE"
sudo mv "$TEMP_FILE" "$CORE_NETPLAN_FILE" || { echo "can not modify netplan yaml file"; rm -f "$TEMP_FILE"; exit 1; }

# set ip addr of eth0/1 as NULL
sudo sed -i "/$INTERFACE_0:/,/optional: yes/ {
s/addresses: \[[^]]*\]/addresses: []/
}" $CORE_NETPLAN_FILE

sudo sed -i "/$INTERFACE_1:/,/optional: yes/ {
s/addresses: \[[^]]*\]/addresses: []/
}" $CORE_NETPLAN_FILE

echo "config netplan..."
sudo netplan apply
if [ $? -ne 0 ]; then
sudo cat $CORE_NETPLAN_FILE
echo "Error: netplan apply failed. Please check your netplan yaml file!"
exit 1
fi
fi

# update wanname in iptable_setup.sh
# iptable_setup="/root/se_ctrl/iptable_setup.sh"
if ! sudo grep -q '^wanname=br0' "$iptable_setup"; then
sudo chmod 777 $iptable_setup
sudo sed -i '/^wanname=/ s/^/#/' "$iptable_setup"
sudo sed -i '/^#wanname=/a wanname=br0' "$iptable_setup"
echo "Update completed: 'wanname=br0' added."
else
echo "wanname is br0."
fi

PID=$(ps -ef | grep "[b]ash $iptable_setup" | awk '{print $2}')
if [ -n "$PID" ]; then
sudo kill "$PID"
# echo "kill iptable_setup.sh"
fi
sudo iptables -P FORWARD ACCEPT

}


if [ "$mode" -eq 0 ]; then
echo "Mode is set to 0"
reset_network
elif [ "$mode" -eq 1 ]; then
echo "Mode is set to 1"
conf_bridge
fi
52 changes: 47 additions & 5 deletions source/pbmsec/deb/opt/sophon/bmsec/configs/1_network.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,57 @@ seNCtrl_HOST_SUB_ETHS=()
if [ "$product" = "SE6-CTRL" ] || [ "$product" = "SE6 CTRL" ] || [ "$product" = "SM7 CTRL" ] || [ "$product" = "SE8 CTRL" ]; then
seNCtrl_HOST_SUB_ETHS+=('eth0' 'eth1')
seNCtrl_DEBUG_UART=/dev/ttyS2

YAML_FILE="/etc/netplan/01-netcfg.yaml"
INTERFACE_0="eth0"
INTERFACE_1="eth1"
WAN="enp4s0"
else #se8 x86
seNCtrl_HOST_SUB_ETHS+=('eno1' 'enp2s0f0' 'bond0')
seNCtrl_DEBUG_UART=/dev/ttyS1
YAML_FILE="/etc/netplan/01-network-manager-all.yaml"
INTERFACE_0="eno1"
INTERFACE_1="eno3"
WAN="eno5"
fi

source ${seNCtrl_PWD}/configs/sub/subInfo.12
if ifconfig | grep "^br" > /dev/null 2>&1 && [ "$Bridge_CONFIG_FLAG" == "1" ]; then
seNCtrl_SUB_IP_HALF=$Bridge_IP_HALF
# echo $seNCtrl_SUB_IP_HALF

#check if netplan yaml of eth0/1 is null
ETH0_ADDRESSES=$(sudo grep -A 2 "$INTERFACE_0:" "$YAML_FILE" | grep "addresses:" | awk '{print $2}' | tr -d '[]')
ETH1_ADDRESSES=$(sudo grep -A 2 "$INTERFACE_1:" "$YAML_FILE" | grep "addresses:" | awk '{print $2}' | tr -d '[]')
# check eth0 addresses
if [ -n "$ETH0_ADDRESSES" ]; then
sudo sed -i "/$INTERFACE_0:/,/optional: yes/ {
s/addresses: \[[^]]*\]/addresses: []/
}" $YAML_FILE
fi

# check eth1 addresses
if [ -n "$ETH1_ADDRESSES" ]; then
sudo sed -i "/$INTERFACE_1:/,/optional: yes/ {
s/addresses: \[[^]]*\]/addresses: []/
}" $YAML_FILE
fi

if [ -n "$ETH0_ADDRESSES" ] || [ -n "$ETH1_ADDRESSES" ]; then
echo "net config..."
sudo netplan apply
sleep 5
fi

else
#seNCtrl_SUB_IP_0 and seNCtrl_SUB_IP_1 are for bridges config
seNCtrl_SUB_IP_0=$(ip addr show "$INTERFACE_0" | grep 'inet ' | awk '{print $2}' | cut -d'/' -f1)
seNCtrl_SUB_IP_1=$(ip addr show "$INTERFACE_1" | grep 'inet ' | awk '{print $2}' | cut -d'/' -f1)
for eth in "${seNCtrl_HOST_SUB_ETHS[@]}"; do
seNCtrl_SUB_IP_HALF=$(ifconfig "$eth" 2> /dev/null | grep "inet "|awk '{print $2}'|awk -F . '{printf("%d.%d\n", $1,$2)}')
if [ -n "$seNCtrl_SUB_IP_HALF" ]; then break; fi
done
fi
for eth in "${seNCtrl_HOST_SUB_ETHS[@]}"; do
seNCtrl_SUB_IP_HALF=$(ifconfig "$eth" 2> /dev/null | grep "inet "|awk '{print $2}'|awk -F . '{printf("%d.%d\n", $1,$2)}')
if [ -n "$seNCtrl_SUB_IP_HALF" ]; then break; fi
done

if [ "$seNCtrl_SUB_IP_HALF" = "" ]; then echo "cannot get ip to core, exit"; ifconfig; exit -1; fi
source <(sed 's/172\.16/${seNCtrl_SUB_IP_HALF}/g' ${seNCtrl_PWD}/configs/sub/subInfo.12)
sudo chmod 777 ${seNCtrl_DEBUG_UART}
Expand Down
2 changes: 2 additions & 0 deletions source/pbmsec/deb/opt/sophon/bmsec/configs/9_comInfo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,5 @@ sudo chmod +x ${seNCtrl_PWD}/commands/*
seNCtrl_OPTIONS_BY_NAME["_reset_all"]="_reset_all.sh"
#对只能进入uboot状态的算力节点进行强制刷机(需要确保/recovery/tftp下刷机包的正确性)
seNCtrl_OPTIONS_BY_NAME["_update_sub"]="_update_sub.sh"
#配置网络模式
seNCtrl_OPTIONS_BY_NAME["netconf"]="_confNetwork.sh"
1 change: 1 addition & 0 deletions source/pbmsec/deb/opt/sophon/bmsec/configs/sub/subInfo.12
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# 当前所有子设备数量,用于校验该文件是否配置正确
seNCtrl_ALL_SUB_NUM=12
seNCtrl_CONFIG_FLAG="OK"
Bridge_CONFIG_FLAG=0

unset seNCtrl_ALL_SUB_IP_ID
declare -A seNCtrl_ALL_SUB_IP_ID
Expand Down
1 change: 0 additions & 1 deletion source/pbmsec/doc/00_README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
15. 重新生成算力核心配置信息 [rconf]
16. 将指定算力核心的环境进行打包,可选生成tftp刷机包和仅打半成品包 [sysbak \<id> \<localPath>]
17. 通过此功能,用户可以编辑端口映射 [pt \<opt> [\<hostIp> \<id> \<host-port> \<core-port> \<protocol>]]

[onlyBak]]

## 注意事项
Expand Down
37 changes: 36 additions & 1 deletion source/pbmsec/doc/10_UserReferenceDocumentation_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -1154,4 +1154,39 @@ config info:
udp 172.26.166.158 16081:16082 (172.16.140.16 6) 16081-16082
udp 172.26.166.158 8554:8555 (172.16.140.11 1) 8554-8555
tcp 172.26.166.158 8022 (172.16.150.12 8) 22
```
```
18. **设置网桥模式 [netconf \<opt>]**
- 注:该功能可以在控制板netplan增加网桥配置,同时设置算力节点为DHCP4模式;如果从网桥模式切换至其他模式,除了执行`bmsec netconf <opt>`外,您还需重启整个设备。当设备处于网桥模式时,如果重启了设备,您需执行`bmsec netconf 1`,以保证网桥模式可用。
- 参数:
- `<opt>`:选项
- 0 :DHCP4端口转发模式
- 1 :网桥模式
- 说明:通过此功能,用户可以配置网络模式。
- 命令行模式示例:
- `bmsec netconf 0` DHCP4端口转发模式
- `bmsec netconf 1` 网桥模式
- 输出示例:
网桥模式
```bash
se6@se6-desktop:~$ bmsec netconf 1
config bridges...
Mode is set to 1
set all cores as DHCP MODE!
setting bridges in netplan yaml file...
backup /etc/netplan/01-network-manager-all.yaml
config netplan...
Update completed: 'wanname=br0' added.
```
DHCP4端口转发模式
```bash
se6@se6-desktop:~$ bmsec netconf 0
delete bridges config...
Mode is set to 0
reset network config of all cores
reset host netplan config...
please reboot!
```
2 changes: 1 addition & 1 deletion source/pbmsec/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ BUILD_RET=0

echo "build bmsec ..."

BMSEC_PACKAGE_VERSION="1.5.1"
BMSEC_PACKAGE_VERSION="1.6.0"

export CMD_PANDOC=$(command -v pandoc)
export CMD_DPKG_DEB=$(command -v dpkg-deb)
Expand Down

0 comments on commit 317eecb

Please sign in to comment.