美文网首页
在MT7628实现wan、wwan与4G动态切换

在MT7628实现wan、wwan与4G动态切换

作者: Mr_Michael | 来源:发表于2019-04-10 16:59 被阅读0次

    前言

    本人使用基于MT7628的开发板运行openwrt lede17.01系统,单独使用有线wan、无线路由wwan和4G功能时都很正常。但是当同时使用以上3种网络接口时,发现接口之间并不能动态切换,当拔掉网线时,即使wifi和4g都正常连接,但是路由器不能ping通外网。当wifi连接断了,也会影响剩余网络接口的通信。

    image

    经过一番调试,发现当网线热插拔时,系统并没有触发链路断开事件通知netifd进程来关闭网络,导致其接口的配置残留在路由表中,从而影响其他接口搜索默认网关。

    image

    根本原因是MT7628系列设备具有内置交换机功能,由于交换机和CPU端口之间始终连接,因此本地以太网接口永远不会看到链路断开事件,这意味着netifd将不会关闭并取消配置关联的网络,导致热插拔无效。

    解决方法

    既然是硬件原因导致无法使用热插拔事件,那只好通过轮询方式来检测网络。定期对外网进行ping测试,如果测试正常,则无需切换网络;如果测试失败,则自动切换到另一个网络接口,以此循环。通过以下脚本,能实现网络接口的动态切换。

    1.网络测试及切换脚本

    通过以下脚本,可以实现周期ping测试和网络接口切换功能。

    脚本名称:/etc/pingtest

    #!/bin/sh
     
    state=0
    IP="www.baidu.com"
    
    ping_status() {
        for var in 1 2 3; do
            if ping -c 1 $IP > /dev/null; then
                #echo "$IP Ping is successful."
                return 0
            else
                #echo "$IP Ping is failure"
                if [ $var -eq 3 ]; then
                    return 1
                fi
        fi
        done
    }
    
    while true
    do 
        ping_status
        
        if [ $? -ne 0 ]; then
            case $state in
                0)  #echo "wan to wwan"
                    if uci get network.wwan > /dev/null; then
                        uci set network.wwan.metric=0
                    fi
                    
                    if uci get network.wan > /dev/null; then
                        uci set network.wan.metric=20
                    fi
    
                    if uci get network.4G > /dev/null; then
                        uci set network.4G.metric=10
                    fi
                ;;
                1)  #echo "wwan to 4G"
                    if uci get network.wwan > /dev/null; then
                        uci set network.wwan.metric=20
                    fi
                    
                    if uci get network.wan > /dev/null; then
                        uci set network.wan.metric=10
                    fi
    
                    if uci get network.4G > /dev/null; then
                        uci set network.4G.metric=0
                    fi
                ;;
                2)  #echo "4G to wan"
                    if uci get network.wwan > /dev/null; then
                        uci set network.wwan.metric=10
                    fi
                    
                    if uci get network.wan > /dev/null; then
                        uci set network.wan.metric=0
                    fi
    
                    if uci get network.4G > /dev/null; then
                        uci set network.4G.metric=20
                    fi
                ;;
            esac
    
            uci commit network;
            /etc/init.d/network reload;
            let state++
            if [ ${state} -ge 3 ]; then
                state=0
            fi
        fi
        
        sleep 10
    
    done
    
    • 进行对外网进行一轮ping测试,最多测试3次,当有一次测试通过则代表成功,不作网络处理;如果3次测试失败,则表示网络已断。

    • 如果网络已断,通过UCI命令修改network文件中各接口的默认网关优先级参数metric(值越小优先级越高),使用uci commit network命令保存修改,并重新加载配置文件:/etc/init.d/network reload。

    • 完成一轮检测后,10秒后再次检测,以此循环。如果当前使用的网络接口断了,脚本就会自动切换到适合的下一个网络接口。

    2.开机自启脚本

    在/etc/init.d/目录下编写开机启动的shell脚本,该脚本运行时,在后台启动/etc/pingtest脚本。

    脚本名称:/etc/init.d/interface_check

    #!/bin/sh /etc/rc.common
    START=98
    STOP=99
    
    USE_PROCD=1
    
    start_service()
    {
        procd_open_instance
        procd_set_param respawn
        . /etc/pingtest &
        procd_close_instance
    }
    
    stop_service() 
    {
        return;
    }
    
    • START=98,STOP=99分别是启动和关闭和优先级

    • USE_PROCD=1表示使用进程,配合procd_open_instance和
      procd_close_instance命令启动新进程,让/etc/pingtest脚本在进程中运行。

    3.开启启动脚本软链接

    在/etc/rc.d目录下新建开机启动脚本/etc/init.d/interface_check的软链接,下次开机脚本就会自动运行。

    ln -s /etc/init.d/interface_check  /etc/rc.d/S98interface_check
    
    • S98是脚本启动的优先级

    相关文章

      网友评论

          本文标题:在MT7628实现wan、wwan与4G动态切换

          本文链接:https://www.haomeiwen.com/subject/jxkoiqtx.html