主要分析两种路由器设备
This article describes the internal layout and configuration of the D-Link DIR-825. This particular hardware has two physical network interfaces, eth0
and eth1
, whereas most emebedded devices have only one: eth0
. It also has two two wireless network interfaces using the IEEE 802.11 protocol, represented by wlan0
and wlan1
.
iface | Port |
---|---|
eth0 | internal interface connected to Gigabit Switch (default) |
eth1 | internal interface connected to WAN port (default) |
wlan0 | radio0 |
wlan1 | radio1 |
usb | Bluetooth/3G/etc. |
Additional information on the rtl8366s switch. Switch 1: rtl8366s(RTL8366S), ports: 6 (cpu @ 5), vlans: 16 (4096 starting with 10.03.1-rc4)
switch port id | Label on the back |
---|---|
port:0 | LAN4 |
port:1 | LAN3 |
port:2 | LAN2 |
port:3 | LAN1 |
port:4 | WAN |
port:5 | internal |
The default config provided looks something like below:
config interface loopback option ifname lo option proto static option ipaddr 127.0.0.1 option netmask 255.0.0.0 config interface lan option ifname eth0 option type bridge option proto static option ipaddr 192.168.1.1 option netmask 255.255.255.0 config interface wan option ifname eth1 option proto dhcp config switch rtl8366s option enable 1 option reset 1 option enable_vlan 1 config switch_vlan option device rtl8366s option vlan 0 option ports "0 1 2 3 5"
Going through the configuration, step by step, provides the following information.
-
First there's the loopback interface
lo
. -
Second, in this configuration,
eth0
is part of the bridged interfacelan
. -
Third,
eth1
is configured as thewan
interface. -
Fourth, is the switch configuration.
-
The 'config switch rtl8366s' options enable the switch, reset it and enable VLAN capability.
-
The 'config switch_vlan' options enable VLAN0 and assigns it to the 4 external LAN ports and internal
eth0
interface.
Note: The eth1
network interface is assigned to VLAN1 by default, which in turn is assigned to switch port 4 by default. Further, eth1
is also configured to be part of the virtual interface wan
. Configuring wan
with a static ip address will provide another avenue to access the router using SSH. Finally, either of the wireless interfaces can be configured to enable wifi access as well.
OpenWrt的网络配置很丰富,在我看来几乎可以完成任何网络结构。下图为一个支持OpenWrt的路由器网络结构:这个路由器内部交换机有6个口,其中1个WAN口、4个LAN口、Port5默认连接内部网卡eth0,还有连接Wifi的无线网卡接eth2,eth3保留。
从中我们可以看到这个路由器最多支持3个物理网络接口eth0、eth1、eth2。其实一般路由器有两张以太网卡和一张无线网卡,但更多的路由器里只有一张以太网卡和一张无线网卡。

最让我惊奇的是OpenWrt网络的灵活性,它主要靠VLAN和(Bridging)网桥等实现。
- VLAN
一般路由器为节约成本只有一张网卡(我的理解是以太网控制芯片),但如何接入多个网络呢?(路由器按常理至少要两张网卡吧),这就是VLAN的功劳啦。
VLAN(Virtual Local Area Network)是虚拟局域网缩写,是把局域网中的同一工作组的主机连在一起,隔绝不同的子网(在一个或多个交换机上),这种局限增强了工作组间的保密性,也减少了全面广播的副作用。用了VLAN也就把连接子网用的路由器给省了,省钱而且效率还很高。
VLAN需要路由器内置的交换机支持(一般都支持啦),最多可以划分4096个VLAN。一般使用只划分两个VLAN:WAN和LAN,分别对应外网和内网。如果是纯粹的VLAN,则WAN和LAN之间是隔离的,互相不能通信,但在自己的VLAN里可以自由通信。
- Trunking
用一张网卡怎么连接两个网络呢?不知你是否留意到上图中的Port5,Port5一边连接路由器,一边连接网卡eth0,它的作用就是连接vlan0和vlan1。这就是采用VLAN的Trunking技术,在Trunk Port(port5)经过的以太网帧上打标签(Tagging)用以区分来自那个VLAN,然后路由器用网卡eth0接收并处理标签从而处理(在CPU中处理),这就是所谓的单臂路由器,很形象啊:)。
- Bridging
网桥说穿了就是交换机,它就是用软件模拟以太网交换机,连接多个以太网网卡(虚拟的或物理的)分享同一个IP子网。在OpenWrt中,网桥主要用来连接无线WIFI网络和LAN,组成一个统一的内部局域网。
最后为了方便理解,我画了一张示意图用普通的交换机和路由器解释上面提到的VLAN、Trunking和Bridging:
