首页 > 其他分享 >pingtest

pingtest

时间:2023-04-09 09:33:14浏览次数:67  
标签:USB pingtest dev wwan network uci usb

pingtest 

来源 https://www.jianshu.com/p/f058229a42d8

 

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

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

根本原因是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是脚本启动的优先级

 

 ===============

来源  https://www.jianshu.com/p/0951be9aa200

1.修改linux内核

1)移植USB Serial驱动

​ 移植后模块连接到USB串行驱动时,驱动程序将在目录/dev中创建设备文件USB0/ttyUSB1/ttyUSB2…

  • 增加PID&VID

    • 修改openwrt/build_dir/target-mipsel_24kc_glibc-2.24/linux-ramips_mt7688/linux-4.4.167/drivers/usb/serial/option.c文件

      static const struct usb_device_id option_ids[] = {
      #if 1 //Added by Quectel
      { USB_DEVICE(0x05C6, 0x9090) }, /* Quectel UC15 */
      { USB_DEVICE(0x05C6, 0x9003) }, /* Quectel UC20 */
      { USB_DEVICE(0x2C7C, 0x0125) }, /* Quectel EC25 EM05*/
      { USB_DEVICE(0x2C7C, 0x0121) }, /* Quectel EC21 */
      { USB_DEVICE(0x05C6, 0x9215) }, /* Quectel EC20 */
      { USB_DEVICE(0x2C7C, 0x0191) }, /* Quectel EG91 */
      { USB_DEVICE(0x2C7C, 0x0195) }, /* Quectel EG95 */
      { USB_DEVICE(0x2C7C, 0x0306) }, /* Quectel EG06/EP06/EM06 */
      { USB_DEVICE(0x2C7C, 0x0296) }, /* Quectel BG96 */
      { USB_DEVICE(0x2C7C, 0x0435) }, /* Quectel AG35 */
      #endif
      
  • 添加零包处理

    • 修改openwrt/build_dir/target-mipsel_24kc_glibc-2.24/linux-ramips_mt7688/linux-4.4.167/drivers/usb/serial/usb_wwan.c文件

      static struct urb *usb_wwan_setup_urb(struct usb_serial_port *port,
                            int endpoint,
                            int dir, void *ctx, char *buf, int len,
                            void (*callback) (struct urb *))
      {
          struct usb_serial *serial = port->serial;
          struct urb *urb;
      
          urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
          if (!urb)
              return NULL;
      
          usb_fill_bulk_urb(urb, serial->dev,
                    usb_sndbulkpipe(serial->dev, endpoint) | dir,
                    buf, len, callback, ctx);
                    
      #if 1 //Added by Quectel for zero packet
          if (dir == USB_DIR_OUT) {
              struct usb_device_descriptor *desc = &serial->dev->descriptor;
              if (desc->idVendor == cpu_to_le16(0x05C6) && desc->idProduct == cpu_to_le16(0x9090))
                  urb->transfer_flags |= URB_ZERO_PACKET;
              if (desc->idVendor == cpu_to_le16(0x05C6) && desc->idProduct == cpu_to_le16(0x9003))
                  urb->transfer_flags |= URB_ZERO_PACKET;
              if (desc->idVendor == cpu_to_le16(0x05C6) && desc->idProduct == cpu_to_le16(0x9215))
                  urb->transfer_flags |= URB_ZERO_PACKET;
              if (desc->idVendor == cpu_to_le16(0x2C7C))
                  urb->transfer_flags |= URB_ZERO_PACKET;
              if (desc->idVendor == cpu_to_le16(0x1286) && desc->idProduct == cpu_to_le16(0x4e3d))
              {
                  urb->transfer_flags |= URB_ZERO_PACKET;
              }
          }
      #endif
      
          return urb;
      }
      
  • 增加休眠后唤醒接口

    • 当MCU进入暂停/休眠模式时,一些USB主机控制器/USB集线器将失去电源或重新设置,并且在MCU退出暂停/休眠模式后,它们不能恢复USB设备。请添加以下语句以启用重新设置恢复过程。

    • 修改openwrt/build_dir/target-mipsel_24kc_glibc-2.24/linux-ramips_mt7688/linux-4.4.167/drivers/usb/serial/option.c文件。

      static struct usb_serial_driver option_1port_device = {
      ……
      #ifdef CONFIG_PM
          .suspend = usb_wwan_suspend,
          .resume = usb_wwan_resume,
      #if 1 //Added by Quectel
          .reset_resume = usb_wwan_resume,
      #endif
      #endif
      };
      

2.修改openwrt配置

  • 执行命令make menuconfig进入配置
1)内核模块
Kernel modules >>
    USB Support >>
        <*> Kmod -usb-core
        -*-Kmod -usb-net
        -*- kmod-usb-net-cdc-ether//【可选】
        <*> kmod-usb-net-cdc-mbim
        -*- kmod-usb-net-cdc-ncm
        <*> kmod-usb-net-cdc-subset//【可选】
        <*>kmod-usb-net-qmi-wwan
        <*>Kmod-usb-ohci     //这个选项一定要勾选,否则可能无法在系统中查看设备
        <*>Kmod-usb-serial
        <*>Kmod-usb-serial-option
        <*>Kmod-usb-serial-wwan
        <*>kmod-usb-uhci
        <*>Kmod-usb2
