An interactive tour of the spanning tree protocol

Vincent Bernat

Warning

This post con­tains in­ter­ac­tive ex­am­ples. To vi­su­al­ize and in­ter­act with them, you need to leave your RSS reader.

Warning

This post con­tains in­ter­ac­tive ex­am­ples. To vi­su­al­ize and in­ter­act with them, you need to en­able JavaScript.

Imag­ine you rent of­fice space for a three-day event. You quickly set up a few Eth­er­net switches and tape some ca­bles on the floor to get ev­ery­one on­line. Un­for­tu­nately, Stan, your clum­si­est co­worker, kicks out a ca­ble ev­ery time he gets up for cof­fee. You could add ex­tra ca­bles, but then you’d get a broad­cast storm: Eth­er­net pack­ets that loop and mul­ti­ply un­til noth­ing else gets through.

That’s where the span­ning tree pro­to­col (STP) comes in. STP blocks just enough of your spare ca­bles to leave a loop-free tree. When Stan strikes again, it re­builds the tree in a sec­ond, leav­ing some time for Blobby, your one-per­son sup­port crew, to re­con­nect the ca­ble.1 See for your­self: the di­a­gram be­low runs a real STP im­ple­men­ta­tion in your browser!

:demo

A1 @0,0 prio=4096
A2 @0,1
A3 @0,2
A4 @0,3

B1 @1,0 prio=8192
B2 @1,1
B3 @1,2
B4 @1,3

C1 @2,0 prio=8192
C2 @2,1
C3 @2,2
C4 @2,3

A1 -- A2 hazard=0
A2 -- A3 hazard=0
A3 -- A4 hazard=0
B1 -- B2
B2 -- B3
B3 -- B4
C1 -- C2 hazard=0
C2 -- C3 hazard=0
C3 -- C4 hazard=0

A1 -- B1 cost=10
B1 -- C1 cost=10
A4 -- B4 cost=20
B4 -- C4 cost=20

Leo @-0.3,0.7 proto=none icon=👦🏻
Mia @-0.3,1.3 proto=none icon=👧🏽
Joy @0.3,0.7  proto=none icon=👱🏻‍♀️
Roy @0.3,1.3  proto=none icon=👨🏾
A2 -- Leo hazard=0 A2:edge
A2 -- Mia hazard=0 A2:edge
A2 -- Joy hazard=0 A2:edge
A2 -- Roy hazard=0 A2:edge

Max @-0.3,1.7 proto=none icon=👨🏽
Zoe @-0.3,2.3 proto=none icon=👩🏾
Ada @0.3,1.7  proto=none icon=👵🏾
Amy @0.3,2.3  proto=none icon=👩🏼
A3 -- Max hazard=0 A3:edge
A3 -- Zoe hazard=0 A3:edge
A3 -- Ada hazard=0 A3:edge
A3 -- Amy hazard=0 A3:edge

Eli @0.7,0.7 proto=none icon=👦🏼
Jay @0.7,1.3 proto=none icon=👨🏻
Kai @1.3,0.7  proto=none icon=🧑🏽
Ben @1.3,1.3  proto=none icon=👱🏼
B2 -- Eli hazard=0.2 B2:edge
B2 -- Jay hazard=0.2 B2:edge
B2 -- Kai hazard=0.2 B2:edge
B2 -- Ben hazard=0.2 B2:edge

Ava @0.7,1.7 proto=none icon=👩🏻
Lea @0.7,2.3 proto=none icon=🧑🏾‍🦱
Ivy @1.3,1.7  proto=none icon=🧕🏽
Rex @1.3,2.3  proto=none icon=👴🏿
B3 -- Ava hazard=0.2 B3:edge
B3 -- Lea hazard=0.2 B3:edge
B3 -- Ivy hazard=0.2 B3:edge
B3 -- Rex hazard=0.2 B3:edge

Ana @1.7,0.7 proto=none icon=👩🏿
Eve @1.7,1.3 proto=none icon=👧🏼
Abe @2.3,0.7  proto=none icon=🧓🏿
Ian @2.3,1.3  proto=none icon=🧔🏾
C2 -- Ana hazard=0 C2:edge
C2 -- Eve hazard=0 C2:edge
C2 -- Abe hazard=0 C2:edge
C2 -- Ian hazard=0 C2:edge

Ned @1.7,1.7 proto=none icon=👨🏼‍🦳
Lou @1.7,2.3 proto=none icon=🧑🏿
Fay @2.3,1.7  proto=none icon=👧🏻
Sue @2.3,2.3  proto=none icon=👩🏽‍🦰
C3 -- Ned hazard=0 C3:edge
C3 -- Lou hazard=0 C3:edge
C3 -- Fay hazard=0 C3:edge
C3 -- Sue hazard=0 C3:edge

Note

This ar­ti­cle is also avail­able as a video, but I ad­vise you to keep read­ing here to try the in­ter­ac­tive demon­strations.

The basics#

De­signed in the ’80s, the span­ning tree pro­to­col has evolved into a “rapid” fla­vor (RSTP) and a “VLAN-aware” vari­a­tion (MSTP).2 Any sound-minded net­work en­gi­neer knows there are bet­ter al­ter­na­tives, like BGP EVPN VXLAN. Yet, be­cause any switch speaks it, the ven­er­a­ble span­ning tree pro­to­col still fills a niche.

We fo­cus on RSTP: it re­placed the orig­i­nal pro­to­col in 2004. To elim­i­nate net­work loops, RSTP im­ple­ments a com­plex state ma­chine. Timers, link state changes, and the link-lo­cal con­trol frames a bridge re­ceives from its neigh­bors drive its tran­si­tions. These Eth­er­net frames are the Bridge Pro­to­col Data Units (BPDUs). You can watch them in ac­tion be­low: hit the “Start” button.

