实验目的
        理解STP的工作原理:阻断一些交换机接口,构建一棵没有环路的转发树。STP利用BPDU(Bridge Protocol Data Unit)和其他交换机进行通信,从而确定那个交换机该阻断那个接口。
        掌握STP树的控制
        利用PVST进行负载平衡
实验拓扑
图中,S1和S2模拟为核心层的交换机,而S3为接入层的交换机。S1和S2实际上是三层交换机,这里并不利用其三层功能,所以他们采用二层交换机的图标。
实验步骤
       在网络中配置2个VLAN,不同VLAN的STP具有不同的跟桥,实现负载平衡。
步骤1. 利用VTP在交换机上创建VLAN 2,在S1和S2之间的链路配置Trunk
sw1#vlan database
sw1(vlan)#vtp domain cisco
Changing VTP domain name from NULL to cisco
sw1(vlan)#vlan 2
VLAN 2 added:
    Name: VLAN0002
sw1(vlan)#exit
sw1(config-if)#interface f0/12
sw1(config-if)#switchport trunk encapsulation dot1q
sw1(config-if)#switchport mode trunk
步骤2  检查初始的STP树
sw1# show spanning-tree
 VLAN1 is executing the ieee compatible Spanning Tree protocol  //表明运行的STP协议是IEEE的802.1D
  Bridge Identifier has priority 32768, //优先级是32768
  address ccdb.0858.0000                //MAC地址是ccdb.0858.0000
  Configured hello time 2, max age 20, forward delay 15
  We are the root of the spanning tree
  Topology change flag set, detected flag set
Port 10 (FastEthernet0/9) of VLAN1 is forwarding
  Port path cost 19, Port priority 128, Port Identifier 128.10.
  Designated root has priority 32768, address ccdb.0858.0000
  Designated bridge has priority 32768, address ccdb.0858.0000
  Designated port id is 128.10, designated path cost 0
  Timers: message age 0, forward delay 0, hold 0
  Number of transitions to forwarding state: 1
  BPDU: sent 2312, received 7
Port 13 (FastEthernet0/12) of VLAN1 is forwarding
  Port path cost 19, Port priority 128, Port Identifier 128.13.
  Designated root has priority 32768, address ccdb.0858.0000
  Designated bridge has priority 32768, address ccdb.0858.0000
  Designated port id is 128.13, designated path cost 0
  Timers: message age 0, forward delay 0, hold 0
  Number of transitions to forwarding state: 1
  BPDU: sent 2723, received 234
sw2#show spanning-tree
 VLAN1 is executing the ieee compatible Spanning Tree protocol
  Bridge Identifier has priority 32768, address ccdc.0684.0000
  Configured hello time 2, max age 20, forward delay 15
  Current root has priority 32768, address ccdb.0858.0000
  Root port is 13 (FastEthernet0/12), cost of root path is 19
  Topology change flag set, detected flag not set
Port 13 (FastEthernet0/12) of VLAN1 is forwarding
   Port path cost 19, Port priority 128, Port Identifier 128.13.
   Designated root has priority 32768, address ccdb.0858.0000
   Designated bridge has priority 32768, address ccdb.0858.0000
   Designated port id is 128.13, designated path cost 0
   Timers: message age 0, forward delay 0, hold 0
   Number of transitions to forwarding state: 1
   BPDU: sent 179, received 177
 Port 14 (FastEthernet0/13) of VLAN1 is forwarding
   Port path cost 19, Port priority 128, Port Identifier 128.14.
   Designated root has priority 32768, address ccdb.0858.0000
   Designated bridge has priority 32768, address ccdc.0684.0000
   Designated port id is 128.14, designated path cost 19
   Timers: message age 0, forward delay 0, hold 0
   Number of transitions to forwarding state: 1
   BPDU: sent 384, received 65
