STP生成树协议

Title
STP生成树协议
Date
May 14, 2023
 
notion image

STP选举过程:

1
选举根桥(Root Bridge)
2
在每个非根桥上选举一个根端口(Root Port)
3
选举指定端口(Designated Port)
4
阻塞剩余端口(Non-Designated Port)

STP选举依据:

Bridge ID:
2字节(16bit)
6字节(48bi't)
Bridge ID
端口优先级(Priority)默认值为32768(16bit的中间十进制数值),步长为4096(16bit最高4bit),后12为vlan id
交换机mac地址
路径开销(通过链路速度决定):
COST表:
Bandwidth(Mbps)
4
10
16
45
100
155
1000
10000
cost值
250
100
62
39
19
14
4
2
Port ID:
端口优先级
端口编号
Port ID
8bit(默认值128
8bit(不可更改)
1.选举根桥(Root Bridge):
比较交换机的Bridge ID,先比较优先级,数值小的胜出,如相同则比较MAC地址,数值小的胜出,作为根桥
2.指定非根网桥上的根端口(Root Port):
比较去往根桥路径开销,数值越小越好,如相同,则比较发送方的Bridge ID,数值小的胜出,如相同则比较Port ID 小的胜出
3.选举指定端口(Designated Port):
比较去往根桥路径开销,数值越小越好,如相同,则比较发送方(发送BPDU的一方,既链路的另一方)的Bridge ID,数值小的胜出,如相同则比较Port ID 小的胜出
4.阻塞剩余端口:
如果既不是Root Port,也不是Designated Port,那么就会被阻塞,不同的stp协议名称不同,PVST(Alternate Port),STP(Blocking Port)
选举原则:
  1. 比较Bridge ID,数值小的胜出(只用在选举Root Bridge时使用)
  1. 比较根路径开销,数值小的胜出
  1. 比较发送方的Bridge ID,数值小的胜出
  1. 比较Port ID,数值小的胜出
选举总结:
  • 根桥上的端口都是指定端口
  • 根端口的对端一定是指定端口
  • 每个非根交换机上只有一个根端口
  • 每条链路上必有一个指定端口

STP生成树版本:

 
 
STP实验:
notion image
notion image
SW0:
Switch#show spanning-tree VLAN0001 Spanning tree enabled protocol ieee Root ID Priority 32769 #优先级32768 + 1(vlan1) Address 0010.11A5.D287 #MAC地址 Cost 19 #根桥cost百兆线 Port 1(FastEthernet0/1) Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Bridge ID Priority 32769 (priority 32768 sys-id-ext 1) Address 0090.0C46.AD25 Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Aging Time 20 Interface Role Sts Cost Prio.Nbr Type ---------------- ---- --- --------- -------- -------------------------------- Fa0/1 Root FWD 19 128.1 P2p #f0/1 root port Fa0/2 Altn BLK 19 128.2 P2p #f0/2 ALternate port
SW1:
Switch#show spanning-tree VLAN0001 Spanning tree enabled protocol ieee Root ID Priority 32769 Address 0010.11A5.D287 This bridge is the root #switch1 为根桥 Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Bridge ID Priority 32769 (priority 32768 sys-id-ext 1) Address 0010.11A5.D287 Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Aging Time 20 Interface Role Sts Cost Prio.Nbr Type ---------------- ---- --- --------- -------- ------------------spanning-tree vlan 1 root primary-------------- Fa0/2 Desg FWD 19 128.2 P2p #f0/2为指定端口 Fa0/1 Desg FWD 19 128.1 P2p #f0/1为指定端口
改变SW0的优先级,这样SW1就可以作为根桥了,交换机的不同端口的角色也就随之改变:
SW0: Switch(config)#spanning-tree vlan 1 priority ? <0-61440> bridge priority in increments of 4096 #设置优先级为4096的倍数 Switch(config)#spanning-tree vlan 1 priority 4096 #设置SW0的优先级为4096比SW1小,这样SW1就作为根桥了
也可以把直接把SW0设置为根桥:
SW0: Switch(config)#spanning-tree vlan 1 root primary #设置这个交换机为这个生成树的根桥这个命令 可以将当前交换机的的默认优先级减去8192 32769 - 8192 = 24577 Switch(config)#spanning-tree vlan 1 root primary Switch(config)#do show span VLAN0001 Spanning tree enabled protocol ieee Root ID Priority 24577 #32769 - 8192 = 24577 Address 0090.0C46.AD25 This bridge is the root Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Bridge ID Priority 24577 (priority 24576 sys-id-ext 1) Address 0090.0C46.AD25 Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Aging Time 20 Interface Role Sts Cost Prio.Nbr Type ---------------- ---- --- --------- -------- -------------------------------- Fa0/1 Desg FWD 19 128.1 P2p Fa0/2 Desg LSN 19 128.2 P2p Switch(config)#spanning-tree vlan 1 root secondary #这个将默认优先级减去4096,这个命令重复执行只减去一次 可以将当前交换机的的默认优先级减去4096 32769 - 4096 = 28673 如果根桥的优先级不是默认值,则设置为根桥优先级减去4096,让非根桥成为根桥,建议手动设置优先级 Switch(config)#spanning-tree vlan 1 root secondary Switch(config)#do show span Switch(config)#do show span VLAN0001 Spanning tree enabled protocol ieee Root ID Priority 28673 #32769 - 4096 = 28673 Address 0090.0C46.AD25 This bridge is the root Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Bridge ID Priority 28673 (priority 28672 sys-id-ext 1) Address 0090.0C46.AD25 Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Aging Time 20 Interface Role Sts Cost Prio.Nbr Type ---------------- ---- --- --------- -------- -------------------------------- Fa0/1 Desg FWD 19 128.1 P2p Fa0/2 Desg FWD 19 128.2 P2p
设置完成后,SW0已经作为根桥了:
notion image
改变交换机SW0的f0/1的vlan 1 的COST值,就可以实现指定某个端口的角色,如将原来RP的f0/1改变成阻塞端口:
SW0: Switch(config)#int f0/1 Switch(config-if)#spanning-tree vlan 1 cost 40 Switch(config-if)#do show span VLAN0001 Spanning tree enabled protocol ieee Root ID Priority 32769 Address 0010.11A5.D287 Cost 19 Port 2(FastEthernet0/2) Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Bridge ID Priority 32769 (priority 32768 sys-id-ext 1) Address 0090.0C46.AD25 Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Aging Time 20 Interface Role Sts Cost Prio.Nbr Type ---------------- ---- --- --------- -------- -------------------------------- Fa0/1 Altn BLK 40 128.1 P2p Fa0/2 Root LSN 19 128.2 P2p
等该生成树收敛完成端口f0/1就会变成阻塞端口:
notion image
改变SW1上f0/1端口优先级,以达到f0/1作为阻塞端口,而f0/2作为RP:
Switch(config-if)#spanning-tree port-priority 64 #修改端口的优先级,改变步长为16,默认为128 改为240最高值,16为最低值,因为端口ID的最高四位为端口优先级 Switch(config-if)#do show span VLAN0001 Spanning tree enabled protocol ieee #协议为PVST协议 Root ID Priority 32769 Address 0010.11A5.D287 This bridge is the root Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Bridge ID Priority 32769 (priority 32768 sys-id-ext 1) Address 0010.11A5.D287 Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Aging Time 20 Interface Role Sts Cost Prio.Nbr Type ---------------- ---- --- --------- -------- -------------------------------- Fa0/2 Desg FWD 19 128.2 P2p Fa0/1 Desg FWD 19 240.1 P2p #端口的优先级为240 改变stp计时器,不建议修改,会造成网络不稳定,而且所有的交换机都要修改 Switch(config)#spanning-tree vlan 1 hello-time 4 #改变hello-time 为4s Switch(config)#spanning-tree vlan 1 max-age 26 #改变max-age为26s Switch(config)#spanning-tree vlan 1 forward-time 20 #改变forward状态改变时间为 20s 另一种改变stp计时器的方法,设定从网络一端到另一端需要经过多少个交换机,switch会按照这个距离去 设定计时器 Switch(config)#spanning-tree vlan 1 root primary diameter ? <2-7> Maximum number of bridges between any two end nodes #任意两个节点中的最大网桥数
notion image
 
STP版本历史:
协议
PVST+
RPVST+
MST
标准
Cisco
Cisco
802.1s 802.1Q
收敛速度
Slow
Rapid
Rapid
trunk协议支持
802.1Q 或者 ISL
802.1Q 或者 ISL
802.1Q 或者 ISL
IOS系统显示
ieee
rstp
mst
RSTP协议:
STP的后续版本,为了解决生成树的收敛时间过慢的问题,而开发出的后续协议版本,在传统的STP协议中,网络的架构发生改变的话,STP重新计算到收敛的时间需要30分钟以上,而在实际情况中,交换机的拓扑发生变化到STP重新收敛到网络重新变为可用状态的时间,最多可以到3分红以上,在现代网络中,这种收敛速度太慢,所以就有了RSTP和MST这样的为了缩短网络收敛时间而产生的STP协议的后续版本

RSTP的不同之处:

RSTP保留了Root Port和Designated Port的设计,选择方法与STP相同,另外加入了ALternate Port和Backup port,用来加快收敛时间
ALternate Port:
没有成为RP和DP的Port会成为ALternate Port,就是去到根桥的另外一条路,会阻塞端口,处于Blocking状态
Backup port:
在通一个广播域中的没有成为DP和RP的端口,只会出现在HUB上面,但是HUB在现代网络中已经几乎见不到了,也会阻塞端口,处于Blocking状态
端口状态:
STP协议端口状态
端口作用
RSTP端口状态
Forwarding
接收发送数据
Forwarding
Learning
构建MAC地址表
Learning
Listening
接收和发送BPDU,并参与stp选举
Discarding
Blcoking
仅接收BPDU
Discarding
Disabled
关闭端口
DIscarding
Link Type:
LinkType是在RSTP中新增的概念,RSTP会把Link非为Edge Port,Point to Point Non-edge Port以及Shared Non-edge Port三种。
Edge Port:
Edge Port 由Discarding直接编程forwarding,并不会产生环路或者发生拓扑变动而发送BPDU,从而Swithc不会清空MAC地址,一般接在主机这样的设备中,让主机的端口快速发送数据,Edge Port一般用在access端口:
Switch(config)#spanning-tree portfast ? #全局模式下设置postfast bpduguard Enable portfast bpdu guard on this switch default Enable portfast by default on all access ports #默认在所有端口上启用POSTfast
Point to Point:
两个端口工作在全双工的模式下
shared Point:
两个端口工作在半双工的模式下,出现这样的状况,端口可能产生了问题
SW1; Switch(config)#spanning-tree mode rapid-pvst #切换stp协议为rstp Switch(config)#do show span VLAN0001 Spanning tree enabled protocol rstp #显示协议为rstp Root ID Priority 32769 Address 0010.11A5.D287 Cost 19 Port 1(FastEthernet0/1) Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Bridge ID Priority 32769 (priority 32768 sys-id-ext 1) Address 0090.0C46.AD25 Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Aging Time 20 Interface Role Sts Cost Prio.Nbr Type ---------------- ---- --- --------- -------- -------------------------------- Fa0/1 Root LSN 19 128.1 P2p Fa0/2 Altn BLK 19 128.2 P2p Switch(config-if)#spanning-tree ? Switch(config-if)#spanning-tree portfast #更改端口为postfast模式 %Warning: portfast should only be enabled on ports connected to a single host. Connecting hubs, concentrators, switches, bridges, etc... to this interface when portfast is enabled, can cause temporary bridging loops. Use with CAUTION %Portfast has been configured on FastEthernet0/3 but will only have effect when the interface is in a non-trunking mode. Switch(config-if)#spanning-tree portfast ? disable Disable portfast for this interface #禁用portfast trunk Enable portfast on the interface even in trunk mode #在trunk模式运行portfast 如果在trunk模式中收到bpdu,则这个端口禁用postfast转为完全stp模式 Switch(config-if)#spanning-tree link-type shared #强制把point to point 改为shared模式,也就是半双工 Switch(config-if)#do show span VLAN0001 Spanning tree enabled protocol ieee Root ID Priority 32769 Address 0010.11A5.D287 Cost 19 Port 1(FastEthernet0/1) Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Bridge ID Priority 32769 (priority 32768 sys-id-ext 1) Address 0090.0C46.AD25 Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Aging Time 20 Interface Role Sts Cost Prio.Nbr Type ---------------- ---- --- --------- -------- -------------------------------- Fa0/1 Root FWD 19 128.1 P2p Fa0/2 Altn BLK 19 128.2 Shr #状态改为shared,如果不是手动改变的话,自动出现就要考虑端口是否有故障

RSTP负载均衡:

在SW0和SW1中创建vlan1和vlan2,在不同的vlan中配置不同的stp,这样不同的vlan走不同的链路,这样就通过vlan实现了负载均衡:
notion image
SW1:
Switch(config)#vlan 2 #在SW1中创建vlan2 Switch(config)#int rang f0/1-2 Switch(config-if-range)#switchport mode trunk #把f0/1和f0/2设置成trunk模式
SW0:
Switch(config)#vlan 2 #在SW0中创建vlan2 Switch(config)#int rang f0/1-2 Switch(config-if-range)#switchport mode trunk #把f0/1和f0/2设置成trunk模式 Switch(config)#int f0/2 Switch(config-if)#spanning-tree vlan 2 port-priority 64 #把vlan2中的f/2端口的优先级调高 让f0/2在vlan2 中的sw1的f0/2端口作为RP,F0/1阻塞
vlan1和vlan2的流量转发的路径不同,实现stp的负载均衡:
notion image
notion image

MSTP:

Multiple Spanning Tree Protocol (MSTP) 多重生成树协议,MSTP出现是为了解决在PSTP出现的没有区域和RSTP中vlan缺乏vlan分组的管理功能。
MSTP实验:
SW1和SW2,SW3,不同的instance的端口角色不同,在vlan10中,SW1为根桥,而在默认vlan 1中SW2为根桥
notion image
SW1:
SW1(config)#int range g0/1-2 SW1(config)#switchport mode trunk SW1(config)#spanning-tree mst configuration #设置MSTP协议 SW1(config-mst)#name lion #设置名称 SW1(config-mst)#revision 1 #设置MSTP协议区域以一 SW1(config-mst)#instance 1 vlan 10-15 #设置vlan和instan 对应表 SW1(config-mst)# SW1#show spanning-tree mst configuration #显示MSTP设置,vlan存在时 才会显示instance和vlan对应关系 SW1#show spanning-tree mst configuration Name [lion] Revision 1 Instances configured 3 Instance Vlans mapped -------- --------------------------------------------------------------------- 0 1-9,16-19,26-4094 1 10-15 -------- --------------------------------------------------------------------- SW1(config)#spanning-tree mode mst #启动MSTP协议 SW1(config-if)#int range g0/0-1 SW1(config-if)#switchport access vlan 10 #g0/0,g0/1划分给vlan 10 SW1(config-if-range)#do show span mst 0 ##### MST0 vlans mapped: 1-9,16-19,26-4094 Bridge address 5000.0001.0000 priority 32768 (32768 sysid 0) Root this switch for the CIST Operational hello time 2 , forward delay 15, max age 20, txholdcount 6 Configured hello time 2 , forward delay 15, max age 20, max hops 20 Interface Role Sts Cost Prio.Nbr Type ---------------- ---- --- --------- -------- -------------------------------- Gi0/0 Desg FWD 20000 128.1 P2p Gi0/1 Desg FWD 20000 128.2 P2p Gi0/2 Desg FWD 20000 128.3 P2p Gi0/3 Desg FWD 20000 128.4 P2p Gi1/0 Desg FWD 20000 128.5 P2p Gi1/1 Desg FWD 20000 128.6 P2p Gi1/2 Desg FWD 20000 128.7 P2p Gi1/3 Desg FWD 20000 128.8 P2p SW1#show span MST0 Spanning tree enabled protocol mstp Root ID Priority 24576 Address 5000.0002.0000 Cost 0 Port 1 (GigabitEthernet0/0) Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Bridge ID Priority 32768 (priority 32768 sys-id-ext 0) Address 5000.0001.0000 Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Interface Role Sts Cost Prio.Nbr Type ------------------- ---- --- --------- -------- -------------------------------- Gi0/0 Root FWD 20000 128.1 P2p Gi0/1 Desg FWD 20000 128.2 P2p Gi0/2 Desg FWD 20000 128.3 P2p Gi0/3 Desg FWD 20000 128.4 P2p Gi1/0 Desg FWD 20000 128.5 P2p Gi1/1 Desg FWD 20000 128.6 P2p Gi1/2 Desg FWD 20000 128.7 P2p Gi1/3 Desg FWD 20000 128.8 P2p MST1 Spanning tree enabled protocol mstp Root ID Priority 32769 Address 5000.0001.0000 #在MST1中SW1为根桥,在vlan10-15中 This bridge is the root Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Bridge ID Priority 32769 (priority 32768 sys-id-ext 1) Address 5000.0001.0000 Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Interface Role Sts Cost Prio.Nbr Type ------------------- ---- --- --------- -------- -------------------------------- Gi0/0 Desg FWD 20000 128.1 P2p Gi0/1 Desg FWD 20000 128.2 P2p
SW2:
SW2#show span MST0 Spanning tree enabled protocol mstp Root ID Priority 24576 Address 5000.0002.0000 This bridge is the root #在mst0中,也就是vlan1中sw2是根桥 Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Bridge ID Priority 24576 (priority 24576 sys-id-ext 0) Address 5000.0002.0000 Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Interface Role Sts Cost Prio.Nbr Type ------------------- ---- --- --------- -------- -------------------------------- Gi0/0 Desg FWD 20000 128.1 P2p Gi0/1 Desg FWD 20000 128.2 P2p Gi0/2 Desg FWD 20000 128.3 P2p Gi0/3 Desg FWD 20000 128.4 P2p Gi1/0 Desg FWD 20000 128.5 P2p Gi1/1 Desg FWD 20000 128.6 P2p Gi1/2 Desg FWD 20000 128.7 P2p Gi1/3 Desg FWD 20000 128.8 P2p MST1 Spanning tree enabled protocol mstp Root ID Priority 32769 Address 5000.0001.0000 Cost 20000 Port 2 (GigabitEthernet0/1) Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Bridge ID Priority 32769 (priority 32768 sys-id-ext 1) Address 5000.0002.0000 Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Interface Role Sts Cost Prio.Nbr Type ------------------- ---- --- --------- -------- -------------------------------- Gi0/0 Desg FWD 20000 128.1 P2p Gi0/1 Root FWD 20000 128.2 P2p
SW3:
SW3的mstp配置和端口配置一样 SW3#show spanning-tree MST0 Spanning tree enabled protocol mstp Root ID Priority 24576 Address 5000.0002.0000 Cost 0 Port 1 (GigabitEthernet0/0) Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Bridge ID Priority 32768 (priority 32768 sys-id-ext 0) Address 5000.0003.0000 Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Interface Role Sts Cost Prio.Nbr Type ------------------- ---- --- --------- -------- -------------------------------- Gi0/0 Root FWD 20000 128.1 P2p Gi0/1 Altn BLK 20000 128.2 P2p Gi0/2 Desg FWD 20000 128.3 P2p Gi0/3 Desg FWD 20000 128.4 P2p Gi1/0 Desg FWD 20000 128.5 P2p Gi1/1 Desg FWD 20000 128.6 P2p Gi1/2 Desg FWD 20000 128.7 P2p Gi1/3 Desg FWD 20000 128.8 P2p MST1 Spanning tree enabled protocol mstp Root ID Priority 32769 Address 5000.0001.0000 Cost 20000 Port 2 (GigabitEthernet0/1) Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Bridge ID Priority 32769 (priority 32768 sys-id-ext 1) Address 5000.0003.0000 Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec Interface Role Sts Cost Prio.Nbr Type ------------------- ---- --- --------- -------- -------------------------------- Gi0/0 Altn BLK 20000 128.1 P2p Gi0/1 Root FWD 20000 128.2 P2p SW2(config)#spanning-tree mst 0 root primary
配置完在mst0中的端口角色:
notion image
在创建的mst1中的端口角色:
notion image
这样在vlan1中SW3的g0/1进行阻塞,在vlan10-15中SW2的g0/0进行阻塞,实现负载不同vlan的负载均衡
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Built with Potion.so