:protocol rstp
:tx-hold 10

A1 @0,1
C11 @1,0 prio=4096 icon=🌳
C12 @1,2 prio=4096 icon=🌳
C21 @2,0 prio=4096 icon=🌳
C22 @2,2 prio=4096 icon=🌳
A2 @3,1

H1 @0,0.2 proto=none icon=💻
H2 @0,1.8 proto=none icon=🖨️
H3 @3,0.2 proto=none icon=📠
H4 @3,1.8 proto=none icon=📺

A1 -- C11
A1 -- C12
A2 -- C21
A2 -- C22
C11 -- C12
C11 -- C21
C11 -- C21
C11 -- C22
C12 -- C21
C12 -- C22
C21 -- C22
A1 -- H1 A1:edge
A1 -- H2 A1:edge
A2 -- H3 A2:edge
A2 -- H4 A2:edge

Af­ter some time, the topol­ogy con­verges to a tree: from the root C11, there is a path to each bridge3 and no loop. In the up­per right cor­ner, the in­ter­face dis­plays a tree icon 🌳 fol­lowed by the time it took to reach this state. Cut a link and see how the pro­to­col finds an al­ter­nate path to reach C12 in less than a sec­ond. You can stop the sim­u­la­tion, move it for­ward step by step, re­set it to its ini­tial state, or slow it down with the “snail” mode 🐌. Don’t worry about all the dis­played in­for­ma­tion: I ex­plain it later.

Note

While you scroll, the cur­rent sim­u­la­tion stays in view so you can look at it while read­ing. Uncheck this box to dis­able this be­havior: .

All ex­am­ples run in your browser, pow­ered by MSTPD—an open-source user-space4 im­ple­men­ta­tion of RSTP.5

Historical interlude#

Ra­dia Perl­man, an in­ductee of the In­ter­net Hall of Fame in 2014, sum­ma­rized the an­ces­tor of STP she in­vented at DEC with this poem, later in­cluded in a US patent:

I think that I shall never see
A graph more lovely than a tree.
A tree whose cru­cial prop­erty
Is loop-free con­nec­tiv­ity.
A tree which must be sure to span
So pack­ets can reach ev­ery LAN.
First, the root must be se­lected.
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 span­ning tree.

― Ra­dia Perl­man, Al­gorhyme.

Electing the root bridge#

To build a tree, RSTP first elects the bridge with the low­est bridge iden­ti­fier as the root bridge. The bridge iden­ti­fier com­bines the pri­or­ity and the MAC ad­dress: 8192.6e:​2b:​10:​a0:5f:29.

In the ex­am­ple be­low, S1 and S2 have pri­or­i­ties of 4,096 and 8,192: S1 be­comes root. S4 has a pri­or­ity of 12,288, while S3 keeps the de­fault pri­or­ity of 32,768:6 S4 be­comes root. S5 and S6 don’t have a spe­cific pri­or­ity, so the low­est MAC ad­dress wins and S5 be­comes root.

:protocol rstp

S1 @0,0 prio=4096
S2 @0,1 prio=8192
S1 -- S2

S3 @1,0
S4 @1,1 prio=12288
S3 -- S4

S5 @2,0
S6 @2,1
S5 -- S6

Ini­tially, each bridge ad­ver­tises it­self as root:7

Spanning Tree Protocol
    Protocol Identifier: Spanning Tree Protocol (0x0000)
    Protocol Version Identifier: Rapid Spanning Tree (2)
    BPDU Type: Rapid/Multiple Spanning Tree (0x02)
    Root Identifier: 8192.02:00:00:01:00:01
    Bridge Identifier: 8192.02:00:00:01:00:01

Once a bridge re­ceives a BPDU ad­ver­tis­ing a bet­ter root bridge, it prop­a­gates this new in­for­ma­tion to its neighbors.

Spanning Tree Protocol
    Protocol Identifier: Spanning Tree Protocol (0x0000)
    Protocol Version Identifier: Rapid Spanning Tree (2)
    BPDU Type: Rapid/Multiple Spanning Tree (0x02)
    Root Identifier: 4096.02:00:00:00:00:00
    Bridge Identifier: 8192.02:00:00:00:00:01

Assigning roles to ports#

The sec­ond step is to as­sign a role to each port. RSTP de­fines five roles, each de­noted by a letter:

  • root (R),
  • des­ig­nated (D),
  • al­ter­nate (A),
  • dis­abled (X), or
  • backup (B).8

Each non-root bridge chooses its root port, the one with the low­est-cost path to the root. Un­less you over­ride it, each bridge de­rives the link cost from the speed: 20,000 for 1 Gbps. In case of equal­ity, the low­est port iden­ti­fier wins.

Each re­main­ing port be­comes a des­ig­nated port if the BPDU it sends is “bet­ter” than the BPDU it re­ceives. Oth­er­wise, it be­comes an al­ter­nate port. Later, if the root port goes down, the “best” al­ter­nate port be­comes the new root port. The tiebreak­ers for the best BPDU are:

  1. the low­est root bridge identifier,
  2. the low­est ac­cu­mu­lated cost to the root,
  3. the low­est bridge iden­ti­fier, and
  4. the low­est port identifier.
:protocol rstp

S1 @1,0  prio=4096 icon=🌳
S2 @0,1
S3 @2,1

S1 -- S2
S1 -- S3
S1 -- S3
S2 -- S3

In the ex­am­ple above, af­ter con­ver­gence, S1 is the root bridge be­cause it has a pri­or­ity of 4,096, while the other bridges have a pri­or­ity of 32,768. All its ports are des­ig­nated ports be­cause the ac­cu­mu­lated cost to the root is 0.

S2’s port fac­ing S1 be­comes a root port be­cause it has the low­est ac­cu­mu­lated cost to the root—20,000 vs 40,000. S3 has two ports fac­ing S1, and the one with the low­est port iden­ti­fier be­comes the root port—0x8000 vs 0x8001. The other can­di­date is an al­ter­nate port be­cause the re­mote port on the link sends a bet­ter BPDU, with an ac­cu­mu­lated cost of 0. On the seg­ment be­tween S2 and S3, S2’s port wins: while both bridges have the same ac­cu­mu­lated cost to the root (20,000), S2’s bridge iden­ti­fier is smaller—32768.02:00:00:00:00:01 vs 32768.02:00:00:00:00:02.

Spanning Tree Protocol
    Protocol Identifier: Spanning Tree Protocol (0x0000)
    Protocol Version Identifier: Rapid Spanning Tree (2)
    BPDU Type: Rapid/Multiple Spanning Tree (0x02)
    Root Identifier: 4096.02:00:00:00:00:00
    Root Path Cost: 20000
    Bridge Identifier: 32768.02:00:00:00:00:01
    Port identifier: 0x8002

If you cut the ac­tive link be­tween S1 and S3, S3 pro­motes the “best” al­ter­nate port to root port. If you also dis­able the sec­ond link, S3 chooses the re­main­ing al­ter­nate port as a root port. But if you dis­able the link be­tween S1 and S2, S2 needs a bit more work to elect a new root port be­cause it does not have an al­ter­nate port.

Un­less a spe­cific event hap­pens, des­ig­nated ports send BPDUs ev­ery 2 sec­onds.9 If a bridge does not re­ceive BPDUs from its neigh­bor for 3 con­sec­u­tive hello pe­ri­ods, it con­sid­ers the neigh­bor dead and re­moves the port in­formation.

Port state transition#

Each port can have one of three states. The di­a­gram dis­plays a back­ground color for each state:

  • dis­card­ing (red),
  • learn­ing (yellow), or
  • for­ward­ing (green).

A root port tran­si­tions au­to­mat­i­cally to the for­ward­ing state. An al­ter­nate port stays in the dis­card­ing state. A des­ig­nated port has two op­tions to tran­si­tion from the dis­card­ing state to the for­ward­ing state:

  • If the port is an edge port, ei­ther through con­fig­u­ra­tion or be­cause the re­mote de­vice does not speak any fla­vor of STP, the bridge as­sumes the de­vice won’t par­tic­i­pate in the pro­to­col and can­not cre­ate a loop. In this case, the des­ig­nated port im­me­di­ately tran­si­tions to the for­ward­ing state.
  • Oth­er­wise, it sends a pro­posal to its down­stream neigh­bor. If the re­mote bridge agrees that the re­ceived BPDU is “bet­ter” than any other BPDU stored for other ports, it elects the re­ceiv­ing port as its root port and starts the syn­chro­niza­tion process: it tran­si­tions all non-edge non-synced des­ig­nated ports to the dis­card­ing state to avoid a loop. Then, it sends back an agree­ment. Upon re­ceiv­ing the agree­ment, the peer des­ig­nated port tran­si­tions to the for­ward­ing state.10
:protocol rstp

S1 @1,0 prio=4096 icon=🌳
S2 @1,1
S3 @0,2
S4 @2,2
S5 @0,3 prio=8192 icon=🪾
S6 @2,3
H1 @0,1.2   proto=none icon=🖨️
H2 @2,1.2   proto=none icon=📠
H3 @2.5,1.3 proto=none icon=📺
H4 @2.5,2.3 proto=none icon=💻

S1 -- S2
S2 -- S3
S2 -- S4
S3 -- S5
S4 -- S6
S4 -- S3
S5 -- S6

S3 -- H1 S3:edge
S4 -- H2 S4:edge
S4 -- H3 S4:edge
S6 -- H4 S6:edge

In the topol­ogy above, H1, H2, H3, and H4 are end de­vices not par­tic­i­pat­ing in the pro­to­col. We con­fig­ure the ports they con­nect to as edge ports, so these ports im­me­di­ately move to the for­ward­ing state.

Use the “step” but­ton to move the sim­u­la­tion for­ward. The clock moves to 1 sec­ond. Step again and S1 and S2 send a pro­posal to each other. Here is the pro­posal from S2:

Spanning Tree Protocol
    Protocol Identifier: Spanning Tree Protocol (0x0000)
    Protocol Version Identifier: Rapid Spanning Tree (2)
    BPDU Type: Rapid/Multiple Spanning Tree (0x02)
    BPDU flags: 0x4e, Agreement, Port Role: Designated, Proposal
        0... .... = Topology Change Acknowledgment: No
        .1.. .... = Agreement: Yes
        ..0. .... = Forwarding: No
        ...0 .... = Learning: No
        .... 11.. = Port Role: Designated (3)
        .... ..1. = Proposal: Yes
        .... ...0 = Topology Change: No
    Root Identifier: 32768.02:00:00:00:00:01
    Root Path Cost: 0
    Bridge Identifier: 32768.02:00:00:00:00:01
    Port identifier: 0x8001

S1 ig­nores it: its own root iden­ti­fier is lower. When S2 re­ceives a sim­i­lar pro­posal from S1, it ac­cepts S1 as its root bridge. It also elects the port to S1 as the root port and starts the syn­chro­niza­tion process. The two des­ig­nated ports are al­ready dis­card­ing, so no change here. Step again and S2 sends two BPDUs to S1. In one of them, the agree­ment bit is 1 and the pro­posal bit is 0. It also shows that S2 ac­cepted S1 as the root bridge and its root port is now in the for­ward­ing state. When re­ceiv­ing this BPDU, S1 tran­si­tions its own des­ig­nated port to the for­ward­ing state. From this point, the link be­tween S1 and S2 for­wards user traffic.

Spanning Tree Protocol
    Protocol Identifier: Spanning Tree Protocol (0x0000)
    Protocol Version Identifier: Rapid Spanning Tree (2)
    BPDU Type: Rapid/Multiple Spanning Tree (0x02)
    BPDU flags: 0x79, Agreement, Forwarding, Learning, Port Role: Root, Topology Change
        0... .... = Topology Change Acknowledgment: No
        .1.. .... = Agreement: Yes
        ..1. .... = Forwarding: Yes
        ...1 .... = Learning: Yes
        .... 10.. = Port Role: Root (2)
        .... ..0. = Proposal: No
        .... ...1 = Topology Change: Yes
    Root Identifier: 4096.02:00:00:00:00:00
    Root Path Cost: 20000
    Bridge Identifier: 32768.02:00:00:00:00:01
    Port identifier: 0x8001

Let’s look at what hap­pened to S5. Re­set the sim­u­la­tion and step twice. S5 ex­changes BPDUs with both S3 and S6. Since S5 has a lower root iden­ti­fier than S3 and S6, it stays the root bridge, while S3 and S6 ac­cept the pro­posal and elect their root ports. S3 and S6 start the syn­chro­niza­tion process. S6’s port to H4 keeps for­ward­ing be­cause it is an edge port. Move one step. Both S3 and S6 send an agree­ment back to S5, which tran­si­tions both des­ig­nated ports to the for­ward­ing state. Yet, the link be­tween S5 and S3 keeps dis­card­ing user traf­fic! If you look care­fully, S3’s port to­ward S5 is now a des­ig­nated port, not a root port. Dur­ing the same step, S3 also re­ceives a bet­ter BPDU from S2 with S1 as the root bridge. It elects its port to S2 as the root port and down­grades the port to S5 to a des­ig­nated port, which stays in the dis­card­ing state.

On the next step, things get a bit tricky. S3 sends a pro­posal to S5:11

Spanning Tree Protocol
    Protocol Identifier: Spanning Tree Protocol (0x0000)
    Protocol Version Identifier: Rapid Spanning Tree (2)
    BPDU Type: Rapid/Multiple Spanning Tree (0x02)
    BPDU flags: 0x4f, Agreement, Port Role: Designated, Proposal, Topology Change
        0... .... = Topology Change Acknowledgment: No
        .1.. .... = Agreement: Yes
        ..0. .... = Forwarding: No
        ...0 .... = Learning: No
        .... 11.. = Port Role: Designated (3)
        .... ..1. = Proposal: Yes
        .... ...1 = Topology Change: Yes
    Root Identifier: 4096.02:00:00:00:00:00
    Root Path Cost: 40000
    Bridge Identifier: 32768.02:00:00:00:00:02
    Port identifier: 0x8002

S5 elects S1 as its root bridge and the port to­ward S3 as its root port. It starts its syn­chro­niza­tion process, but the des­ig­nated port to S6 does not move into the dis­card­ing state. Why? That port stays a des­ig­nated port and its neigh­bor S6 has al­ready sent an agree­ment on the link, so the port keeps its synced status.

Now, let’s step back to look at what hap­pens to S6. At this point, S6 be­lieves S5 is the root bridge. Step once and S4 sends a new pro­posal to S6. S6 ac­cepts the pro­posal, elects S1 as the root bridge and the port to S4 as its root port. The role of the port fac­ing S5 changes: from a root port, it be­comes a des­ig­nated port. Be­cause its peer keeps ad­ver­tis­ing an in­fe­rior BPDU on the link, this port be­comes dis­puted and moves to the dis­card­ing state. The root port tran­si­tions to the for­ward­ing state and the link starts for­ward­ing im­me­di­ately be­cause S4’s des­ig­nated port is al­ready in the for­ward­ing state. If we step one more time, S5 and S6 ex­change two BPDUs. The one from S5 is bet­ter be­cause of its lower bridge iden­ti­fier. S5’s port stays a des­ig­nated port, while S6 down­grades its own port to an al­ter­nate port.

Let’s rewind to the start one last time: cut the link be­tween S1 and S2, run the sim­u­la­tion un­til the topol­ogy is sta­ble, stop the sim­u­la­tion, and re­store the link be­tween S1 and S2. Dur­ing the first step, S1 and S2 ex­change pro­pos­als. S2 elects S1 as the root bridge in­stead of S5 and the port to S1 as the root port. It down­grades the pre­vi­ous root port to a des­ig­nated port and moves it into the dis­card­ing state. The other des­ig­nated port stays synced and keeps its for­ward­ing state. At the next step, S2 sends an agree­ment to S1 and the link be­tween them starts for­ward­ing user traf­fic. It also sends a pro­posal to S3, but not to S4. In­stead, it sends a reg­u­lar BPDU to S4. S4 still elects S1 as its root bridge and the port to S2 as its root port. It de­motes its pre­vi­ous root port, the one to S3, to a des­ig­nated port, which tran­si­tions to the dis­card­ing state be­cause of the root port change. The other al­ter­nate port, to S6, also be­comes a des­ig­nated port and stays in the dis­card­ing state. The new root port moves to the for­ward­ing state. On the next step, S4’s port to S3 set­tles as an al­ter­nate port af­ter re­ceiv­ing a “bet­ter” BPDU from S3.

