光纤环网mstpd编译,移植到am335x
1、下载
git clone https://github.com/mstpd/mstpd
2、配置
sudo ./autogen.sh
3、配置
./configure --build=i686-linux --host=arm-arago-linux-gnueabi --target=arm-arago-linux-gnueabi --prefix=/home/picohood/projects/tools/ring_net/mstpd/install_lsh CC=arm-arago-linux-gnueabi-gcc CXX=arm-arago-linux-gnueabi-g++ --disable-udev
4、编译
make
目前只支持am335x本体的网卡
1、拷贝bridge-stp cp bridge-stp /sbin/ cp mstpd /sbin 2、创建目录 mkdir /lib/mstpctl-utils cp mstp_config_bridge ifupdown.sh ifquery mstpctl-utils-functions.sh /lib/mstpctl-utils 3、创建文件 /etc/network/bro.config 填写内容 address: 192.168.3.10 netmask: 255.255.255.0 gateway: 192.168.3.1 bridge_ports: eth0 eth1
重要注释
bridge-stp这个脚本很重要 在ifupdown.sh里会调用"$brctl" stp "$IFACE" on 该命令即是brctl stp br0 on,它是为了触发内核代码。执行判断是kernel_stp,还是user_stp;
如果在/sbin/里没有bridge-stp,应用层无论是用rstp还是mstp协议。都会强制使用内核的stp协议。
修改 /etc/network/interfaces
# bridge lsh auto lo eth0 eth1 br0 iface lo inet loopback iface br0 inet static address 192.168.3.10 netmask 255.255.255.0 gateway 192.168.3.1 pre-up ip link set eth0 promisc on pre-up ip link set eth1 promisc on pre-up echo "1" >/proc/sys/net/ipv4/ip_forward bridge_ports eth0 eth1
修改mstp_config_bridge
#!/bin/sh # # This script uses `ifquery` to read the config in # /etc/network/interfaces and then runs the mstpctl-utils # `ifupdown.sh` script to configure the specified bridge in mstpd. # # This is used by the `/sbin/bridge-stp` script when called as # `mstpctl_restart_config` or `mstp_restart` or # `/sbin/bridge-stp restart_config` or `/sbin/bridge-stp restart` to # automatically reconfigure bridges. # # This may also be used to configure mstpd on systems without ifupdown (on # systems that are not based on Debian or Ubuntu). # Parse arguments. if [ $# -ne 1 ]; then echo "Usage: $0 <bridge>" >&2 exit 1 fi Bridge="$1" # Make sure this script is being run as root. if [ "$(id -u)" != '0' ]; then echo 'This script must be run as root' >&2 exit 1 fi # Ensure that we have a sane umask. umask 022 # Ensure that we have a sane PATH. PATH='/sbin:/usr/sbin:/bin:/usr/bin' export PATH # The mstp ifupdown.sh script will not work properly unless mstpctl, brctl, and # ip exist and are executable. mstpctl='/sbin/mstpctl' if [ ! -x "$mstpctl" ]; then echo "'mstpctl' binary does not exist or is not executable" >&2 exit 2 fi brctl="$(command -v brctl)" if [ -z "$brctl" ] || [ ! -x "$brctl" ]; then echo "'brctl' binary does not exist or is not executable" >&2 exit 2 fi ip="$(command -v ip)" if [ -z "$ip" ] || [ ! -x "$ip" ]; then echo "'ip' binary does not exist or is not executable" >&2 exit 2 fi # Find ifquery. ifquery="$(command -v ifquery 2>/dev/null)" if [ -z "$ifquery" ]; then # If the real ifquery is not installed, use our emulator. ifquery='/lib/mstpctl-utils/ifquery' fi if [ ! -x "$ifquery" ]; then echo "'ifquery' binary does not exist or is not executable" >&2 # exit 2 fi # Run ifquery. #Out="$("$ifquery" "$Bridge" 2>/dev/null)" ; Err=$? #if [ $Err -ne 0 ]; then # echo "Skipping configuration of bridge '$Bridge' that is not configured in" # echo '/etc/network/interfaces' # exit 0 #fi Out=`cat /etc/network/br0.config` # Read the interface config and generate the environment variables that are # typically generated by ifupdown. export IFACE="$Bridge" export LOGICAL="$Bridge" export ADDRFAM='inet' export METHOD='manual' export MODE='start' export PHASE='post-up' export VERBOSITY=0 # add by lsh export IF_MSTPCTL_STP='on' export IF_MSTPCTL_PORTS="eth0 eth1" export IF_MSTPCTL_FORCEVERS="rstp" #export IF_MSTPCTL_HELLO='1' #export IF_MSTPCTL_FDELAY='4' eval "$( IFS= ; echo "$Out" | \ while read -r Line; do OptName="${Line%%: *}" # Strip everything after the first ': ' OptName="IF_$(echo "$OptName" | tr '[:lower:]' '[:upper:]')" # Uppercase OptVal="${Line#*: }" # Strip everything before the first ': ' echo "export $OptName=\"$OptVal\"" done )" # Call the mstpctl-utils ifupdown.sh script. '/lib/mstpctl-utils/ifupdown.sh'
修改ifupdown.sh
#!/bin/sh # This script should be symlinked into /etc/network/ip-pre-up.d/ and # /etc/network/ip-post-down.d/ on Debian/Ubuntu systems so that it will be run # by ifupdown when configuring network interfaces. # # This script is also used by `mstp_config_bridge`. # `mstp_config_bridge` is used by `/sbin/bridge-stp` when # `mstpctl_restart_config` or `mstp_restart` or # `/sbin/bridge-stp restart_config` or `/sbin/bridge-stp restart` are called to # automatically reconfigure bridges. # `mstp_config_bridge` may also be used and to configure bridges on systems # without ifupdown (on systems that are not based on Debian or Ubuntu). # # Have a look at /usr/share/doc/bridge-utils/README.Debian if you want # more info about the way in which bridges are set up on Debian. # # Author: Satish Ashok, <sashok@cumulusnetworks.com> if [ "$IF_MSTPCTL_STP" != 'on' ]; then exit 0 fi mstpctl='/sbin/mstpctl' if [ ! -x "$mstpctl" ]; then exit 0 fi brctl="$(command -v brctl)" if [ -z "$brctl" ] || [ ! -x "$brctl" ]; then echo "'brctl' binary does not exist or is not executable" >&2 exit 2 fi ip="$(command -v ip)" if [ -z "$ip" ] || [ ! -x "$ip" ]; then echo "'ip' binary does not exist or is not executable" >&2 exit 2 fi # shellcheck disable=SC1091 . '/lib/mstpctl-utils/mstpctl-utils-functions.sh' || exit 2 case "$IF_MSTPCTL_PORTS" in '') exit 0 ;; none) INTERFACES='' ;; *) INTERFACES="$IF_MSTPCTL_PORTS" ;; esac # Previous work (create the interface) if [ "$MODE" = 'start' ] && [ ! -d "/sys/class/net/$IFACE" ]; then "$brctl" addbr "$IFACE" || exit 2 # Previous work (stop the interface) elif [ "$MODE" = 'stop' ]; then "$ip" link set dev "$IFACE" down || exit 2 fi # shellcheck disable=SC2086 unset all_interfaces && # $INTERFACES should be word split into multiple arguments to # mstpctl_parse_ports. Therefore it should not be quoted here. mstpctl_parse_ports $INTERFACES | while read -r i; do for port in $i; do echo 1114000 # We attach and configure each port of the bridge if [ "$MODE" = 'start' ] && [ ! -d "/sys/class/net/$IFACE/brif/$port" ]; then if [ -x /etc/network/if-pre-up.d/vlan ]; then IFACE="$port" /etc/network/if-pre-up.d/vlan echo 1114 fi if [ "$IF_BRIDGE_HW" ]; then "$ip" link set dev "$port" down; "$ip" link set dev "$port" address "$IF_BRIDGE_HW" echo 11141 fi if [ -f "/proc/sys/net/ipv6/conf/$port/disable_ipv6" ]; then echo 1 > "/proc/sys/net/ipv6/conf/$port/disable_ipv6" echo 11142 fi "$brctl" addif "$IFACE" "$port" && "$ip" link set dev "$port" up echo 11143 # We detach each port of the bridge elif [ "$MODE" = 'stop' ] && [ -d "/sys/class/net/$IFACE/brif/$port" ]; then "$ip" link set dev "$port" down && "$brctl" delif "$IFACE" "$port" && \ if [ -x /etc/network/if-post-down.d/vlan ]; then IFACE="$port" /etc/network/if-post-down.d/vlan echo 1144 fi if [ -f "/proc/sys/net/ipv6/conf/$port/disable_ipv6" ]; then echo 0 > "/proc/sys/net/ipv6/conf/$port/disable_ipv6" echo 11141 fi fi done done # We finish setting up the bridge if [ "$MODE" = 'start' ]; then # This triggers the kernel to run `/sbin/bridge-stp start $IFACE` "$brctl" stp "$IFACE" on #add by lsh if [ -f /usrinit/netset.sh ] then ip0=`cat /usrinit/netset.sh | grep ip:br0 | tail -1 |sed 's/ip:br0 //g'` mask0=`cat /usrinit/netset.sh | grep mask:br0 | tail -1 |sed 's/mask:br0 //g'` ifconfig "$IFACE" $ip0 netmask $mask0 else ifconfig "$IFACE" 10.16.0.254 fi echo 1116 # `mstpctl addbridge $IFACE` must be called before this script continues. # If mstpd is already running then /sbin/bridge-stp will call # `mstpctl addbridge $IFACE` before `brctl stp $IFACE on` returns. # If mstpd is not already running then /sbin/bridge-stp will start it and call # `mstpctl addbridge $IFACE` as a delayed background process, in which case it # may not run before `brctl stp $IFACE on` returns. # It should not hurt to call `mstpctl addbridge $IFACE` twice, so call it # again to ensure that it has been called before continuing. # See the code and comments in /sbin/bridge-stp for more details. /sbin/mstpd & sleep 2 "$mstpctl" addbridge "$IFACE" echo 1117 if [ "$IF_MSTPCTL_MAXAGE" ]; then "$mstpctl" setmaxage "$IFACE" "$IF_MSTPCTL_MAXAGE" echo 1118 fi if [ "$IF_MSTPCTL_FDELAY" ]; then "$mstpctl" setfdelay "$IFACE" "$IF_MSTPCTL_FDELAY" echo 1119 fi if [ "$IF_MSTPCTL_MAXHOPS" ]; then "$mstpctl" setmaxhops "$IFACE" "$IF_MSTPCTL_MAXHOPS" echo 111a fi if [ "$IF_MSTPCTL_TXHOLDCOUNT" ]; then "$mstpctl" settxholdcount "$IFACE" "$IF_MSTPCTL_TXHOLDCOUNT" echo 111b fi if [ "$IF_MSTPCTL_FORCEVERS" ]; then "$mstpctl" setforcevers "$IFACE" "$IF_MSTPCTL_FORCEVERS" echo 111c fi if [ "$IF_MSTPCTL_TREEPRIO" ]; then "$mstpctl" settreeprio "$IFACE" 0 "$IF_MSTPCTL_TREEPRIO" echo 111d fi if [ "$IF_MSTPCTL_PORTPATHCOST" ]; then portpathcosts="$(echo "$IF_MSTPCTL_PORTPATHCOST" | tr '\n' ' ' | tr -s ' ')" for portpathcost in $portpathcosts; do port="$(echo "$portpathcost" | cut -d '=' -f1)" pathcost="$(echo "$portpathcost" | cut -d '=' -f2)" if [ -n "$port" ] && [ -n "$pathcost" ]; then "$mstpctl" setportpathcost "$IFACE" "$port" "$pathcost" echo 111e fi done fi if [ "$IF_MSTPCTL_PORTADMINEDGE" ]; then portadminedges="$(echo "$IF_MSTPCTL_PORTADMINEDGE" | tr '\n' ' ' | tr -s ' ')" for portadminedge in $portadminedges; do port="$(echo "$portadminedge" | cut -d '=' -f1)" adminedge="$(echo "$portadminedge" | cut -d '=' -f2)" if [ -n "$port" ] && [ -n "$adminedge" ]; then "$mstpctl" setportadminedge "$IFACE" "$port" "$adminedge" echo 111f fi done fi if [ "$IF_MSTPCTL_PORTAUTOEDGE" ]; then portautoedges="$(echo "$IF_MSTPCTL_PORTAUTOEDGE" | tr '\n' ' ' | tr -s ' ')" for portautoedge in $portautoedges; do port="$(echo "$portautoedge" | cut -d '=' -f1)" autoedge="$(echo "$portautoedge" | cut -d '=' -f2)" if [ -n "$port" ] && [ -n "$autoedge" ]; then "$mstpctl" setportautoedge "$IFACE" "$port" "$autoedge" echo 2111 fi done fi if [ "$IF_MSTPCTL_PORTP2P" ]; then portp2ps="$(echo "$IF_MSTPCTL_PORTP2P" | tr '\n' ' ' | tr -s ' ')" for portp2p in $portp2ps; do port="$(echo "$portp2p" | cut -d '=' -f1)" p2p="$(echo "$portp2p" | cut -d '=' -f2)" if [ -n "$port" ] && [ -n "$p2p" ]; then "$mstpctl" setportp2p "$IFACE" "$port" "$p2p" echo 3111 fi done fi if [ "$IF_MSTPCTL_PORTRESTRROLE" ]; then portrestrroles="$(echo "$IF_MSTPCTL_PORTRESTRROLE" | tr '\n' ' ' | tr -s ' ')" for portrestrrole in $portrestrroles; do port="$(echo "$portrestrrole" | cut -d '=' -f1)" restrrole="$(echo "$portrestrrole" | cut -d '=' -f2)" if [ -n "$port" ] && [ -n "$restrrole" ]; then "$mstpctl" setportrestrrole "$IFACE" "$port" "$restrrole" echo 4111 fi done fi if [ "$IF_MSTPCTL_PORTRESTRTCN" ]; then portrestrtcns="$(echo "$IF_MSTPCTL_PORTRESTRTCN" | tr '\n' ' ' | tr -s ' ')" for portrestrtcn in $portrestrtcns; do port="$(echo "$portrestrtcn" | cut -d '=' -f1)" restrtcn="$(echo "$portrestrtcn" | cut -d '=' -f2)" if [ -n "$port" ] && [ -n "$restrtcn" ]; then "$mstpctl" setportrestrtcn "$IFACE" "$port" "$restrtcn" echo 5111 fi done fi if [ "$IF_MSTPCTL_BPDUGUARD" ]; then portbpduguards="$(echo "$IF_MSTPCTL_BPDUGUARD" | tr '\n' ' ' | tr -s ' ')" for portbpduguard in $portbpduguards; do port="$(echo "$portbpduguard" | cut -d '=' -f1)" bpduguard="$(echo "$portbpduguard" | cut -d '=' -f2)" if [ -n "$port" ] && [ -n "$bpduguard" ]; then "$mstpctl" setbpduguard "$IFACE" "$port" "$bpduguard" echo 6111 fi done fi if [ "$IF_MSTPCTL_PORTBPDUFILTER" ]; then portbpdufilters="$(echo "$IF_MSTPCTL_PORTBPDUFILTER" | tr '\n' ' ' | tr -s ' ')" for portbpdufilter in $portbpdufilters; do port="$(echo "$portbpdufilter" | cut -d '=' -f1)" bpdufilter="$(echo "$portbpdufilter" | cut -d '=' -f2)" if [ -n "$port" ] && [ -n "$bpdufilter" ]; then "$mstpctl" setportbpdufilter "$IFACE" "$port" "$bpdufilter" echo 6111 fi done fi if [ "$IF_MSTPCTL_TREEPORTPRIO" ]; then treeportprios="$(echo "$IF_MSTPCTL_TREEPORTPRIO" | tr '\n' ' ' | tr -s ' ')" for treeportprio in $treeportprios; do treeport="$(echo "$treeportprio" | cut -d '=' -f1)" prio="$(echo "$treeportprio" | cut -d '=' -f2)" if [ -n "$treeport" ] && [ -n "$prio" ]; then "$mstpctl" settreeportprio "$IFACE" "$treeport" 0 "$prio" echo 7111 fi done fi if [ "$IF_MSTPCTL_TREEPORTCOST" ]; then treeportcosts="$(echo "$IF_MSTPCTL_TREEPORTCOST" | tr '\n' ' ' | tr -s ' ')" for treeportcost in $treeportcosts; do treeport="$(echo "$treeportcost" | cut -d '=' -f1)" cost="$(echo "$treeportcost" | cut -d '=' -f2)" if [ -n "$treeport" ] && [ -n "$cost" ]; then "$mstpctl" settreeportcost "$IFACE" "$treeport" 0 "$cost" echo 8111 fi done fi if [ "$IF_MSTPCTL_HELLO" ]; then "$mstpctl" sethello "$IFACE" "$IF_MSTPCTL_HELLO" echo 9111 fi if [ "$IF_MSTPCTL_AGEING" ]; then "$mstpctl" setageing "$IFACE" "$IF_MSTPCTL_AGEING" echo 3112 fi if [ "$IF_MSTPCTL_PORTNETWORK" ]; then portnetworks="$(echo "$IF_MSTPCTL_PORTNETWORK" | tr '\n' ' ' | tr -s ' ')" for portnetwork in $portnetworks; do port="$(echo "$portnetwork" | cut -d '=' -f1)" network="$(echo "$portnetwork" | cut -d '=' -f2)" if [ -n "$port" ] && [ -n "$network" ]; then "$mstpctl" setportnetwork "$IFACE" "$port" "$network" echo 33 fi done fi # We activate the bridge "$ip" link set dev "$IFACE" up # Calculate the maximum time to wait for STP to converge maxwait='' if [ -n "$IF_MSTPCTL_MAXWAIT" ] && [ "$IF_MSTPCTL_MAXWAIT" != 'auto' ]; then # if [ "$IF_MSTPCTL_MAXWAIT" is a number ]; then if [ "$IF_MSTPCTL_MAXWAIT" = "${IF_MSTPCTL_MAXWAIT%[!0-9]*}" ]; then maxwait="$IF_MSTPCTL_MAXWAIT" echo 34 else echo "Ignoring invalid mstpctl_maxwait value '$maxwait'" >&2 fi fi if [ -z "$maxwait" ]; then root_forward_delay="$("$mstpctl" showbridge "$IFACE" forward-delay)" if [ -z "$root_forward_delay" ] || [ "$root_forward_delay" != "${root_forward_delay%[!0-9]*}" ]; then root_forward_delay=0 fi bridge_forward_delay="$("$mstpctl" showbridge "$IFACE" bridge-forward-delay)" if [ -z "$bridge_forward_delay" ] || [ "$root_forward_delay" != "${bridge_forward_delay%[!0-9]*}" ]; then bridge_forward_delay=0 fi if [ $root_forward_delay -gt $bridge_forward_delay ]; then maxwait="$root_forward_delay" else maxwait="$bridge_forward_delay" fi maxwait=$((maxwait*2+1)) fi # Wait for STP to converge if [ $maxwait -ne 0 ]; then echo echo "Waiting for STP on bridge '$IFACE' to converge (mstpctl_maxwait is $maxwait seconds)." # Use 0.1 delay if available sleep 0.1 2>/dev/null && maxwait=$((maxwait*10)) count=0 ; transitioned='' ; converged='' while [ -z "$converged" ] && [ $count -lt $maxwait ]; do sleep 0.1 2>/dev/null || sleep 1 count=$((count+1)) # Converged if all ports are either 'forwarding', 'blocking', or # 'disabled', except if all ports are 'disabled' and we have yet to see # any port transition to any other valid state. converged=true for i in $("$brctl" showstp "$IFACE" | sed -n 's/^.*port id.*state[ \t]*\(.*\)$/\1/p'); do if [ "$i" = 'listening' ] || [ "$i" = 'learning' ]; then transitioned=true converged='' break elif [ "$i" = 'forwarding' ] || [ "$i" = 'blocking' ]; then transitioned=true elif [ "$i" != 'disabled' ] || [ -z "$transitioned" ]; then converged='' fi done done if [ -z "$converged" ]; then echo "Timed out waiting for STP on bridge '$IFACE' to converge" >&2 fi fi # Finally we destroy the interface elif [ "$MODE" = 'stop' ]; then "$brctl" delbr "$IFACE" fi
运行
1、mstpd 2、sleep 5 3、./mstp_config_bridge br0