2)网络配置
NetWork   >>
    <*>wwan
    <*>chat
    <*>ppp
    <*>uqmi
    <*>umbim
3)Utilities配置
Utilities
  <*>comgt
  <*>usb-modeswitch //自动模式更改,将设备置于USB调制解调器模式
  <*>usbutils//【可选,可查看usb设备】
4)Luci配置
Luci
1. Collections
        <*> luci
3. Applications
        <*> luci-app-multiwan (optional to support multiple 3g dongles)//新版本为mwan3 【可选】
        <*> luci-app-qos (optional to provide QOS support)//【可选】
6. Protocols
        <*> luci-proto-3g
        -*- luci-proto-ppp

3.网关配置

1)接入4G模块后,使用ls /dev命令,可以看到usb转串口设备
 
  • lsusb 命令查看usb设备

    root@LEDE:~# lsusb
    Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 001 Device 012: ID 2c7c:0125  #PID-2c7c:VID-0125
    Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
    
2)使用USB 3g / UMTS-modem进行WAN连接
  • 参考https://openwrt.org/docs/guide-user/network/wan/wwan/3gdongle

  • 修改\etc\config\network配置

    config interface '4G'
            option ifname 'ppp0'
            option _orig_ifname 'eth0.2'
            option _orig_bridge 'false'
            option proto '3g'
            option device '/dev/ttyUSB2'
            option service 'umts'
            option ipv6 'auto'
            option keepalive '10 5'
            option metric '0'
    
  • 重启网络

    /etc/init.d/network restart
    
    • 等待4G连接成功
    root@LEDE:~# ifconfig 
    3g-4G     Link encap:Point-to-Point Protocol  
              inet addr:10.124.22.77  P-t-P:10.64.64.64  Mask:255.255.255.255
              UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
              RX packets:25 errors:0 dropped:0 overruns:0 frame:0
              TX packets:58 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:3 
              RX bytes:1762 (1.7 KiB)  TX bytes:3818 (3.7 KiB)
    
    br-lan    Link encap:Ethernet  HWaddr C6:E5:72:41:19:1D  
              inet addr:192.168.2.1  Bcast:192.168.2.255  Mask:255.255.255.0
              inet6 addr: fe80::c4e5:72ff:fe41:191d/64 Scope:Link
              inet6 addr: fd3f:6922:178d::1/60 Scope:Global
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:19 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:0 (0.0 B)  TX bytes:2610 (2.5 KiB)
    
    eth0      Link encap:Ethernet  HWaddr 40:D6:3C:1E:70:14  
              inet6 addr: fe80::42d6:3cff:fe1e:7014/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:5519 errors:0 dropped:4 overruns:0 frame:0
              TX packets:1579 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:476283 (465.1 KiB)  TX bytes:152174 (148.6 KiB)
              Interrupt:5 
    
    eth0.1    Link encap:Ethernet  HWaddr C6:E5:72:41:19:1D  
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:19 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:0 (0.0 B)  TX bytes:2610 (2.5 KiB)
    
    eth0.2    Link encap:Ethernet  HWaddr C6:E5:72:41:19:1E  
              inet addr:192.168.3.94  Bcast:192.168.3.255  Mask:255.255.255.0
              inet6 addr: fe80::c4e5:72ff:fe41:191e/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:1170 errors:0 dropped:0 overruns:0 frame:0
              TX packets:280 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:70616 (68.9 KiB)  TX bytes:22980 (22.4 KiB)
    
    lo        Link encap:Local Loopback  
              inet addr:127.0.0.1  Mask:255.0.0.0
              inet6 addr: ::1/128 Scope:Host
              UP LOOPBACK RUNNING  MTU:65536  Metric:1
              RX packets:2204 errors:0 dropped:0 overruns:0 frame:0
              TX packets:2204 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1 
              RX bytes:189751 (185.3 KiB)  TX bytes:189751 (185.3 KiB)
    
    wlan0     Link encap:Ethernet  HWaddr 40:D6:3C:1E:70:14  
              inet6 addr: fe80::42d6:3cff:fe1e:7014/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:13 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:0 (0.0 B)  TX bytes:1856 (1.8 KiB)

 

============== End

 

标签:USB,pingtest,dev,wwan,network,uci,usb
From: https://www.cnblogs.com/lsgxeva/p/17299843.html

相关文章