RSTP is a gi­ant state ma­chine split into smaller ones: bridge de­tec­tion, port in­for­ma­tion, port pro­to­col mi­gra­tion, port role se­lec­tion, port role tran­si­tions, port re­ceive, port state tran­si­tions, port timers, port trans­mit, and topol­ogy change. Some of them are per bridge, some per port. Each bridge runs an in­stance. Time, op­er­a­tional port state changes, and the BPDUs it re­ceives from other in­stances drive the tran­si­tions. Be­ing event-driven makes RSTP more ef­fi­cient but also more dif­fi­cult to un­derstand.

Western Australian Government Railways class Msa Garratt articulated steam
locomotive: elevation and plan drawing
Placeholder for the Port Information state machine extracted from IEEE 802.1Q-2005, page 182. Pending IEEE authorization for reproduction, this is the blueprint for the Western Australian Government Railways class Msa Garratt articulated steam locomotive.

Topology change notification#

A bridge pop­u­lates a MAC ad­dress ta­ble: it as­so­ciates each source MAC ad­dress with the port that last re­ceived it. When for­ward­ing an Eth­er­net frame, it looks up this ta­ble to choose the right port.12 When a link fails, a con­nected fridge reach­able through one port may be­come reach­able through an­other one. The af­fected bridges should flush the MAC ad­dresses they learned, be­cause these en­tries may now be wrong.

For this pur­pose, RSTP im­ple­ments topol­ogy change no­ti­fi­ca­tions us­ing a flood­ing mech­a­nism. When a non-edge port tran­si­tions to the for­ward­ing state, a bridge gen­er­ates BPDUs with the topol­ogy change (TC) bit set. It sends them to all the non-edge des­ig­nated ports and to the root port. It also flushes the MAC ad­dress ta­ble on these ports. When a bridge re­ceives such a BPDU, it prop­a­gates the no­ti­fi­ca­tion to all non-edge des­ig­nated ports and the root port, ex­cept the one the no­ti­fi­ca­tion came from. It also flushes the MAC ad­dress ta­ble on these ports. In the ex­am­ples, the BPDUs with the TC bit set to 1 have a red circle.

:protocol rstp

S1 @1,0 prio=4096 icon=🌳
S2 @0,1
S3 @1,1
S4 @2,1
S5 @1,2
LPT @0.1,2 proto=none icon=🖨️

S1 -- S2
S1 -- S3
S1 -- S4
S2 -- S3
S2 -- S5
S4 -- S5
S5 -- LPT S5:edge

Start the sim­u­la­tion and wait a few sec­onds for the topol­ogy to set­tle. Stop the sim­u­la­tion and dis­able the link be­tween S2 and S5. S5 elects the port fac­ing S4 as the root port, which tran­si­tions im­me­di­ately to the for­ward­ing state. Step once and S5 emits a BPDU with the TC bit set to 1:

Spanning Tree Protocol
    Protocol Identifier: Spanning Tree Protocol (0x0000)
    Protocol Version Identifier: Rapid Spanning Tree (2)
    BPDU Type: Rapid/Multiple Spanning Tree (0x02)
    BPDU flags: 0x79, Agreement, Forwarding, Learning, Port Role: Root, Topology Change
        0... .... = Topology Change Acknowledgment: No
        .1.. .... = Agreement: Yes
        ..1. .... = Forwarding: Yes
        ...1 .... = Learning: Yes
        .... 10.. = Port Role: Root (2)
        .... ..0. = Proposal: No
        .... ...1 = Topology Change: Yes
    Root Identifier: 4096.02:00:00:00:00:00
    Root Path Cost: 40000
    Bridge Identifier: 32768.02:00:00:00:00:04
    Port identifier: 0x8002

S4 re­ceives this BPDU. It flushes the MAC ad­dress ta­ble on the port fac­ing S1: while LPT was pre­vi­ously reach­able through this port, it is now reach­able through S5. Step once. S4 sends S1 a BPDU with the TC bit set to 1. When S1 re­ceives this BPDU, it flushes the MAC ad­dress ta­ble on the ports fac­ing S2 and S3. Step once and S1 sends a no­ti­fi­ca­tion to S2 and S3. Step once again and S2 sends a no­ti­fi­ca­tion to S3, while S3 does noth­ing be­cause the port to­ward S2 is an al­ter­nate port. S3 does not flush any MAC ad­dress ta­ble: LPT is still reach­able through its port to S1.

If you step a bit more, you will see that some of the pe­ri­odic BPDUs keep the TC bit set to 1. Each port runs a timer equal to the hello timer plus one sec­ond.13 The timer starts when the port emits a no­ti­fi­ca­tion. Un­til it ex­pires, the port sets the TC bit to 1 in ev­ery BPDU it sends. You can also see some pe­ri­odic BPDUs with­out the TC bit: they orig­i­nate from a port that only re­ceived a no­ti­fi­ca­tion and there­fore did not arm its timer.

Security#

RSTP is weak against con­fig­u­ra­tion er­rors and ma­li­cious ac­tors. A bridge not talk­ing RSTP can cre­ate a loop. An at­tacker can in­sert them­selves into the topol­ogy to dis­rupt the ser­vice, spy on the traf­fic, or alter it.

To mit­i­gate such prob­lems, you need to iden­tify the edge ports. An edge port con­nects to an end de­vice, like a PC or a printer. Such de­vices do not gen­er­ate BPDUs and can­not cre­ate a loop. RSTP de­fines two re­lated flags:

  • When true, AdminEdge ini­tial­izes a port as an edge port. It de­faults to false.
  • When true, AutoEdge lets a port be­come an edge port when it does not re­ceive BPDUs for 3 sec­onds. It de­faults to true.

If an edge port re­ceives a BPDU, re­gard­less of the val­ues of these two flags, it re­verts to a non-edge port.

R0 @1.5,1.5 prio=8192

# AutoEdge=true, AdminEdge=false, bridge
S1 @3,1.58
R0 -- S1

