Chapter8 Virtual LANs(VLANs)
 
 
VLAN Basics
 
如何在 1 个交换性的网络里,分割广播域呢?答案是创建 VLAN.VLAN 是连接到定义好了的
switch 的端口的网络用户和资源的逻辑分组.给不同的子网分配不同的端口,就可以创建更小
的广播域.默认情况下,在某个 VLAN 中的主机是不可以与其他 VLAN 通信的,除非你使用
router来创建 VLAN 间的通信
 
VLAN 的一些特点:
1.网络的增加,移动和改变,只需要在适当的 VLAN 中配置合适的端口
2.安全,因为不同 VLAN 的用户不能互相通信,除非依靠 router来做 VLAN 间的通信
3.因为 VLAN 可以被认为是按功能划分的逻辑分组,所以 VLAN 和物理位置,地理位置无关
4.VLAN 增加安全性
5.VLAN 增加广播域的数量,而减小广播域的大小
 
Broadcast Control
 
每种协议都会有广播的现象发生,至于发生不频率,次数,一般由以下几点决定:
1.协议类型
2.在网络上运行的应用程序
3.这些服务如何的被使用
 
Security
 
安全性是 VLAN 的 1 大特点,不同 VLAN 的用户不能互相通信,除非依靠 router 来做 VLAN
间的通信
 
Flexibility and Scalability
 
VLAN 的灵活性和可扩展性:
1.可以不管物理位置如何,把适当的端口分配到适当的 VLAN 中就可以了.可以把 VLAN 理
解成下面的样子,如图: 
2.当 VLAN 增加的太大以后,你可以划分更多的 VLAN,来减少广播消耗掉更多带宽的影响,
在 VLAN 中的用户越少,被广播影响的就越少
来比较下下面 2 个图,明显可以发现,图 2,即 VLAN的具有更高的灵活性和可扩展性,如下:
 
 
VLAN Memberships
 
手动由管理员分配端口划分的VLAN叫静态VLAN(static VLAN);使用智能管理软件,动态划
分 VLAN 的叫动态 VLAN(dynamic VLAN)
 
Static VLANs
 
静态 VLAN:静态 VLAN 安全性较高,手动划分端口给 VLAN,和设备的物理位置没什么关系.
而且,每个 VLAN 中的主机必须拥有正确的 IP地址信息,如 VLAN2 配置为 172.16.20.0/24
 
Dynamic VLANs
 
动态 VLAN:使用智能管理软件,可以基于 MAC 地址,协议,甚至应用程序来动态创建
VLAN.Cisco 设备管理员可以使用 VLAN 管理策略服务器(VLAN Management Policy
Server,VMPS)的服务来建立个 MAC 地址数据库,来根据这个动态创建 VLAN,VMPS 数据库
把 MAC 地址映射 VLAN 上
 
Identifying VLANs
 
当帧在网络中被交换,switches 根据类型对其跟踪,加上根据硬件地址来判断如何对它们进行
操作.有 1 点要记住的是:在不同类型的连接中,帧被处理的方式也不一样
 
交换环境中的 2 种连接类型:
1.access links:指的是只属于一个 VLAN,且仅向该 VLAN 转发数据帧的端口,也叫做 native
VLAN.switches 把帧发送到 access-link 设备之前,移去任何的 VLAN 信息.而且 access-link 设
备不能与 VLAN 外通信,除非数据包被路由
2.trunk links:指的是能够转发多个不同 VLAN 的通信的端口.trunk link 必须使用 100Mbps 以上的端口来进行点对点连接,1次最多可以携带1005个VLAN信息.trunk link使你的单独的1
个端口同时成为数个 VLAN 的端口,这样可以不需要层 3 设备.当你在 switches 之间使用了
trunk link,多个VLAN的信息将从这个连接上通过;如果在你switches之间没有使用trunk link
而使用一般的连接,那么只有VLAN1的信息通过这个连接被互相传递.VLAN1默认作为管理
VLAN
 
Frame Tagging
 
frame tagging:帧的鉴别方法.当帧到达每个switch,首先先检查VLAN ID,然后决定如何对帧进
行处理.当帧到达和 VLAN ID 所匹配的 access link 的时候,switch 移去 VLAN标识符
 
VLAN Identification Method
 