sw3#show spanning-tree
 VLAN1 is executing the ieee compatible Spanning Tree protocol
  Bridge Identifier has priority 32768, address ccdd.0698.0000
  Configured hello time 2, max age 20, forward delay 15
  Current root has priority 32768, address ccdb.0858.0000
  Root port is 10 (FastEthernet0/9), cost of root path is 19
  Topology change flag set, detected flag not set
Port 10 (FastEthernet0/9) of VLAN1 is forwarding
   Port path cost 19, Port priority 128, Port Identifier 128.10.
   Designated root has priority 32768, address ccdb.0858.0000
   Designated bridge has priority 32768, address ccdb.0858.0000
   Designated port id is 128.10, designated path cost 0
   Timers: message age 1, forward delay 0, hold 0
   Number of transitions to forwarding state: 1
   BPDU: sent 3, received 625
 Port 14 (FastEthernet0/13) of VLAN1 is blocking
   Port path cost 19, Port priority 128, Port Identifier 128.14.
   Designated root has priority 32768, address ccdb.0858.0000
   Designated bridge has priority 32768, address ccdc.0684.0000
   Designated port id is 128.14, designated path cost 19
   Timers: message age 16, forward delay 0, hold 0
   Number of transitions to forwarding state: 0
   BPDU: sent 106, received 211
//以上显示VLAN1的STP树的跟桥信息,通过跟桥的MAC地址可以确定S1是跟桥。这是因为S1是较早的交换机,具有较低的MAC地址。由于S1是一台地段的交换机,成为跟桥显然不合理的。
Port 10 (FastEthernet0/9) of VLAN1 is forwarding
Port 13 (FastEthernet0/12) of VLAN1 is forwarding
//以上显示交换机各个接口的状态,F0/9,和F0/12都是转发状态。
VLAN2 is executing the ieee compatible Spanning Tree protocol
 Bridge Identifier has priority 32768, address ccdb.0858.0001
 Configured hello time 2, max age 20, forward delay 15
 We are the root of the spanning tree
 Topology change flag set, detected flag set
 Number of topology changes 4 last change occurred 00:00:38 ago
         from FastEthernet0/12
 Times:  hold 1, topology change 35, notification 2
         hello 2, max age 20, forward delay 15
 Timers: hello 0, topology change 22, notification 0, aging 0
Port 10 (FastEthernet0/9) of VLAN2 is forwarding
Port 13 (FastEthernet0/12) of VLAN2 is forwarding
//以上是VLAN2的STP树情况,VLAN 2的STP树和VLAN 1的类似。默认时,Cisco交换机会为每个VLAN都生成一个单独的STP树,称为PVST(Per VLAN Spanning Tree)
分析: STP为什么会是目前这种情况。三个交换机的默认优先级都是32768,而S1的MAC较低,所以成为了跟桥,则S1上的F0/9和F0/12是指定端口,处于Forword状态。S2有连个接口可以到达S1,一个接口是F0/12,到达S1的Cost为19,另一个接口是F0/13,到达S1的Cost为19+19=38,因此F0/12是根端口,处于Forword状态。同样S3上F0/9是根端口。处于Forword状态。在S2和S3之间的链路上,要选举一个指定端口。根据选举的要素,跟桥的ID是一样的,不能决出胜负;到达跟桥的COST值也是一样的。不能决出胜负;但是发送桥ID不一样,S2的MAC地址低,S3的MAC地址高。S2获胜,所以S2的F0/13是指定端口,处于Forward状态,S3的F0/13就处于Block状态了。
步骤3 控制S3为VLAN 2跟桥,S2为VLAN1跟桥
sw3(config)#spanning-tree vlan 2 priority 4096
sw2(config)#spanning-tree vlan 1 priority 4096
//对于VLAN 1来说,S3的优先级为4096.而S1和S2保持默认值32768,这样S3就成为ulVLAN 1的跟桥。同样我们控制S2成为了VLAN 2的跟桥。优先级统称要是4096的倍数。
VLAN2 is executing the ieee compatible Spanning Tree protocol
  Bridge Identifier has priority 4096, address ccdd.0698.0001
  Configured hello time 2, max age 20, forward delay 15
  We are the root of the spanning tree