# AutoEdge=true, AdminEdge=false, end device
H1 @2.84,2.18 icon=🖨️ proto=none
R0 -- H1

# AutoEdge=true, AdminEdge=true, bridge
S2 @2.18,2.84
R0 -- S2 R0:edge

# AutoEdge=true, AdminEdge=true, end device
H2 @1.58,3 icon=💻 proto=none
R0 -- H2 R0:edge

# AutoEdge=false, AdminEdge=true, bridge
S3 @0.68,2.76
R0 -- S3 R0:edge R0:no-auto-edge

# AutoEdge=false, AdminEdge=true, end device
H3 @0.24,2.32 icon=📠 proto=none
R0 -- H3 R0:edge R0:no-auto-edge

# AutoEdge=false, AdminEdge=false, bridge
S4 @0,1.42
R0 -- S4 R0:no-auto-edge

# AutoEdge=false, AdminEdge=false, end device
H4 @0.16,0.82 icon=📺 proto=none
R0 -- H4 R0:no-auto-edge

# Network port, bridge
S5 @0.82,0.16
R0 -- S5 R0:network S5:network

# Network port, end device
H5 @1.42,0 icon=☕ proto=none
R0 -- H5 R0:network

# AdminEdge=true, bpdu-guard=true, bridge
S6 @2.32,0.24
R0 -- S6 R0:bpdu-guard R0:edge

# AdminEdge=true, bpdu-guard=true, end device
H6 @2.76,0.68 icon=💡 proto=none
R0 -- H6 R0:bpdu-guard R0:edge

In the topol­ogy above, S1, S2, S3, S4, S5, and S6 act as bridges, while H1, H2, H3, H4, H5, and H6 act as end devices:

  • S1 and H1 are on a port with­out a spe­cific con­fig­u­ra­tion: AutoEdge is true, AdminEdge is false,
  • S2 and H2 are on a port where AdminEdge is true,
  • S3 and H3 are on a port where AutoEdge is false and AdminEdge is true,
  • S4 and H4 are on a port where AutoEdge is false.

If you start the sim­u­la­tion and wait about 20 sec­onds, links to S1, S2, S3, S4, H1, H2, H3, and H4 even­tu­ally for­ward user traf­fic: none of the flags matter.

But what about the two re­main­ing pairs? S5 and H5 con­nect to a net­work port. Such a port en­ables a non-stan­dard fea­ture: bridge as­sur­ance. The port trans­mits BPDUs re­gard­less of its role. If it does not re­ceive BPDUs for 3 con­sec­u­tive hello pe­ri­ods, it tran­si­tions to the dis­card­ing state. On the link be­tween R0 and S5, you can see BPDUs trav­el­ing in both di­rec­tions, un­like the other links, where only des­ig­nated ports send BPDUs.

S6 and H6 con­nect to a port where AdminEdge is true and BPDU guard is en­abled. This is an­other non-stan­dard fea­ture that shuts down a port if it re­ceives a BPDU.

In sum­mary, if you ex­pect a port to be an edge port, you should set AdminEdge to true and en­able BPDU guard. Oth­er­wise, de­clare it as a net­work port.

Why RSTP today?#

A com­pelling use case for RSTP to­day is an out-of-band net­work for a dat­a­cen­ter, since you can tol­er­ate an out­age of a few sec­onds. The con­fig­u­ra­tion is min­i­mal and you can use cheap switches, like a Cisco 2960X.14 You need two switches act­ing as root bridges, and you build sev­eral loops to con­nect OOB switches in each cab­i­net. This sim­ple de­sign sur­vives one fail­ure on each loop.15

:protocol rstp
:tx-hold 10

# Root bridges
R1 @0,1 prio=0
R2 @0,2 prio=4096
R1 -- R2 cost=200 R1:network R2:network
R1 -- R2 cost=200 R1:network R2:network

# First loop
C1  @1,0 icon=🗄️
C4  @2,0 icon=🗄️
C7  @3,0 icon=🗄️
C10 @4,0 icon=🗄️
C12 @5,0 icon=🗄️
C13 @5,3 icon=🗄️
C15 @4,3 icon=🗄️
C18 @3,3 icon=🗄️
C21 @2,3 icon=🗄️
C24 @1,3 icon=🗄️
R1  -- C1  R1:network C1:network
C1  -- C4  C1:network C4:network
C4  -- C7  C4:network C7:network
C7  -- C10 C7:network C10:network
C10 -- C12 C10:network C12:network
C12 -- C13 C12:network C13:network
C13 -- C15 C13:network C15:network
C15 -- C18 C15:network C18:network
C18 -- C21 C18:network C21:network
C21 -- C24 C21:network C24:network
C24 -- R2  C24:network R2:network

# Second loop
C2  @1,0.5 icon=🗄️
C5  @2,0.5 icon=🗄️
C8  @3,0.5 icon=🗄️
C11 @4,0.5 icon=🗄️
C14 @4,2.5 icon=🗄️
C17 @3,2.5 icon=🗄️
C20 @2,2.5 icon=🗄️
C23 @1,2.5 icon=🗄️
R1  -- C2  R1:network C2:network
C2  -- C5  C2:network C5:network
C5  -- C8  C5:network C8:network
C8  -- C11 C8:network C11:network
C11 -- C14 C11:network C14:network
C14 -- C17 C14:network C17:network
C17 -- C20 C17:network C20:network
C20 -- C23 C20:network C23:network
C23 -- R2  C23:network R2:network