VLAN 标识符:在交换机的 trunk link上,可以通过对数据帧附加 VLAN 信息,构建跨越多台交
换机的 VLAN.附加 VLAN 信息的方法,最具有代表性的有:
1.Inter-Switch Link(ISL):属于 Cisco 私有,只能在快速和千兆以太网连接中使用,ISL 路由可以
使用在 switch 的断端口,router的接口和服务器接口卡等
2.IEEE 802.1Q:俗称 dot 1 Q.由 IEEE 创建,所以在 Cisco 和非Cisco 设备之间,就不能使用 ISL
必须使用802.1Q.802.1Q所附加的VLAN识别信息,位于数据帧中的源MAC地址与类型字段
之间.基于 IEEE802.1Q附加的 VLAN 信息,就像在传递物品时附加的标签
当然 ISL 和 802.1Q的主要目的是提供 VLAN 间通信
 
Inter-Switch Link(ISL) Protocol
 
ISL:ISL 运作在层 2,ISL 是 1 种外部标签处理过程,所以原始的数据帧不被改变,ISL 在数据帧
头部加上 26 字节长的 ISL 头部信息,在数据帧尾部加上 4 字节的 FCS 字段进行 CRC 运算,
所以只有支持 ISL 的设备才能对它进行读取,最大程度 1522 字节.当帧被传送到 access link
时,ISL 封装信息将被移去
 
使用 trunk link 在多个 VLAN中行走,比使用 router连接的好处是:减少延时间
 
VLAN Trunking Protocol(VTP)
 
VTP 也是 Cisco 创建的,但是现在已经不为 Cisco 所私有.VTP 的主要目的是在 1 个交换性的
环境中管理所有配置好的 VLAN 使所有的 VLAN 保持一致性 VTP 允许增加,删除和重命名
VLAN,然后这些修改后的信息传播到整个 VTP域里的所有 switches 上
 
VTP的一些优点:
1.保持 VLAN 信息的连续性
2.精确跟踪和监视 VLAN
3.动态报告增加了的 VLAN 信息给 VTP域中所有 switch
4.可以使用即插即用(plug-and-play)的方法增加 VLAN
5.可以在混合型网络中进行 trunk link,比如以太网到 ATM LANE,FDDI等
 
在你使用 VTP 管理 VLAN 之前,必须先创建个 VTP 服务器(VTP server),所有要共享 VLAN
信息的服务器必须使用相同的域名.而且,假如你把某个 switch 和其他的 switch 配置在 1 个
VTP域里,这个 switch 就只能和这个 VTP域里的 switch 共享 VLAN 信息.其实,如果你只有 1
个 VLAN,就不需要使用 VTP 了.VTP 信息通过 trunk 端口进行发送和接收.可以给 VTP 配置
密码,但是要记住的是,所有的 switch必须配置相同的密码
 switch 通告 VTP 管理域信息,加上版本号和已知 VLAN 配置参数信息.还有种叫做透明 VTP
模式(transparent VTP mode),在这种模式里,你可以给 switch 配置成通过 trunk 端口转发VTP
信息,但是不接受 VTP更新信息来更新它自己的 VTP数据库
 
switch通过VTP通告检测到增加的VLAN,然后把新增加的VLAN和已有的联结在一起共享
信息.新的更新信息在之前的版本号上加 1
 
VTP Modes of Operation
 
在 VTP域里操作的 3 种模式:
1.服务器模式(server mode):所有 Catalyst switches的默认设置,1个 VTP域里必须至少要有 1
个服务器用来传播 VLAN 信息,对 VTP 信息的改变必须在服务器模式下操作.配置保存在
NVRAM 里
2.客户机模式(client mode):在这种模式下,switches 从 VTP 服务器接受信息,而且它们也发送
和接收更新,但是它们不能做任何改变.在VTP服务器通知客户switches说增加了新的VLAN
之前,你不能在客户 switch 的端口上增加新的 VLAN.配置不保存在 NVRAM 里
3.透明模式(transparent mode):该模式下的 switch 不能增加和删除 VLAN,因为它们保持的有
自己的数据库,不和其他的共享.配置保存在 NVRAM 里
 
VTP Pruning
 
