0%

Newifi-D2逻辑结构探究

逻辑构造图

先放结论,经过探究,Newifi D2的网络结构图是这样的:

  • 可编程交换机各端口对应关系为
Port Switch Port
Internal (CPU) 6
Internet (WAN) 4
LAN1 3
LAN2 2
LAN3 1
LAN4 0
  • 默认的网络配置为
Interface Name Description Default configuration
br-lan LAN & 2.4GHz WiFi & 5GHz WiFi 192.168.1.1/24
vlan1 (eth0.1) LAN ports(1 - 4) None
vlan2 (eth0.2) WAN port DHCP
wlan0 2.4GHz WiFI Disabled
wlan1 5GHz WiFI Disabled

产品信息

芯片结构

Newifi已经倒闭所以找不到官网产品信息页了,根据MediaTek | MT7621A/N的介绍,MT7621A内嵌 5端口千兆以太网交换机,下面是 MT7621A Datasheet 中的芯片功能模块图,图中也可以看出WAN口和4个LAN口同属于一个Switch,他们都是交换机的端口。除了以太网交换机,芯片还包含了两个以PCIe方式连接的WiFi模块,11n WiFi11ac WiFi,分别用于2.4GHz和5GHz。

内嵌可编程交换机结构

下面是摘自 MT7621 Giga Switch Programming Guide 的交换机功能模块图以及部分寄存器配置截图,该文档描述了 MT7621A 内嵌可编程交换机的结构以及编程方法,可以看到内嵌可编程交换机共有 7 个Port,其中有5个Port引出了网口,根据交换机编程手册,可以配置 CPU_PORTPort Number

Openwrt for Newifi D2 交换机端口分配方式

我也不懂设备树文件(Device Tree Source)的具体语法,但阅读下面两段代码,大致可以看出Openwrt源码对Newifi D2硬件交换机的配置方式为:

  • Port0 - Port3 记为 lan4 - lan1
  • Port4 记为 wan
  • Port6 定义为 CPU端口

