一、目的
(1) 理解 STP 的工作原理
(2) 掌握 STP 树的控制
(3) 利用 PVST 进行负载平衡
二、拓扑

三、步骤
我们要在网络中配置 2 个 VLAN,不同 VLAN 的 STP 具有不同的根桥,实现负载平衡。
(1) 步骤 1:利用 VTP 在交换机上创建 VLAN2,在 S1 和 S2 之间的链路配置 Trunk
S1(config)# vtp domain VTP-TEST
Changing VTP domain name from NULL to VTP-TEST
S1(config) #vlan 2
//在 S1 上配置 VTP 的域名,并创建 VLAN 2。由于默认时 S2 和 S3 的 VTP 域名为空,它们将
自动学习到 S1 的 VTP 域名,同时 S2、S3 也将自动学习到 VLAN 2,请确认是否成功。
S1(config)#i nt f0/14
S1(config-if)# shutdown
//关闭该接口,以免影响我们的实验
S1(config)#i nt f0/13
S1(config-if)# switchport trunk encapsulation dot1q
S1(config-if)# switchport mode trunk
//S1 的 f0/13 改为 negotiate 后,由于默认时 S2 的 f0/13 为 auto 模式,S1 和 S2 将自动协
商成功 Trunk。而默认时 S3 的以太网接口就是 desirable 模式,所以 S3 和 S1、S2 的链路
也自动协商成功 Trunk。请确认三条链路的 Trunk 是否成功。
(2) 步骤 2:检查初始的 STP 树
S1# show spanning-tree
VLAN0001
Spanning tree enabled protocol ieee
//以上表明运行的 STP 协议是 IEEE 的 802.1D
Root ID Priority 32768
Address 0009.b7a4.b181
Root ID Priority 32768
Address 0009.b7a4.b181
Cost 19
Port 17 (FastEthernet0/15)
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
//以上显示 VLAN 1 的 STP 树的根桥信息,通过根桥的 MAC 地址可以确定 S3 是根桥。这是
因为 S3 是较早的交换机,具有较低的 MAC 地址。由于 S3 是一台低端的交换机,成为根桥
显然是不合理的。
//以上显示 VLAN 1 的 STP 树的根桥信息,通过根桥的 MAC 地址可以确定 S3 是根桥。这是
因为 S3 是较早的交换机,具有较低的 MAC 地址。由于 S3 是一台低端的交换机,成为根桥
显然是不合理的。
Bridge ID Priority 32769 (priority 32768 sys-id-ext 1)
Address 0018.ba11.f500
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Aging Time 300
//以上显示该交换机的桥 ID
Interface Role Sts Cost Prio.Nbr Type
---------------- ---- --- --------- -------- --------------------------------
Fa0/13 Altn BLK 19 128.15 P2p
Fa0/15 Root FWD 19 128.17 P2p
Fa0/13 Altn BLK 19 128.15 P2p
Fa0/15 Root FWD 19 128.17 P2p
//以上显示该交换机各个接口的状态,f0/13 为阻断状态,f0/15 为根口
VLAN0002
Spanning tree enabled protocol ieee
Root ID Priority 32768
Address 0009.b7a4.b182
Cost 19
Port 17 (FastEthernet0/15)
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Bridge ID Priority 32770 (priority 32768 sys-id-ext 2)
Address 0018.ba11.f500
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Aging Time 300
Interface Role Sts Cost Prio.Nbr Type
---------------- ---- --- --------- -------- --------------------------------
Fa0/13 Altn BLK 19 128.15 P2p
Fa0/15 Root FWD 19 128.17 P2p
//以上是 VLAN 2 的 STP 树情况,VLAN 2 的 STP 树和 VLAN 1 的类似。默认时,Cisco 交换机
会为每个 VLAN 都生成一个单独的 STP 树,称为 PVST(Per VLAN Spanning Tree)。
【技术要点】需要仔细分析为什么 STP 会是目前这种情况。三个交换机的默认优先级都是
32768,而 S3 的 MAC 较低,所以成为了根桥,则 S3 上的 f0/1 和 f0/2 是指定口,处于 Forword
状态。S1 有两个接口可以到达 S3,一个接口是 f0/13,到达 S3 的 Cost 为 19+19=38,另
一个接口是 f0/15,到达 S1 的 Cost 为 19,因此 f0/15 是根口,处于 Forword 状态。同样
S2 上,f0/15 也是根口,处于 Forword 状态。在 S1 和 S2 之间的链路上,要选举出一个指定
口。根据选举的要素,根桥的 ID 是一样的,不能决出胜负;到达根桥的 Cost 值也是一样的,
都为 19,不能决出胜负;但是发送者桥 ID 不一样,S1 的 MAC 地址高,S2 的 MAC 地址低,
S2 获胜,所以 S2 的 f0/13 是指定口,处于 Forward 状态,S1 的 f0/13 就处于 Block 状态了。
(3) 步骤 3:控制 S1 为 VLAN1 的根桥,S2 为 VLAN2 的根桥
S1(config)# spanning-tree vlan 1 priority 4096
S2(config)# spanning-tree vlan 2 priority 4096
//对于 VLAN 1 来说,S1 的优先级为 4096,而 S2 和 S3 保持默认值 32768,这样 S1 就成为
了 VLAN 1 的根桥。同样我们控制 S2 成为了 VLAN 2 的根桥。优先级通常要是 4096 的倍数。
S1# show spanning-tree
VLAN0001
Spanning tree enabled protocol ieee
Root ID Priority 4097
Address 0018.ba11.f500
This bridge is the root
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
//S1 成为了 VLAN 1 的根桥了
Bridge ID Priority 4097 (priority 4096 sys-id-ext 1)
Address 0018.ba11.f500
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Aging Time 15
Interface Role Sts Cost Prio.Nbr Type
---------------- ---- --- --------- -------- --------------------------------
Fa0/13 Desg FWD 19 128.15 P2p
Fa0/15 Desg FWD 19 128.17 P2p
Fa0/13 Desg FWD 19 128.15 P2p
Fa0/15 Desg FWD 19 128.17 P2p
//对于 VLAN 1 来说,f0/13 和 f0/15 是指定口,都处于转发状态了
VLAN0002
Spanning tree enabled protocol ieee
Root ID Priority 4098
Address 0018.ba11.eb80
Root ID Priority 4098
Address 0018.ba11.eb80
Cost 19
Port 15 (FastEthernet0/13)
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
//S2 成为了 VLAN 2 的根桥了
Bridge ID Priority 32770 (priority 32768 sys-id-ext 2)
Address 0018.ba11.f500
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Aging Time 15
Interface Role Sts Cost Prio.Nbr Type
---------------- ---- --- --------- -------- --------------------------------
Fa0/13 Root FWD 19 128.15 P2p
Fa0/15 Altn BLK 19 128.17 P2p
Fa0/13 Root FWD 19 128.15 P2p
Fa0/15 Altn BLK 19 128.17 P2p
//对于 VLAN 2 来说,f0/13 是根口,处于转发状态,而 f0/15 却是阻断状态
//对于 VLAN 2 来说,f0/13 是根口,处于转发状态,而 f0/15 却是阻断状态
S3#s how spanning-tree brief
VLAN1
Spanning tree enabled protocol ieee
Root ID Priority 4097
Address 0018.ba11.f500
Cost 19
Port 1 (FastEthernet0/1)
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Bridge ID Priority 32768
Address 0009.b7a4.b181
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Aging Time 300
Interface Designated
Name Port ID Prio Cost Sts Cost Bridge ID Port ID
-------------------- ------- ---- ----- --- ----- -------------------- -------
FastEthernet0/1 128.1 128 19 FWD 0 4097 0018.ba11.f500 128.17
FastEthernet0/2 128.2 128 19 FWD 19 32768 0009.b7a4.b181 128.2
FastEthernet0/1 128.1 128 19 FWD 0 4097 0018.ba11.f500 128.17
FastEthernet0/2 128.2 128 19 FWD 19 32768 0009.b7a4.b181 128.2
//在 S3 上,对于 VLAN1,S3 的 f0/1 和 f0/2 都处于转发状态。
VLAN2
Spanning tree enabled protocol ieee
Root ID Priority 4098
Address 0018.ba11.eb80
Cost 19
Port 2 (FastEthernet0/2)
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Bridge ID Priority 32768
Address 0009.b7a4.b182
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Aging Time 300
Interface Designated
Name Port ID Prio Cost Sts Cost Bridge ID Port ID
-------------------- ------- ---- ----- --- ----- -------------------- -------
FastEthernet0/1 128.1 128 19 FWD 19 32768 0009.b7a4.b182 128.1
FastEthernet0/2 128.2 128 19 FWD 0 4098 0018.ba11.eb80 128.17
FastEthernet0/1 128.1 128 19 FWD 19 32768 0009.b7a4.b182 128.1
FastEthernet0/2 128.2 128 19 FWD 0 4098 0018.ba11.eb80 128.17
//S3 上,对于 VLAN2,S3 的 f0/1 和 f0/2 也都处于转发状态。
(4) 步骤 4:控制指定口
在步骤 3 中可以看到对于 VLAN 1,S1 成为了根桥,S1 的 f0/13 和 f0/15 处于转发状态;
S2 的 f0/13 是根口,也处于转发状态;S3 的 f0/1 是根口,也处于转发状态;然而 S2 和 S3
之间的链路上,却是低端交换机 S3 的 f0/2 在转发数据,原因在于 S2 和 S3 在竞争指定口时,
由于 S3 的 MAC 较低而获胜了,这是不合理的。VLAN 2 的情况类似。
我们要控制指定口,这可以通过改变优先级实现,如下:
S2(config)# spanning-tree vlan 1 priority 8192
S1(config)# spanning-tree vlan 2 priority 8192
//对于 VLAN 1 来说,S2 的优先级为 8192,比 S1 的 4096 低,不至于成为根桥,但是比 S3
的 32768 低,所以在竞争指定口时会获胜。VLAN 2 的情况类似。
S3#show spanning-tree brief
VLAN1
Interface Designated
Name Port ID Prio Cost Sts Cost Bridge ID Port ID
-------------------- ------- ---- ----- --- ----- -------------------- -------
FastEthernet0/1 128.1 128 19 FWD 0 4097 0018.ba11.f500 128.17
FastEthernet0/2 128.2 128 19 BLK 19 8193 0018.ba11.eb80 128.17
FastEthernet0/1 128.1 128 19 FWD 0 4097 0018.ba11.f500 128.17
FastEthernet0/2 128.2 128 19 BLK 19 8193 0018.ba11.eb80 128.17
//S3 上,对于 VLAN1,S3 的 f0/1 处于转发状态,而 f0/2 处于阻断状态。
VLAN2
Interface Designated
Name Port ID Prio Cost Sts Cost Bridge ID Port ID
-------------------- ------- ---- ----- --- ----- -------------------- -------
FastEthernet0/1 128.1 128 19 BLK 19 8194 0018.ba11.f500 128.17
FastEthernet0/2 128.2 128 19 FWD 0 4098 0018.ba11.eb80 128.17
FastEthernet0/1 128.1 128 19 BLK 19 8194 0018.ba11.f500 128.17
FastEthernet0/2 128.2 128 19 FWD 0 4098 0018.ba11.eb80 128.17
// S3 上,对于 VLAN 2,S3 的 f0/1 处于阻断状态,而 f0/2 处于转发状态,这样起到了负
载平衡的作用。
以下是华为数通路由交换方向完整技术分享,欢迎对华为网络技术感兴趣的小伙伴们订阅。
【可在专栏中进行查看订阅】
华为新版HCIP数通路由交换
华为新版HCIE数通路由交换