


























来源 https://www.jianshu.com/p/a85aba5b0e98
来源 https://blog.51cto.com/u_15477596/5346162
参考 https://blog.csdn.net/mfs_bad/article/details/99642117
路由器上面基本都是用switch芯片来实现lan/wan的划分,但是有的switch只有一个GMAC,有的有多个GMAC,对于内核驱动设备来说就是有的switch生成的只有eth0,有的switch生成的有eth0和eth1。
嵌入式设备的switch 以及PHY 芯片调试和选型 :https://blog.csdn.net/noheike/article/details/105037362
openwrt官方使用的是swconfig工具,它属于package下面的一个包
https://oldwiki.archive.openwrt.org/doc/techref/swconfig
swconfig 结构框架是应用层与内核驱动通信的一种框架,主要实通过应用层命令去配置交换机芯片的底层驱动,应用层与内核层采用netlink通信机制.
首先看到
这是内核层的代码,因为交换机芯片种类繁多,需要有统一接口去兼容所有交换机驱动接口,swconfig.c就是一套定义接口。应用层的命令首先会跳到swconig.c去选择底层驱动函数。
交换机芯片驱动位置
所以使用应用层swconfig的package包时,内核也需要有相应的CONFIG_SWCONFIG=y配置开启。
/etc/init.d/network start的时候会调用setup_switch函数,该函数位于/lib/network/switch.sh中
init_switch() {
setup_switch() { return 0; }
include /lib/network
setup_switch
}
start_service() {
init_switch
procd_open_instance
procd_set_param command /sbin/netifd
procd_set_param respawn
procd_set_param watch network.interface
[ -e /proc/sys/kernel/core_pattern ] && {
procd_set_param limits core="unlimited"
}
procd_close_instance
}
/lib/network/switch.sh的代码如下,所以最终是调用swconfig来配置switch参数
#!/bin/sh
# Copyright (C) 2009 OpenWrt.org
setup_switch_dev() {
local name
config_get name "$1" name
name="${name:-$1}"
[ -d "/sys/class/net/$name" ] && ip link set dev "$name" up
swconfig dev "$name" load network
}
setup_switch() {
config_load network
config_foreach setup_switch_dev switch
}
这边配置的name为switch0是驱动查出来的
root@OpenWrt:/# swconfig list
Found: switch0 - rt305x
如下配置信息,lan配置为eth0.1则下面的vlan1为lan口的信息,wan配置为eth0.2则下面的vlan2为wan口的信息。
root@OpenWrt:/# cat /etc/config/network
config interface 'lan'
swconfig dev switch0 show可以查看具体信息
swconfig其他命令可以查看官网:http://wiki.openwrt.org/doc/techref/swconfig。
或者使用swconfig dev switch0 help命令就可以列出全部支持的命令
root@OpenWrt:/# swconfig dev switch0 show
Global attributes:
enable_vlan: 1
alternate_vlan_disable: 0
bc_storm_protect: 0
led_frequency: 0
Port 0:
disable: 0
doubletag: 0
untag: 1
led: 5
lan: 0
recv_bad: 0
recv_good: 587
tr_bad: 0
tr_good: 246
pvid: 2
link: port:0 link:up speed:100baseT full-duplex
Port 1:
...
pvid: 1
link: port:1 link:down
Port 2:
...
pvid: 1
link: port:2 link:down
Port 3:
...
pvid: 1
link: port:3 link:up speed:100baseT full-duplex
Port 4:
...
pvid: 1
link: port:4 link:down
Port 5:
...
tr_good: 0
pvid: 0
link: port:5 link:down
Port 6:
...
pvid: 0
link: port:6 link:up speed:1000baseT full-duplex
VLAN 1:
ports: 1 2 3 4 6t
VLAN 2:
ports: 0 6t

image.png
图片来自官网:https://oldwiki.archive.openwrt.org/doc/uci/network/switch
https://oldwiki.archive.openwrt.org/doc/uci/network
br-lan = eth0.1 + rai0 + ra0,即将有线LAN口和无线网统一划分为 LAN,便于管理,可以用brctl show查看使用情况。
root@Openwrt:/# brctl show
bridge name bridge id STP enabled interfaces
br-lan 7fff.008811225577 no eth0.1
ra0
ra1
如mt7531就是双GMAC的switch,datasheet上面也有标注
1-port SGMII MAC(P6), and -1-port RGMII/SGMII MAC(P5)

image.png
这就相当于P6会生成eth0给lan口使用,P5会生成eth1给wan口使用
root@OpenWrt:/# swconfig list
Found: switch0 - mt763x
如下配置信息,lan配置为eth0则下面的vlan1为lan口的信息,wan配置为eth01则下面的vlan2为wan口的信息。
root@OpenWrt:/# cat /etc/config/network
config interface 'lan'
option type 'bridge'
option ifname 'eth0'
option proto 'static'
option ipaddr '192.168.1.1'
option netmask '255.255.255.0'
option ip6assign '60'
config interface 'wan'
option ifname 'eth1'
option proto 'dhcp'
config switch
option name 'switch0'
option reset '1'
option enable_vlan '1'
config switch_vlan
option device 'switch0'
option vlan '1'
option ports '1 2 3 4 6'
config switch_vlan
option device 'switch0'
option vlan '2'
option ports '0 5'
root@Openwrt:~# swconfig dev switch0 show
Global attributes:
enable_vlan: 1
Port 0:
mib: Port 0 MIB counters
TxDrop : 0
TxCRC : 0
TxUni : 236162
TxMulti : 0
TxBroad : 224
TxCollision: 0
TxSingleCol: 0
TxMultiCol : 0
TxDefer : 0
TxLateCol : 0
TxExcCol : 0
TxPause : 0
Tx64Byte : 56862
Tx65Byte : 120468
Tx128Byte : 24152
Tx256Byte : 9256
Tx512Byte : 5214
Tx1024Byte : 20434
TxByte : 54175070
RxDrop : 0
RxFiltered : 28
RxUni : 1341775
RxMulti : 4705
RxBroad : 47850
RxAlignErr : 0
RxCRC : 0
RxUnderSize: 0
RxFragment : 0
RxOverSize : 0
RxJabber : 0
RxPause : 2
Rx64Byte : 244418
Rx65Byte : 741681
Rx128Byte : 134996
Rx256Byte : 80427
Rx512Byte : 71055
Rx1024Byte : 121755
RxByte : 351255018
RxCtrlDrop : 0
RxIngDrop : 0
RxARLDrop : 0
pvid: 2
link: port:0 link:up speed:1000baseT full-duplex
Port 1:
mib: Port 1 MIB counters
...
pvid: 1
link: port:1 link:down
Port 2:
mib: Port 2 MIB counters
...
pvid: 1
link: port:2 link:up speed:1000baseT full-duplex
Port 3:
mib: Port 3 MIB counters
...
pvid: 1
link: port:3 link:up speed:1000baseT full-duplex
Port 4:
mib: Port 4 MIB counters
...
pvid: 1
link: port:4 link:up speed:1000baseT full-duplex
Port 5:
mib: Port 5 MIB counters
...
pvid: 2
link: port:5 link:up speed:1000baseT full-duplex
Port 6:
mib: Port 6