//S3成为了VLAN2 的跟桥了
change flag not set, detected flag not  set
  Number of topology changes 19 last change occurred 00:02:55 ago
  Times:  hold 1, topology change 35, notification 2
          hello 2, max age 20, forward delay 15
  Timers: hello 1, topology change 0, notification 0, aging 0
 Port 10 (FastEthernet0/9) of VLAN2 is forwarding
   Port path cost 19, Port priority 128, Port Identifier 128.10.
   Designated root has priority 4096, address ccdd.0698.0001
   Designated bridge has priority 4096, address ccdd.0698.0001
   Designated port id is 128.10, designated path cost 0
   Timers: message age 0, forward delay 0, hold 0
   Number of transitions to forwarding state: 1
   BPDU: sent 106, received 1785
 Port 14 (FastEthernet0/13) of VLAN2 is forwarding
   Port path cost 19, Port priority 128, Port Identifier 128.14.
   Designated root has priority 4096, address ccdd.0698.0001
   Designated bridge has priority 4096, address ccdd.0698.0001
   Designated port id is 128.14, designated path cost 0
   Timers: message age 0, forward delay 0, hold 0
   Number of transitions to forwarding state: 1
   BPDU: sent 144, received 1334
//对于VLAN 2来说,F0/9和F0/13是指定端口,都处于转发状态了。
sw2#show spanning-tree
 VLAN1 is executing the ieee compatible Spanning Tree protocol
  Bridge Identifier has priority 4096, address ccdc.0684.0000
  Configured hello time 2, max age 20, forward delay 15
  We are the root of the spanning tree //S2成了VLAN 1的跟桥。
ge flag not set, detected flag not set
  Number of topology changes 2 last change occurred 01:20:47 ago
          from FastEthernet0/12
  Times:  hold 1, topology change 35, notification 2
          hello 2, max age 20, forward delay 15
  Timers: hello 0, topology change 0, notification 0, aging 0
步骤4  控制指定端口
            在步骤3中可以看到VLAN 1 ,S2成为了跟桥,然而S1和S3之间的链路上,却是交换机S1F0/9在转发数据,原因在于S1和S3在竞争指定端口时,由于S1的MAC较低而活胜了,VLAN2的情况类似。
现在控制指定端口,可以通过改变优先级实现,如下
sw3(config)#spanning-tree vlan 1 priority 8192
sw2(config)#spanning-tree vlan 2 priority 8192
//对于VLAN 1来说,S3的优先级为8192,比S2的4096低,不至于成为跟桥,但是比S1的32768低,所以在竞争指定端口时会获胜,VLAN2的情况类似。
sw1#show spanning-tree brief
VLAN1
Interface                                   Designated
Name                 Port ID Prio Cost  Sts Cost  Bridge ID            Port ID
-------------------- ------- ---- ----- --- ----- -------------------- -------
FastEthernet0/9      128.10   128    19 BLK    19  8192 ccdd.0698.0000 128.10
FastEthernet0/12     128.13   128    19 FWD     0  4096 ccdc.0684.0000 128.13
//S1上,对于VLAN1,S1的F0/9处于转发状态,而F0/12处于阻断状态
VLAN2
Interface                                   Designated
Name                 Port ID Prio Cost  Sts Cost  Bridge ID            Port ID
-------------------- ------- ---- ----- --- ----- -------------------- -------
FastEthernet0/9      128.10   128    19 FWD     0  4096 ccdd.0698.0001 128.10
FastEthernet0/12     128.13   128    19 BLK    19  8192 ccdc.0684.0001 128.13
//S1上,对于VLAN1,S1的F0/9处于阻断状态,而F0/12处于转发状态
这样就起到了负载均衡的作用