这些都是对 MT7621A 中集成的硬件交换机的配置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// From: /target/linux/ramips/dts/mt7621.dtsi
// Url: https://github.com/openwrt/openwrt/blob/master/target/linux/ramips/dts/mt7621.dtsi
ports {
#address-cells = <1>;
#size-cells = <0>;
reg = <0>;

port@0 {
status = "disabled";
reg = <0>;
label = "lan0";
};

port@1 {
status = "disabled";
reg = <1>;
label = "lan1";
};

port@2 {
status = "disabled";
reg = <2>;
label = "lan2";
};

port@3 {
status = "disabled";
reg = <3>;
label = "lan3";
};

port@4 {
status = "disabled";
reg = <4>;
label = "lan4";
};

port@6 {
reg = <6>;
label = "cpu";
ethernet = <&gmac0>;
phy-mode = "rgmii";

fixed-link {
speed = <1000>;
full-duplex;
};
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// From: /target/linux/ramips/dts/mt7621_d-team_newifi-d2.dts
// Url: https://github.com/openwrt/openwrt/blob/master/target/linux/ramips/dts/mt7621_d-team_newifi-d2.dts
&switch0 {
ports {
port@0 {
status = "okay";
label = "lan4";
};

port@1 {
status = "okay";
label = "lan3";
};

port@2 {
status = "okay";
label = "lan2";
};

port@3 {
status = "okay";
label = "lan1";
};

port@4 {
status = "okay";
label = "wan";
mtd-mac-address = <&factory 0xe006>;
};
};
};

查看网络设备

通过 ls /sys/class/net/ -al 可以查看系统中所有的网络设备:

1
2
3
4
5
6
7
8
9
root@OpenWrt:~# ls /sys/class/net/ -l
lrwxrwxrwx 1 root root 0 Jul 22 14:01 br-lan -> ../../devices/virtual/net/br-lan
lrwxrwxrwx 1 root root 0 Jan 1 1970 eth0 -> ../../devices/platform/1e100000.ethernet/net/eth0
lrwxrwxrwx 1 root root 0 Jul 22 14:01 eth0.1 -> ../../devices/virtual/net/eth0.1
lrwxrwxrwx 1 root root 0 Jul 22 14:01 eth0.2 -> ../../devices/virtual/net/eth0.2
lrwxrwxrwx 1 root root 0 Jan 1 1970 lo -> ../../devices/virtual/net/lo
lrwxrwxrwx 1 root root 0 Jul 22 14:01 pppoe-wan -> ../../devices/virtual/net/pppoe-wan
lrwxrwxrwx 1 root root 0 Jul 22 14:01 wlan0 -> ../../devices/pci0000:00/0000:00:01.0/0000:02:00.0/net/wlan0
lrwxrwxrwx 1 root root 0 Jul 22 14:01 wlan1 -> ../../devices/pci0000:00/0000:00:00.0/0000:01:00.0/net/wlan1

根据符号链接指向的位置,可以分辨出Newifi D2中有三个物理网络设备,五个虚拟网络设备。

物理设备有:

  • eth0
  • wlan0
  • wlan1

虚拟设备有:

  • br-lan
  • eth0.1
  • eth0.2
  • lo
  • pppoe-wan

系统网络信息

接口信息

通过 ip -d addr 可以查看系统中接口以及其IP地址信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 promiscuity 0 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UNKNOWN group default qlen 1000
link/ether 20:76:93:44:fe:97 brd ff:ff:ff:ff:ff:ff promiscuity 2 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535

5: br-lan: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 20:76:93:44:fe:97 brd ff:ff:ff:ff:ff:ff promiscuity 0
bridge forward_delay 200 hello_time 200 max_age 2000 ageing_time 30000 stp_state 0 priority 32767 vlan_filtering 0 vlan_protocol 802.1Q bridge_id 7fff.20:76:93:44:FE:97 designated_root 7fff.20:76:93:44:FE:97 root_port 0 root_path_cost 0 topology_change 0 topology_change_detected 0 hello_timer 0.00 tcn_timer 0.00 topology_change_timer 0.00 gc_timer 100.32 vlan_default_pvid 1 vlan_stats_enabled 0 group_fwd_mask 0 group_address 01:80:c2:00:00:00 mcast_snooping 0 mcast_router 1 mcast_query_use_ifaddr 0 mcast_querier 0 mcast_hash_elasticity 4 mcast_hash_max 512 mcast_last_member_count 2 mcast_startup_query_count 2 mcast_last_member_interval 100 mcast_membership_interval 26000 mcast_querier_interval 25500 mcast_query_interval 12500 mcast_query_response_interval 1000 mcast_startup_query_interval 3125 mcast_stats_enabled 0 mcast_igmp_version 2 mcast_mld_version 1 nf_call_iptables 0 nf_call_ip6tables 0 nf_call_arptables 0 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535
inet 192.168.2.1/24 brd 192.168.2.255 scope global br-lan
valid_lft forever preferred_lft forever

6: eth0.1@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br-lan state UP group default qlen 1000
link/ether 20:76:93:44:fe:97 brd ff:ff:ff:ff:ff:ff promiscuity 1
vlan protocol 802.1Q id 1 <REORDER_HDR>
bridge_slave state forwarding priority 32 cost 100 hairpin off guard off root_block off fastleave off learning on flood on port_id 0x8001 port_no 0x1 designated_port 32769 designated_cost 0 designated_bridge 7fff.20:76:93:44:FE:97 designated_root 7fff.20:76:93:44:FE:97 hold_timer 0.00 message_age_timer 0.00 forward_delay_timer 0.00 topology_change_ack 0 config_pending 0 proxy_arp off proxy_arp_wifi off mcast_router 1 mcast_fast_leave off mcast_flood on neigh_suppress off vlan_tunnel off numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535

7: eth0.2@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 20:76:93:44:fe:98 brd ff:ff:ff:ff:ff:ff promiscuity 0
vlan protocol 802.1Q id 2 <REORDER_HDR> numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535

8: pppoe-wan: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1492 qdisc fq_codel state UNKNOWN group default qlen 3
link/ppp promiscuity 0
ppp numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535
inet 100.82.7.242 peer 100.82.0.1/32 scope global pppoe-wan
valid_lft forever preferred_lft forever
inet6 2409:8a20:ae02:f845:539:f95a:7989:37c8/64 scope global dynamic noprefixroute
valid_lft 259059sec preferred_lft 172659sec

9: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br-lan state UP group default qlen 1000
link/ether 20:76:93:44:fe:96 brd ff:ff:ff:ff:ff:ff promiscuity 1
bridge_slave state forwarding priority 32 cost 100 hairpin on guard off root_block off fastleave off learning on flood on port_id 0x8002 port_no 0x2 designated_port 32770 designated_cost 0 designated_bridge 7fff.20:76:93:44:FE:97 designated_root 7fff.20:76:93:44:FE:97 hold_timer 0.00 message_age_timer 0.00 forward_delay_timer 0.00 topology_change_ack 0 config_pending 0 proxy_arp off proxy_arp_wifi off mcast_router 1 mcast_fast_leave off mcast_flood on neigh_suppress off vlan_tunnel off numtxqueues 4 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535

10: wlan1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br-lan state UP group default qlen 1000
link/ether 20:76:93:44:fe:98 brd ff:ff:ff:ff:ff:ff promiscuity 1
bridge_slave state forwarding priority 32 cost 100 hairpin on guard off root_block off fastleave off learning on flood on port_id 0x8003 port_no 0x3 designated_port 32771 designated_cost 0 designated_bridge 7fff.20:76:93:44:FE:97 designated_root 7fff.20:76:93:44:FE:97 hold_timer 0.00 message_age_timer 0.00 forward_delay_timer 0.00 topology_change_ack 0 config_pending 0 proxy_arp off proxy_arp_wifi off mcast_router 1 mcast_fast_leave off mcast_flood on neigh_suppress off vlan_tunnel off numtxqueues 4 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535

接下来,我们逐一分析一下各个接口所展示的信息,为了便于展示以及分析,忽略IPv6地址(已从输出中删掉):

  • lo: 虚拟设备,本地回环设备,IPv4地址为 127.0.0.1
  • eth0:物理设备,物理以太网设备,MAC地址为 20:76:93:44:fe:97,无IP地址
  • br-lan:虚拟设备,Linux网桥设备,MAC地址为 20:76:93:44:fe:97,IP地址为 192.168.2.1
  • eth0.1@eth0:虚拟设备,基于 eth0 的VLAN设备,VLAN id为1,MAC地址为 20:76:93:44:fe:97,无IP地址,bridge_slave表示它是网桥的从设备
  • eth0.2@eth0:虚拟设备,基于 eth0 的VLAN设备,VLAN id为2,MAC地址为 20:76:93:44:fe:98,无IP地址
  • pppoe-wan:虚拟设备,PPPoE拨号设备,未显示MAC地址(暂不清楚如何解释,估计是这种虚拟设备就没有MAC,应该是依赖于某个其他设备),IP地址为 100.82.7.242
  • wlan0:物理设备,无线网卡,MAC地址为 20:76:93:44:fe:96,无IP地址,bridge_slave表示它是网桥的从设备
  • wlan1:物理设备,无线网卡,MAC地址为 20:76:93:44:fe:98,无IP地址,bridge_slave表示它是网桥的从设备

桥信息

系统自带了 brctl,通过 brctl show 可以查看系统中的桥信息:

1
2
3
4
bridge name     bridge id               STP enabled     interfaces
br-lan 7fff.20769344fe97 no eth0.1
wlan0
wlan1

也可以另外安装 ip-bridge 包,然后使用 bridge 命令进行Linux虚拟网桥操作,比如 bridge link 可以展示桥中的接口:

1
2
3
6: eth0.1@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br-lan state forwarding priority 32 cost 100
9: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br-lan state forwarding priority 32 cost 100
10: wlan1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br-lan state forwarding priority 32 cost 100

上面的信息表明,网桥 br-lan 中包含三个设备:eth0.1wlan0wlan1

根据上面对各个接口的分析,得知 br-lan 有IP地址,eth0.1wlan0wlan1均没有IP地址,而网桥的作用就类似于交换机,可以在桥中各个设备直接交换MAC帧,这三者的关系如下图所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
+--------------------------------------------------------------------+
| |
| +--------------+ |
| | 192.168.2.1 | |
| +------------→| br-lan |←--------------+ |
| | +--------------+ | |
| | ↑ | |
| | | | |
| ↓ ↓ ↓ |
| +---------+ +---------+ +---------+ |
| | eth0.1 | | wlan0 | | wlan1 | |
| +---------+ +---------+ +---------+ |
| |
+--------------------------------------------------------------------+

默认网络配置

网络配置位于 /etc/config/network

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

config interface 'loopback'
option ifname 'lo'
option proto 'static'
option ipaddr '127.0.0.1'
option netmask '255.0.0.0'

config globals 'globals'
option ula_prefix 'fdff:faeb:0d9b::/48'

config interface 'lan'
option type 'bridge'
option ifname 'eth0.1'
option proto 'static'
option ipaddr '192.168.2.1'
option netmask '255.255.255.0'
option ip6assign '60'

config device 'lan_eth0_1_dev'
option name 'eth0.1'
option macaddr '20:76:93:44:fe:97'

config interface 'wan'
option ifname 'eth0.2'
option proto 'pppoe'
option password '************'
option ipv6 'auto'
option username '************'

config device 'wan_eth0_2_dev'
option name 'eth0.2'
option macaddr '20:76:93:44:fe:98'

config interface 'wan6'
option ifname 'eth0.2'
option proto 'dhcpv6'

config switch
option name 'switch0'
option reset '1'
option enable_vlan '1'

config switch_vlan
option device 'switch0'
option vlan '1'
option ports '0 1 2 3 6t'

config switch_vlan
option device 'switch0'
option vlan '2'
option ports '4 6t'

网络拓扑汇总

通过查看硬件信息,以及系统中的网络信息,可以大致得知整体的网络拓扑结构为:

参考资料&相关链接