VTP pruning:减少广播,组播,单播,保留带宽.VTP pruning 只在 trunk link 上发送广播.默认情
况,VTP pruning在所有的switches上是没有启用的.当你在VTP服务器上启用了VTP pruning,
整个VTP域就启用了VTP pruning,默认只能在VLAN2到VLAN1005,VLAN1是管理VLAN
 
Routing between VLANs
 
可以使用支持 ISL 路由的 router 来连接 VLAN,支持 ISL 路由的最低型号是 2600 系
列,1600,1700和2500系列都不支持.如下图,就是router和每个VLAN之间的关联,每个router
的接口都插入1个access link,这个同时也说明了router的每个接口的IP地址都是每个VLAN
的默认网关:
 
假如你有太多的VLAN,数量超过了router接口数量,明显上面的方法就不适用了.你可以使用
Cisco 的 3 层 switch Cisco3550,或者使用 router 的快速以太网接口来做 ISL 或者 802.1Q 的
trunk link,这样的方法叫做单臂路由(router on a stick).如下图: 
 
Configuring VLANs
 
创建 VLAN:
1900 下,使用vlan [vlan#] name [name] [vlan#]命令,  如下:
>en
#config t
(config)#hostname 1900
1900(config)#vlan 2 name sales
1900(config)#vlan 3 name marketing
1900(config)#vlan 4 name mis
1900(config)#exit
验证,使用show vlan命令,记住在你没给VLAN分配端口之前,之前做的VLAN是不会起作用
的.而且所有的端口默认是处在 VLAN1 的,VLAN1 是管理 VLAN.如下:
1900#sh vlan
VLAN Name                               Status             Ports
----------------------------------------------------------------------------
1    default                                 Enable             1-12, AUI, A, B
2    sales                                    Enable
3    marketing                              Enable
(略)
在2950下创建VLAN,在特权模式下使用vlan database命令,创建命令和1900下的类似,注意
结尾使用 apply命令.如下:
2950#vlan database
2950(vlan)#vlan 2 name Marketing
VLAN 2 modified:
     Name: Marketing
2950(vlan)#vlan 3 name Accounting
VLAN 3 added:
     Name: Accounting
2950(vlan)#apply
APPLY complete
2950(vlan)#^C
2950#
使用 show vlan 或者 show vlan brief 命令验证下:
2950#sh vlan brief
VLAN Name                               Status             Ports
----------------------------------------------------------------------------
1    default                                 active             Fa0/1...Fa0/12 2   Marketing                        active
3   Accounting                       active
(略)
 
Assigning Switch Ports to VLANs
 
创建了VLAN,接下来要做的就是给VLAN分配端口.1900下,使用vlan-membership命令1次
只能分配 1个,可以 static 或 dynamic 作为参数,如下:
1900(config)#int e0/2
1900(config-if)#vlan-membership static 2
1900(config)#int e0/4
1900(config-if)#vlan-membership static 3
1900(config)#int e0/5
1900(config-if)#vlan-membership static 4
1900(config-if)#exit
1900(config)#exit
1900#
验证,如下:
1900#sh vlan
VLAN Name                     Status         Ports
----------------------------------------------------------------------------
1   default                      Enable         1-12, AUI, A, B
2   sales                        Enable         2
3   marketing                    Enable         4
(略)
2950 下的配置,使用 switchport access vlan [vlan#]命令,如下:
2950(config-if)#int f0/2
2950(config-if)#switchport access vlan 2
2950(config-if)#int f0/3
2950(config-if)#switchport access vlan 3
2950(config-if)#int f0/4
2950(config-if)#switchport access vlan 4
2950(config-if)#exit
2950(config)#exit
2950#
验证配置信息,如下:
2950#sh vlan brief
VLAN Name                     Status         Ports
----------------------------------------------------------------------------
1   default                      active         Fa0/1 Fa0/5...Fa0/12
2   Marketing                   active         Fa0/2
3   Accounting                  active         Fa0/3
(略)
 
Configuring Trunk Ports
 
1900 只使用动态 ISL(DISL)封装方式,在快速以太网配置 trunk,在接口配置模式下使用 trunk
[参数]的命令,如下,将 26接口设置为 trunk 端口:
1900(config)#int f0/26
1900(config-if)#trunk ?
  auto        Set DISL state to AUTO
  desirable    Set DISL state to DESIRABLE
  nonegotiate  Set DISL state to NONEGOTIATE
  off         Set DISL state to OFF
  on         Set DISL state to ON 
1900(config-if)#trunk on 设置参数为on即接口将作为永久ISL的trunk端口,可以和和相连的设备协商,并且把连接转
换成 trunk link
2950 下在接口配置模式,使用 switchport 命令,如下:
2950(config)#int f0/12
2950(config-if)#switchport mode trunk
2950(config-if)#^Z
2950#
验证配置信息:
2950#sh run
(略)
!
interface FastEthernet0/12
  switchport mode trunk
    no ip address
!
(略)
 
Configuring Inter-VLAN Routing
 
使VLAN间互相通信,就必须使用router或者3层switch来连接.要在router的快速以太网接
口支持 ISL 和 802.1Q,要把接口划分成许多逻辑接口(非物理),1 个接口对应 1 个 VLAN.这些
接口就叫子接口(subinterfaces).还有要必须知道的是,默认你不可能在 1900 和 2950 之间做
trunk 连接,因为 1900 只支持 ISL 路由而 2950 只支持 802.1Q路由,2 种相互不兼容
 
给连接 1900的 trunk 端口配置,使用 encapsulation isl [vlan#]命令,如下:
2600Router(config)#int f0/0.1
2600Router(config-subif)#encapsulation isl [vlan#]
给连接 2950的这样配置,如下:
2600Router(config)#int f0/0.1
2600Router(config-subif)#encapsulation dot1q [vlan#]
 
Configuring VTP
 
默认下,1900和2950都被配置成VTP服务器模式,配置VTP,先配置VTP域名,还有密码,是否
pruning 等.
1900 下,在全局配置模式下使用 vtp 命令,如下:
1900(config)#vtp ?
  client       VTP client
  domain      Set VTP domain name
  password    Set VTP password
  pruning      VTP pruning
  server       VTP server
  transparent   VTP transparent
  trap         VTP trap
1900(config)#vtp server
1900(config)#vtp domain noco
1900(config)#vtp password noko
在特权模式下使用 show vtp 命令验证,如下:
1900#sh vtp
   VTP version: 1
   Configuration revision: 0
   Maximum VLANs supported locally: 1005
      Number of existing VLANs: 5
   VTP domain name: noco
   VTP password: noko VTP operating mode: Server
(略)
2950 如下:
2950(config)#vtp mode server
2950(config)#vtp domain noco
验证信息,如下:
2950#sh vtp ?
counters       VTP statistics
status          VTP domain status
2950#sh vtp status
(略)
 
Configuring Switching in Our Sample Internetwork
 
配置实例:
 
先配置 2950C,如下
2950C(config)#enable secret noko
2950C(config)#line con 0
2950C(config-line)#login
2950C(config-line)#password noco
2950C(config-line)#line vty 0 15
2950C(config-line)#login
2950C(config-line)#password noco
2950C(config-line)#banner motd #
2950C
#
2950C(config-line)#exit
2950C(config)#int vlan1
2950C(config-if)#ip address 172.16.10.2 255.255.255.0
2950C(config-if)#no shut
2950C(config-if)#exit
2950C(config)#up default-gateway 172.16.10.1
2950C(config)#^Z
2950C#copy run start
配置 2950B,如下:
2950B(config)#enable secret noko
2950B(config)#line con 0 2950B(config-line)#login
2950B(config-line)#password noco
2950B(config-line)#line vty 0 15
2950B(config-line)#login
2950B(config-line)#password noco
2950B(config-line)#banner motd #
2950B
#
2950B(config-line)#exit
2950B(config)#int vlan1
2950B(config-if)#ip address 172.16.10.3 255.255.255.0
2950B(config-if)#no shut
2950B(config-if)#exit
2950B(config)#up default-gateway 172.16.10.1
2950B(config)#^Z
2950B#copy run start
配置 trunk,2950B 如下:
2950B(config)#int f0/1
2950B(config-if)#switchport mode trunk
2950B(config-if)#int f0/4
2950B(config-if)#switchport mode trunk
2950B(config-if)#int f0/5
2950B(confgi-if)#switchport mode trunk
配置 trunk,2950C 如下:
2950C(config)#int f0/4
2950C(confgi-if)#switchport mode trunk
2950C(config-if)#int f0/5
2950C(config-if)#switchport mode trunk
验证 trunk 信息,使用 show interface trunk命令.如下:
2950B#sh int trunk
Port     Mode      Encapsulation     Status         Native vlan
Fa0/1    on         802.1q           trunking       1
Fa0/4    on         802.1q           trunking       1
Fa0/5    on         802.1q           trunking        1
(略)
之前我们已经对2950B和2950C做了基本配置和trunk端口的配置,接下来应该设置VTP和
创建 VLAN,并且进行验证.2950C 如下:
2950C(config)#vtp mode server
2950C(config)#vtp domain Cisco
2950C(config)#^Z
2950C#vlan database  
2950C(vlan)#vlan 2 name sales
2950C(vlan)#vlan 3 name marketing
2950C(vlan)#apply
2950C(vlan)#^C
2950C#sh vlan brief
(略)
接下来分配端口,把 Fa0/2 分配给 VLAN2,Fa0/3 分配给 VLAN3,默认所有的端口都处在
VLAN1 下,配置如下:
2950C(config)#int fa0/2
2950C(config-if)#switchport access vlan2
2950C(config)#int fa0/3
2950C(config-if)#switchport access vlan3
验证信息,注意 VLAN1 里的 Ports 栏,如下:
2950C#sh vlan brief
VLAN Name                     Status         Ports ----------------------------------------------------------------------------
1   default                      active         Fa0/1 Fa0/5...Fa0/10
2   sales                        active         Fa0/2
3   marketing                    active         Fa0/3
配置 2950B,把它设置成客户模式,2950B 从2950C接收 VLAN 信息,如下:
2950B(config)#vtp mode client
2950B(config)#vtp domain Cisco
2950B(config)#^Z
验证,注意 2950B 已经从 2950C 知道了VLAN 的信息,如下:
2950B#sh vlan brief
VLAN Name                     Status         Ports
----------------------------------------------------------------------------
1   default                      active         Fa0/1...Fa0/12
2   sales                        active         
3   marketing                    active        
但是仍然要给 2950B 分配端口,如下:
2950B(config)#int fa0/2
2950B(config-if)#switchport access vlan2
2950B(config)#int fa0/3
2950B(config-if)#switchport access vlan3
验证信息,如下:
VLAN Name                     Status         Ports
----------------------------------------------------------------------------
1   default                      active         Fa0/1 Fa0/5...Fa0/12
2   sales                        active         Fa0/2
3   marketing                    active         Fa0/3
到现在,2950C 和 2950B 的配置就算是完成了,经过验证,我们也没发现什么问题,接下来该配
置什么呢?当然是配置 VLAN 间的通信,根据上面的拓扑图,可以知道需要在 RouterB 上进行
配置,如下:
RouterB(config)#hostname Trunkrouter
Trunkrouter(config)#int f0/0
Trunkrouter(config-if)#no ip address
Trunkrouter(config-if)#no shut
创建子接口,并定义封装类型,如下:
Trunkrouter(config-if)#int f0/0.1
Trunkrouter(config-subif)#encapsulation dot1q 1
Trunkrouter(config-subif)#ip address 172.16.10.1 255.255.255.0
Trunkrouter(config-if)#int f0/0.2
Trunkrouter(config-subif)#encapsulation dot1q 2
Trunkrouter(config-subif)#ip address 172.16.20.1 255.255.255.0
Trunkrouter(config-if)#int f0/0.3
Trunkrouter(config-subif)#encapsulation dot1q 3
Trunkrouter(config-subif)#ip address 172.16.30.1 255.255.255.0
Trunkrouter(config-if)#exit
创建子接口,每个接口对应 1 个 VLAN.注意,如果你试图在第一个子接口分配 IP 地址,将收到
错误信息,除非你先定义了封装类型,如下:
Trunkrouter(config-if)#int f0/0.1
Trunkrouter(config-subif)#ip address 172.16.10.1 255.255.255.0
Configuring IP routing on a LAN subinterface is only allowed if that subinterface is already
configured as part of an IEEE 802.10, IEEE 802.1Q, or ISL VLAN.
验证配置信息,如下:
Trunkrouter#sh run
(略)