STP 的原理
STP 的原理
1.根交换机(根网桥)选举:
网络中的所有交换机都参与根交换机的选举过程。每个交换机都有一个桥 ID(Bridge ID),桥 ID 由优先级(2 字节)和交换机的 MAC 地址(6 字节)组成。优先级默认值为 32768,可手动配置,且必须是 4096 的倍数(最大为65536)。在选举时,交换机互相交换 BPDU(Bridge Protocol Data Unit,桥协议数据单元),比较桥 ID,桥 ID 最小的交换机将成为根交换机。例如在下面的 H3C 设备实验中,通过设置 “stp priority 4096” 命令,将 SW4 的优先级设为 4096,使其成为根交换机。
2.端口角色确定
根端口:在非根交换机上,到根交换机路径开销最小的端口被选举为根端口。路径开销与端口带宽相关,带宽越高,开销越小。根端口负责将数据转发到根交换机。如实验中 SW2 上离根交换机最近的端口就成为根端口。
指定端口:每个网段(连接两个或多个设备的链路)都要选举一个指定端口。指定端口负责向网段转发数据。根交换机上的所有端口都是指定端口。在网段中,比较端口所在交换机到根交换机的路径开销,开销小的端口成为指定端口;若开销相同,则比较端口所在交换机的桥 ID,桥 ID 小的交换机上的端口成为指定端口。
阻塞端口:既不是根端口也不是指定端口的端口将被设置为阻塞状态。阻塞端口不转发数据,仅接收 BPDU,用于检测网络拓扑变化。当网络拓扑发生变化时,阻塞端口可能会转换为转发状态,重新构建网络连接。例如在环形网络拓扑中,为防止环路,部分端口会被阻塞。
3.BPDU 交互:
交换机通过周期性发送 BPDU 来交换网络信息,包括根桥 ID、路径开销、端口 ID 等。BPDU 有两种类型,配置 BPDU 用于选举根交换机、确定端口角色等;拓扑变更通知 BPDU(TCN BPDU)用于在网络拓扑发生变化时,通知其他交换机重新计算生成树。正常情况下,根交换机每隔 2 秒发送一次配置 BPDU,其他交换机收到后会将其转发。当网络拓扑发生变化时,检测到变化的交换机会发送 TCN BPDU,最终通知根交换机,触发全网重新计算生成树。
STP实验
实验需求
开启所有设备,等待STP收敛后观察STP状态
使SW4成为根网桥
使闭塞端口出现在SW2上
把SW1上连接的PC的端口配置为边缘端口
实验
1.等待STP收敛后,查看STP运行状态,找出根网桥,闭塞端口
步骤1:根据HCL模拟器的运行机制,SW1的Mac地址应该是最小的,会成为根网桥,在SW1上查看STP运行状态,确认SW1为根网桥
<H3C>system-view
System View: return to User View with Ctrl+Z.
[H3C]sysname SW1
[SW1]
[SW1]display stp
-------[CIST Global Info][Mode MSTP]-------Bridge ID : 32768.128e-3107-0100Bridge times : Hello 2s MaxAge 20s FwdDelay 15s MaxHops 20Root ID/ERPC : 32768.128e-3107-0100, 0RegRoot ID/IRPC : 32768.128e-3107-0100, 0RootPort ID : 0.0BPDU-Protection : DisabledBridge Config-Digest-Snooping : DisabledTC or TCN received : 5Time since last TC : 0 days 0h:1m:32s
<H3C>system-view
System View: return to User View with Ctrl+Z.
[H3C]sysname SW2
[SW2]display stp
-------[CIST Global Info][Mode MSTP]-------Bridge ID : 32768.128e-3809-0200Bridge times : Hello 2s MaxAge 20s FwdDelay 15s MaxHops 20Root ID/ERPC : 32768.128e-3107-0100, 20RegRoot ID/IRPC : 32768.128e-3809-0200, 0RootPort ID : 128.2BPDU-Protection : DisabledBridge Config-Digest-Snooping : DisabledTC or TCN received : 4Time since last TC : 0 days 0h:1m:39s
<H3C>system-view
System View: return to User View with Ctrl+Z.
[H3C]sysname SW3
[SW3]
[SW3]display stp
-------[CIST Global Info][Mode MSTP]-------Bridge ID : 32768.128e-3e1f-0300Bridge times : Hello 2s MaxAge 20s FwdDelay 15s MaxHops 20Root ID/ERPC : 32768.128e-3107-0100, 20RegRoot ID/IRPC : 32768.128e-3e1f-0300, 0RootPort ID : 128.2BPDU-Protection : DisabledBridge Config-Digest-Snooping : DisabledTC or TCN received : 7Time since last TC : 0 days 0h:1m:48s
<H3C>system-view
System View: return to User View with Ctrl+Z.
[H3C]sysname SW4
[SW4]
[SW4]display stp
-------[CIST Global Info][Mode MSTP]-------Bridge ID : 32768.128e-444d-0400Bridge times : Hello 2s MaxAge 20s FwdDelay 15s MaxHops 20Root ID/ERPC : 32768.128e-3107-0100, 40RegRoot ID/IRPC : 32768.128e-444d-0400, 0RootPort ID : 128.2BPDU-Protection : DisabledBridge Config-Digest-Snooping : DisabledTC or TCN received : 15Time since last TC : 0 days 0h:1m:53s
比较Bridge ID 最小的为根网桥(如图SW1为根网桥)
步骤2:在SW4上查看端口状态,发现SW4的g1/0/2被闭塞
[SW1]display stp brief MST ID Port Role STP State Protection0 GigabitEthernet1/0/1 DESI FORWARDING NONE0 GigabitEthernet1/0/2 DESI FORWARDING NONE
[SW2]display stp brief MST ID Port Role STP State Protection0 GigabitEthernet1/0/1 ROOT FORWARDING NONE0 GigabitEthernet1/0/2 DESI FORWARDING NONE
[SW3]display stp brief MST ID Port Role STP State Protection0 GigabitEthernet1/0/1 ROOT FORWARDING NONE0 GigabitEthernet1/0/2 DESI FORWARDING NONE
[SW4]display stp brief MST ID Port Role STP State Protection0 GigabitEthernet1/0/1 ROOT FORWARDING NONE0 GigabitEthernet1/0/2 ALTE DISCARDING NONE
查看SW1,SW2,SW3,SW4链路的状态。(SW4链路g1/0/2闭塞)
2.使SW4成为新的根网桥
分析:要想使SW4成为根网桥,就是让SW4的优先级成为全网最小。只要把SW4的优先级改成比3其它小就可以了
步骤1:修改SW4的优先级为4096
[SW4]stp priority 4096
[SW4]%Jul 3 11:34:50:637 2025 SW4 STP/6/STP_DETECTED_TC: Instance 0's port GigabitEthernet1/0/2 detected a topology change.
步骤2:在SW4上查看STP运行状态,发现SW4已经成为新的根网桥
[SW4]display stp brief MST ID Port Role STP State Protection0 GigabitEthernet1/0/1 DESI FORWARDING NONE0 GigabitEthernet1/0/2 DESI FORWARDING NONE
[SW4]display stp
-------[CIST Global Info][Mode MSTP]-------Bridge ID : 4096.128e-444d-0400Bridge times : Hello 2s MaxAge 20s FwdDelay 15s MaxHops 20Root ID/ERPC : 4096.128e-444d-0400, 0RegRoot ID/IRPC : 4096.128e-444d-0400, 0RootPort ID : 0.0BPDU-Protection : DisabledBridge Config-Digest-Snooping : DisabledTC or TCN received : 15Time since last TC : 0 days 0h:0m:59s
从Bridge ID中可以看到更改成功
[SW1]display stp brief MST ID Port Role STP State Protection0 GigabitEthernet1/0/1 ROOT FORWARDING NONE0 GigabitEthernet1/0/2 ALTE DISCARDING NONE
[SW2]display stp brief MST ID Port Role STP State Protection0 GigabitEthernet1/0/1 DESI FORWARDING NONE0 GigabitEthernet1/0/2 ROOT FORWARDING NONE
[SW3]display stp brief MST ID Port Role STP State Protection0 GigabitEthernet1/0/1 DESI FORWARDING NONE0 GigabitEthernet1/0/2 ROOT FORWARDING NONE
查看链路发现链路已经改变
3.使闭塞端口出现在SW2上
SW4成为了根网桥,按照STP计算机制,闭塞端口此时应该在SW1上。要使闭塞端口到SW2上,就需要让SW2到SW4的cost比SW1到SW4更大。H3C交换机千兆口的cost默认是20,我们可以把SW2的g1/0/2接口的cost改为1000
在SW2上进入g1/0/2接口的接口视图,修改stp cost为1000
[SW2]int
[SW2]interface g1/0/2
[SW2-GigabitEthernet1/0/2]stp cost 1000
[SW2-GigabitEthernet1/0/2]%Jul 3 11:40:15:206 2025 SW2 STP/6/STP_NOTIFIED_TC: Instance 0's port GigabitEthernet1/0/1 was notified a topology change.
[SW2-GigabitEthernet1/0/2]display stp brief MST ID Port Role STP State Protection0 GigabitEthernet1/0/1 ROOT FORWARDING NONE0 GigabitEthernet1/0/2 ALTE DISCARDING NONE
4.把SW1连接PC的所有接口配置为边缘接口
[SW1]interface g1/0/3
[SW1-GigabitEthernet1/0/3]stp ed
[SW1-GigabitEthernet1/0/3]stp edged-port
Edge port should only be connected to terminal. It will cause temporary loops if port GigabitEthernet1/0/3 is connected to bridges. Please use it carefully.
[SW1-GigabitEthernet1/0/3]
[SW1-GigabitEthernet1/0/3]interface g1/0/4
[SW1-GigabitEthernet1/0/4]stp ed
[SW1-GigabitEthernet1/0/4]stp edged-port
Edge port should only be connected to terminal. It will cause temporary loops if port GigabitEthernet1/0/4 is connected to bridges. Please use it carefully.
[SW1-GigabitEthernet1/0/4]