An interactive introduction to the spanning tree protocol
An interactive introduction to the spanning tree protocol
生成树协议(STP)交互式入门
Imagine you rent office space for a three-day event. You quickly set up a few Ethernet switches and tape some cables on the floor to get everyone online. Unfortunately, Stan, your clumsiest coworker, kicks out a cable every time he gets up for coffee. You could add extra cables, but then you’d get a broadcast storm: Ethernet packets that loop and multiply until nothing else gets through. That’s where the spanning tree protocol (STP) comes in.
想象一下,你租了一个办公空间举办为期三天的活动。你快速架设了几台以太网交换机,并用胶带把线缆固定在地板上,让大家都能上网。不幸的是,你那位笨手笨脚的同事斯坦(Stan)每次起身去喝咖啡时都会踢掉一根网线。你本可以多加几根线缆,但那样会导致广播风暴:以太网数据包会在网络中循环并成倍增加,直到网络瘫痪。这时,生成树协议(STP)就派上用场了。
STP blocks just enough of your spare cables to leave a loop-free tree. When Stan strikes again, it rebuilds the tree in a second, leaving some time for Blobby, your one-person support crew, to reconnect the cable. See for yourself: the diagram below runs a real STP implementation in your browser!
STP 会自动阻断多余的线缆,从而构建出一棵无环的树状拓扑。当斯坦再次“作案”时,协议会在一秒钟内重建这棵树,为你的一人技术支持团队成员 Blobby 争取到重新连接线缆的时间。亲眼看看吧:下面的图表在你的浏览器中运行了一个真实的 STP 实现!
The basics
基础知识
Designed in the ’80s, the spanning tree protocol has evolved into a “rapid” flavor (RSTP) and a “VLAN-aware” variation (MSTP). Any sound-minded network engineer knows there are better alternatives, like BGP EVPN VXLAN. Yet, because any switch speaks it, the venerable spanning tree protocol still fills a niche. We focus on RSTP: it replaced the original protocol in 2004.
生成树协议设计于 80 年代,后来演进为“快速”版本(RSTP)和“支持 VLAN”的变体(MSTP)。任何理智的网络工程师都知道现在有更好的替代方案,例如 BGP EVPN VXLAN。然而,由于几乎所有交换机都支持它,这种古老的生成树协议依然占据着一席之地。我们重点关注 RSTP:它在 2004 年取代了原始协议。
To eliminate network loops, RSTP implements a complex state machine. Timers, link state changes, and the link-local control frames a bridge receives from its neighbors drive its transitions. These Ethernet frames are the Bridge Protocol Data Units (BPDUs). You can watch them in action below: hit the “Start” button.
为了消除网络环路,RSTP 实现了一个复杂的状态机。定时器、链路状态变化以及网桥从邻居接收到的链路本地控制帧驱动着协议的状态转换。这些以太网帧被称为网桥协议数据单元(BPDU)。你可以在下方观察它们的运行过程:点击“Start”按钮即可。
After some time, the topology converges to a tree: from the root C11, there is a path to each bridge and no loop. In the upper right corner, the interface displays a tree icon 🌳 followed by the time it took to reach this state. Cut a link and see how the protocol finds an alternate path to reach C12 in less than a second.
经过一段时间后,拓扑结构收敛为一棵树:从根节点 C11 出发,到每个网桥都有且仅有一条路径,且不存在环路。在右上角,界面会显示一个树状图标 🌳,后面跟着达到该状态所花费的时间。切断一条链路,看看协议如何在不到一秒的时间内找到到达 C12 的替代路径。
Historical interlude
历史插曲
Radia Perlman, an inductee of the Internet Hall of Fame in 2014, summarized the ancestor of STP she invented at DEC with this poem, later included in a US patent:
2014 年入选互联网名人堂的拉迪亚·珀尔曼(Radia Perlman)曾用一首诗总结了她在 DEC 公司发明的 STP 前身,这首诗后来被收录进了一项美国专利中:
I think that I shall never see / A graph more lovely than a tree. / A tree whose crucial property / Is loop-free connectivity. / A tree which must be sure to span / So packets can reach every LAN. / First, the root must be selected. / By ID, it is elected. / Least cost paths from root are traced. / In the tree, these paths are placed. / A mesh is made by folks like me, / Then bridges find a spanning tree.
(译文:我以为我永远不会见到,比树更可爱的图。树的关键属性,是无环的连通性。树必须确保覆盖,以便数据包到达每个局域网。首先,必须选出根节点。通过 ID 进行选举。追踪从根节点出发的最小代价路径。这些路径被置于树中。像我这样的人构建了网格,然后网桥找到了生成树。)
Electing the root bridge
选举根网桥
To build a tree, RSTP first elects the bridge with the lowest bridge identifier as the root bridge. The bridge identifier combines the priority and the MAC address: 8192.6e:2b:10:a0:5f:29. In the example below, S1 and S2 have priorities of 4,096 and 8,192: S1 becomes root. S4 has a priority of 12,288, while S3 keeps the default priority of 32,768: S4 becomes root.
为了构建一棵树,RSTP 首先会选举具有最低网桥标识符(Bridge ID)的网桥作为根网桥。网桥标识符由优先级和 MAC 地址组合而成,例如:8192.6e:2b:10:a0:5f:29。在下面的例子中,S1 和 S2 的优先级分别为 4096 和 8192:S1 成为根网桥。S4 的优先级为 12288,而 S3 保持默认优先级 32768:S4 成为根网桥。