生成树协议诞生背景:广播风暴有多可怕?
真实案例:某医院因网络环路导致全院断网3小时
- 交换机CPU占用率100%
- 核心链路带宽被ARP广播占满
- 患者监护仪数据中断
- 根本原因:接入层两台交换机形成环路,未启用STP
数据佐证:
- 单个ARP请求在环路中每秒复制5000次
- 千兆链路3分钟内被垃圾流量占满
-
STP协议核心原理四部曲
1️⃣选举根桥(Root Bridge)

- 选举规则:
- 比较Bridge ID(优先级+MAC地址)
- 优先级默认32768,值小者胜出
- MAC地址小者作为决胜局
- 人为指定根桥:
# 华为
[Switch] stp root primary # 设置为主根桥(优先级0)
[Switch] stp root secondary# 备份根桥(优先级4096)
# 思科
Switch(config)# spanning-tree vlan 1 root primary
2️⃣选举根端口(Root Port)
- 非根桥到达根桥路径成本最小的端口
- 路径成本参考表:
3️⃣选举指定端口(Designated Port)
- 每个网段选择到达根桥最优路径的端口
- 比较顺序:根路径成本 → 发送者Bridge ID → 发送端口ID
4️⃣阻塞其他端口
- 未被选为根端口或指定端口的端口进入Blocking状态
STP端口状态机详解

RSTP:快速生成树的三大革新
1️⃣端口角色扩展
- 新增Alternate Port(替代端口)和Backup Port(备份端口)
- 收敛速度从30秒提升到1秒内
2️⃣快速收敛机制
- Proposal/Agreement机制:通过握手协议快速切换状态
- 边缘端口:连接终端的端口直接进入Forwarding状态
# 华为配置边缘端口
[Switch-GigabitEthernet0/0/1] stp edged-portenable
# 思科配置PortFast
Switch(config-if)# spanning-tree portfast
3️⃣BPDU格式升级
- Version字段从0(STP)变为2(RSTP)
- Flags字段新增角色和状态标识
MSTP:多实例生成树实战配置
1️⃣典型应用场景
- VLAN负载均衡:
- 配置步骤(华为):
[Switch] stp region-configuration
[Switch-mst-region] instance 1 vlan 10-20
[Switch-mst-region] instance 2 vlan 30-40
[Switch-mst-region] active region-configuration
[Switch] stp instance 1 root primary
[Switch] stp instance 2 root secondary
2️⃣思科PVST+配置
Switch(config)# spanning-tree mode rapid-pvst
Switch(config)# spanning-tree vlan 10 priority 4096
Switch(config)# spanning-tree vlan 20 priority 8192
生成树安全加固方案
1️⃣BPDU防护(防私接交换机)
# 华为
[Switch-GigabitEthernet0/0/24] stp bpdu-filterenable
# 思科
Switch(config-if)# spanning-tree bpduguard enable
2️⃣根防护(Root Guard)
# 华为
[Switch-GigabitEthernet0/0/1] stp root-protection
# 思科
Switch(config-if)# spanning-tree guard root
3️⃣环路防护(Loop Guard)
# 华为
[Switch-GigabitEthernet0/0/2] stp loop-protection
# 思科
Switch(config-if)# spanning-tree guard loop
生成树环境经典故障案例
案例1:TCN风暴导致CPU过载
- 现象:核心交换机CPU持续90%+
- 定位:display stp tc-bpdu statistics 发现某端口频繁发送TCN
- 解决:更换接入层故障网卡
案例2:STP模式不兼容引发环路
- 场景:华为交换机(MSTP)与思科交换机(PVST+)互联
- 现象:部分VLAN通信正常,部分VLAN存在环路
- 修复:统一配置为MSTP或启用厂商兼容模式
生成树调优指南
1️⃣计时器优化建议

2️⃣链路类型识别优化
# 华为强制P2P链路类型
[Switch-GigabitEthernet0/0/1] stp point-to-point force-true
# 思科配置链路类型
Switch(config-if)# spanning-tree link-type point-to-point
🔥网络工程师必知:
- 在启用STP的环境中,禁用协议前必须确认无环路风险
- 核心设备建议配置为根桥并设置备份根桥
- 生产环境推荐使用RSTP或MSTP,避免传统STP的慢收敛问题

附:生成树协议速查表(关注公众号后回复”STP”获取)END
发表回复