# Third loop
C3  @1,1 icon=🗄️
C6  @2,1 icon=🗄️
C9  @3,1 icon=🗄️
C16 @3,2 icon=🗄️
C19 @2,2 icon=🗄️
C22 @1,2 icon=🗄️
R1  -- C3  R1:network C3:network
C3  -- C6  C3:network C6:network
C6  -- C9  C6:network C9:network
C9  -- C16 C9:network C16:network
C16 -- C19 C16:network C19:network
C19 -- C22 C19:network C22:network
C22 -- R2  C22:network R2:network

This topol­ogy con­verges in about 6 sec­onds. Each loop should stay small (around 16 bridges) to re­duce the prob­a­bil­ity of a dou­ble fail­ure and to avoid shar­ing too much band­width. The de­sign can evolve a bit with­out adding too much com­plex­ity: one VLAN per loop or one bridge do­main per loop.

How large can a network be?#

The max­i­mum age, whose de­fault value is 20, gov­erns the max­i­mum dis­tance of a bridge from the root. The topol­ogy be­low is too big for BPDUs from R1 to reach be­yond S20.16

:protocol rstp
:tx-hold 10
:max-age 20

R1 @0,0 prio=4096 icon=🌳
R2 @0,5 prio=4096 icon=🪾

S1  @1,0
S2  @2,0
S3  @3,0
S4  @4,0
S5  @5,0
S6  @6,0

S7  @6,1
S8  @5,1
S9  @4,1
S10 @3,1
S11 @2,1
S12 @1,1

S13 @1,2
S14 @2,2
S15 @3,2
S16 @4,2
S17 @5,2
S18 @6,2

S19 @6,3
S20 @5,3
S21 @4,3
S22 @3,3
S23 @2,3
S24 @1,3

S25 @1,4
S26 @2,4
S27 @3,4
S28 @4,4
S29 @5,4
S30 @6,4

S31 @6,5
S32 @5,5
S33 @4,5
S34 @3,5
S35 @2,5
S36 @1,5

R1  -- S1
S1  -- S2
S2  -- S3
S3  -- S4
S4  -- S5
S5  -- S6
S6  -- S7
S7  -- S8
S8  -- S9
S9  -- S10
S10 -- S11
S11 -- S12
S12 -- S13
S13 -- S14
S14 -- S15
S15 -- S16
S16 -- S17
S17 -- S18
S18 -- S19
S19 -- S20
S20 -- S21
S21 -- S22
S22 -- S23
S23 -- S24
S24 -- S25
S25 -- S26
S26 -- S27
S27 -- S28
S28 -- S29
S29 -- S30
S30 -- S31
S31 -- S32
S32 -- S33
S33 -- S34
S34 -- S35
S35 -- S36
S36 -- R2
R1  -- R2 cost=200 down

Once the topol­ogy set­tles, part of the net­work con­sid­ers R1 the root, while the other votes for R2. At the bound­ary, S20 tries to start a syn­chro­niza­tion with S21 to move its des­ig­nated port to the for­ward­ing state. The BPDU looks like this:

Spanning Tree Protocol
    Protocol Identifier: Spanning Tree Protocol (0x0000)
    Protocol Version Identifier: Rapid Spanning Tree (2)
    BPDU Type: Rapid/Multiple Spanning Tree (0x02)
    BPDU flags: 0x4e, Agreement, Port Role: Designated, Proposal
    Root Identifier: 4096.02:00:00:00:00:00
    Root Path Cost: 400000
    Bridge Identifier: 32768.02:00:00:00:00:15
    Port identifier: 0x8002
    Message Age: 20
    Max Age: 20

S21 re­jects it be­cause the mes­sage age equals the max­i­mum age. On the other hand, the BPDU S21 sends to S20 looks like this:

Spanning Tree Protocol
    Protocol Identifier: Spanning Tree Protocol (0x0000)
    Protocol Version Identifier: Rapid Spanning Tree (2)
    BPDU Type: Rapid/Multiple Spanning Tree (0x02)
    BPDU flags: 0x7c, Agreement, Forwarding, Learning, Port Role: Designated
    Root Identifier: 4096.02:00:00:00:00:01
    Root Path Cost: 320000
    Bridge Identifier: 32768.02:00:00:00:00:16
    Port identifier: 0x8001
    Message Age: 16
    Max Age: 20

This is not enough to change S20’s root port be­cause S20 has a lower root iden­ti­fier—4096.02:00:00:00:00:00 vs 4096.02:00:00:00:00:01.

Fix­ing the link be­tween R1 and R2 re­solves the is­sue. The max­i­mum mes­sage age any packet car­ries is now 18, be­low the con­fig­ured max­i­mum age. But it only works un­til an­other link breaks. A plau­si­ble fix is to in­crease the max­i­mum age to 40.17

How fast is RSTP?#

RSTP usu­ally con­verges in a cou­ple of sec­onds at startup. It of­ten re­pairs a tree in less than a sec­ond. Even the 38-bridge topol­ogy takes less than 10 sec­onds to con­verge.18 Some topolo­gies can take a bit more time to re­cover when the root bridge be­comes un­available.19

:protocol rstp

R0 @1,0 prio=0
S1 @1,1 prio=4096
S2 @0,2 prio=8192
S3 @2,2

R0 -- S1
S1 -- S2
S2 -- S3
S3 -- S1

In the topol­ogy above, start the sim­u­la­tion, wait for con­ver­gence, hit stop, and cut the link be­tween R0 and S1. The topol­ogy is al­ready op­ti­mal, but RSTP has a hard time con­verg­ing again.

First, S1 loses its root port. It has no more in­for­ma­tion about R0 and elects it­self as the root bridge. It keeps its ports to S2 and S3 as des­ig­nated ports in the for­ward­ing state. Step once and it sends a BPDU to both S2 and S3 to let them know about the root change. When re­ceiv­ing it, S2 ac­cepts S1 as its root be­cause it does not have a bet­ter root on an­other port. It elects the port to S1 as its root port. The other port stays a des­ig­nated port. Both ports keep for­warding.

When re­ceiv­ing the BPDU from S1, S3 be­haves dif­fer­ently: it knows R0 as a bet­ter root than S1 through its al­ter­nate port to S2. It pro­motes this port to a root port and de­motes the port fac­ing S1 to a des­ig­nated port, which re­quires a new agree­ment. Step once and S3 sends a pro­posal to S1 with R0 as the root bridge. S1 elects R0 as the root bridge and pro­motes its port to S3 as a root port.

Dur­ing the same step, S3 also re­ceives a BPDU from S2 stat­ing that S1 is the root bridge. There­fore, S3 has no port left with R0 as the root bridge: it elects S1 as the root bridge and its port to S2 as the root port. Step once and its next BPDU to S1 in­cludes this in­for­ma­tion: S1 elects it­self again as the root bridge. But dur­ing the same wave, S1 sends a pro­posal to S2 with R0 as the root bridge. While S1 and S3 agree that S1 is the root bridge, S2 now be­lieves this is R0! In turn, S2 again con­vinces S3 that R0 is the root bridge, S3 con­vinces S1, S1 con­vinces S2, and S2 con­vinces S3.

This could go on for­ever, but it does not. The BPDUs say­ing “R0 is root” even­tu­ally age out when the mes­sage age goes past the max­i­mum age. In the ex­am­ple above, at the eleventh sec­ond, S2 sends a BPDU to S3 with R0 as root, but S3 drops it be­cause its mes­sage age reached the max­i­mum. With some luck, the topol­ogy can also con­verge faster if a port stops trans­mit­ting new BPDUs af­ter trip­ping the trans­mit hold count, whose de­fault value is 6 per second.

About MSTP#

MSTP is the “VLAN-aware” ver­sion of RSTP: it runs sev­eral in­stances of RSTP and lets the ad­min­is­tra­tor map each VLAN to a spe­cific in­stance. For ex­am­ple, you can map VLANs 100 to 200 to a first in­stance, and 300 to 400 to a sec­ond in­stance. The re­main­ing VLANs map to a spe­cial in­stance named the In­ter­nal Span­ning Tree (IST). MSTP adds its own com­plex­ity, but the gist is that you have sev­eral log­i­cal topolo­gies act­ing in­de­pen­dently. If you want to dig deeper, have a look at “MSTP Tu­to­rial Part I: In­side a Region.”

About the interactive examples#

The in­ter­ac­tive ex­am­ples run MSTPD di­rectly in your browser, com­piled to WebAssembly with em­scripten. A C API re­places the code talk­ing to the Linux ker­nel: it man­ages bridges and ports, ex­ports state as JSON, and drives time de­ter­min­is­ti­cally. A Java­Script wrap­per makes it more user-friendly:

import { loadMSTPD } from "./dist/mstpd.mjs";
const mstp = await loadMSTPD();

// Create 3 bridges
const a = mstp.createBridge("A", { priority: 4096 });
const b = mstp.createBridge("B", { priority: 8192 });
const c = mstp.createBridge("C");

// Each bridge has two ports
const a1 = a.addPort("a-b", { portno: 1 });
const a2 = a.addPort("a-c", { portno: 2 });
const b1 = b.addPort("b-a", { portno: 1 });
const b2 = b.addPort("b-c", { portno: 2 });
const c1 = c.addPort("c-a", { portno: 1 });
const c2 = c.addPort("c-b", { portno: 2 });

// Build a triangle topology
mstp.link(a1, b1);
mstp.link(a2, c1);
mstp.link(b2, c2);

// Enable all bridges and ports
for (const br of [a, b, c]) br.enable();
for (const p of [a1, a2, b1, b2, c1, c2]) p.enable();

// Execute 40 seconds' worth of wall clock and display the topology
mstp.step(40);
console.log("Topology:", mstp.topology());

Sev­eral dozen unit tests ex­plore the fea­tures of MSTPD and check that they work cor­rectly in this en­vi­ronment:

$ node --test *.test.mjs
✔ two bridges: lower priority becomes root (41.657342ms)
✔ triangle loop: exactly one port blocks and all agree on the root (5.832ms)
✔ breaking the active link reconverges and restoring recovers (18.730753ms)
[…]
ℹ tests 40
ℹ pass 40
ℹ fail 0
[…]
ℹ duration_ms 396.190897

Ad­di­tional Java­Script code looks for spe­cific <pre> blocks con­tain­ing a topol­ogy def­i­ni­tion and turns them into in­ter­ac­tive wid­gets. You can in­spect and mod­ify the def­i­ni­tion by hit­ting the “edit” button.

There is also a cool trick to tell whether the topol­ogy has con­verged. Af­ter each step, we save a snap­shot of the sim­u­la­tion mem­ory, play 50 sec­onds’ worth of sim­u­la­tion to check if the topol­ogy is sta­ble, and travel back in time by restor­ing that snapshot. 🕰️

The com­plete code lives on GitHub. I am happy with the re­sult. It can be dif­fi­cult to fol­low ev­ery­thing hap­pen­ing dur­ing a sin­gle step, but step­ping for­ward and back­ward helps. I plan to use the same ap­proach in fu­ture blog posts about net­work­ing features.

Note

Michael Lynch re­viewed a first draft of this ar­ti­cle. He au­thored “Refac­tor­ing Eng­lish,” a book to sharpen your writ­ing for blog posts, doc­u­men­ta­tion, com­mit mes­sages, and tu­to­ri­als. Any er­rors are still mine!

Up­date (2026-10)

This post was briefly dis­cussed on Hacker News.