3

如何在OpenHarmony上进行双网卡设置

 1 year ago
source link: https://www.51cto.com/article/746125.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

如何在OpenHarmony上进行双网卡设置

作者:软通动力HOS 2023-02-09 15:32:55
本文带大家了解如何配置双网卡达成同时访问内、外网的目的,该示例相对简单,重点是需要将默认网关配置为外网网关。
716f58001841aaef1d6332492cc5d7f81ff890.png

​想了解更多关于开源的内容,请访问:​

​51CTO 开源基础软件社区​

​https://ost.51cto.com​

在某些特殊场景下,我们可能有这样的网络配置诉求,即:访问内网同时也需要访问外网。
本文主要针对该场景进行梳理,以双网卡配置为例,指导初学者进行网卡配置,达成同时访问内外网。

双网卡配置硬件前提条件:

硬件方面:板卡有2个网卡。
双网卡配置规则:1、连接外网LAN口,配置外网网段IP、子网掩码、路由和网关;2、连接内网LAN口,配置内网网段IP、子网掩码和路由。
说明:特别要注意,一台设备不能同时配置两个网关,且网关只能配置为外网网关,否则设备将无法区分外网出口,导致外网访问失败。
以下示例内外网的IP分配如下:(具体以你所在的网络为准)

如下为例:

外网:IP地址10.61.*** 掩码:255.0.0.0 路由:10.61.*** 网关:10.61.***
内网:IP地址192.168.*** 掩码:255.255.255.0 路由:192.168.0.0

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:15 errors:0 dropped:0 overruns:0 frame:0
TX packets:15 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1320 TX bytes:1320
eth0 Link encap:Ethernet HWaddr 96:d0:79:2d:9b:88 Driver rk_gmac-dwmac
inet addr:10.61.*** Bcast:10.255.255.255 Mask:255.0.0.0
inet6 addr: fe80::94d0:79ff:fe2d:9b88/64 Scope: Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:28813 errors:0 dropped:0 overruns:0 frame:0
TX packets:3445 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2706345 TX bytes:330787
Interrupt:35
eth1 Link encap:Ethernet HWaddr c2:5c:62:d7:1f:20 Driver smsc75xx
inet addr:192.168.*** Bcast:192.168.*** Mask:255.255.255.0
inet6 addr: fe80::c05c:62ff:fed7:1f20/64 Scope: Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:3282 errors:0 dropped:0 overruns:0 frame:0
TX packets:488 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:280938 TX bytes:55862
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 10.61.*** 0.0.0.0 UG 0 0 0 eth0
10.0.0.0 * 255.0.0.0 U 0 0 0 eth0
10.61.*** * 255.255.252.0 U 0 0 0 eth0
192.168.0.0 * 255.255.255.0 U 0 0 0 eth1

本例中设置192网段为内网,10网段为外网,经过以上配置后,ping内网(例如:192.168.***)正常,ping外网(例如:ping www.baidu.com)正常。说明同时访问内外网成功

Ping 192.168.*** (192.168.***): 56(84) bytes.
64 bytes from 192.168.***: icmp_seq=1 ttl=0 time=1 ms
64 bytes from 192.168.***: icmp_seq=2 ttl=0 time=2 ms
64 bytes from 192.168.***: icmp_seq=3 ttl=0 time=1 ms
--- 192.168.*** ping statistics ---
3 packets transmitted, 3 received, 0% packet loss
round-trip min/avg/max = 0/0/1 ms
# ping 10.61.***
Ping 10.61.*** (10.61.***): 56(84) bytes.
64 bytes from 10.61.***: icmp_seq=1 ttl=0 time=0 ms
64 bytes from 10.61.***: icmp_seq=2 ttl=0 time=0 ms
64 bytes from 10.61.***: icmp_seq=3 ttl=0 time=1 ms
--- 10.61.*** ping statistics ---
3 packets transmitted, 3 received, 0% packet loss
round-trip min/avg/max = 0/0/0 ms

知识总结:
以上已经成功完成了双网卡配置,下面我们来看下相关配置命令。
1、相关调试命令
1)删除路由

route del -net 10.0.0.0 netmask 255.0.0.0 dev eth1
route del -net 10.61.*** netmask 255.255.252.0 dev eth1

2)删除默认网关

route del default

3)配置IP

ifconfig eth0 192.168.*** //给eth0网卡配置IP地址
ifconfig eth0 192.168.*** netmask 255.255.255.0 //给eth0网卡配置IP,并加mask
ifconfig eth0 192.168.*** netmask 255.255.255.0 broadcast 192.168.120.255

4)配置默认网关

route add default gw 10.61.*** dev eth0`

5)配置路由

route add -net 10.61.*** netmask 255.255.252.0 dev eth0`

2、路由及IP在板子保存位置:
1)路由保存位置:/proc/net/route

# cat proc/net/route
Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
eth0 00000000 01C43D0A 0003 0 0 0 00000000 0 0 0
eth0 0000000A 00000000 0001 0 0 0 000000FF 0 0 0
eth0 00C43D0A 00000000 0001 0 0 0 00FCFFFF 0 0 0
eth1 0000A8C0        00000000        0001    0       0       0       00FFFFFF        0       0       0

2)静态IP保存位置

# cat data/ethernet/eth0
DEVICE=eth0
BOOTPROTO=STATIC
IPADDR=10.61.***
NETMASK=255.255.255.0
GATEWAY=10.61.***
ROUTE=10.61.***
# cat data/ethernet/eth1
DEVICE=eth1
BOOTPROTO=STATIC
IPADDR=192.168.***23
NETMASK=255.255.255.0
GATEWAY=192.168.***
ROUTE=192.168.***

3)动态IP(dhcp)保存位置 :/data/dhcp/eth0.result

# cat /data/dhcp/eth0.result
IP4 1666952418 10.61.*** 10.61.*** 255.255.252.0 10.61.10.6 10.60.10.6 10.61.*** *

问题总结:
【问题现象】:拔掉网线不自动清除IP和路由
【原因分析】:拔掉网线后事件触发StopDhcpClient,但未同步更新网络信息;
【解决方案】:
在foundation\communication\netmanager_ext\services\ethernetmanager\src\ethernet_management.cpp 的StopDhcpClient()函数中增加更新网络链接状态。

index 9b03ad8..0c4f708 100644
#NAME?
#NAME?
@@ -569,6 +569,7 @@ void EthernetManagement::StopDhcpClient(const std::string &dev, sptr<DevInterfac
{
NETMGR_EXT_LOG_D("EthernetManagement StopDhcpClient[%{public}s]", dev.c_str());
ethDhcpController_->StopDhcpClient(dev, false);
+ devState->RemoteUpdateNetLinkInfo();
devState->SetDhcpReqState(false);
}
@@ -581,4 +582,4 @@ void EthernetManagement::SetDevState(sptr<DevInterfaceState> &devState, const st
devState->SetLowerUp(lowerUp);
}
} // namespace NetManagerStandard
-} // namespace OHOS
\ No newline at end of file
+} // namespace OHOS

通过以上示例指导,相信大家已经掌握了如何配置双网卡达成同时访问内、外网的目的,该示例相对简单,重点是需要将默认网关配置为外网网关。同时,通过该示例,相信大家也熟练掌握了网络配置相关命令。当然,同时访问内网和外网的网络结构,不止示例中一种,大家也可以不断尝试,举一反三。

​想了解更多关于开源的内容,请访问:​

​51CTO 开源基础软件社区​

​https://ost.51cto.com​

责任编辑:姜华 来源: 51CTO 开源基础软件社区

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK