The idea: a loop-free tree that heals fast
Switches are often wired in a ring or a mesh, so that one broken cable does not cut anybody off. But Ethernet frames have no hop limit, and a loop makes broadcasts circle forever. The Spanning Tree Protocol keeps every cable plugged in and turns some ports off, so that exactly one path joins any two switches. Why loops are so bad, and STP on two switches, is on How Ethernet Switches Forward Frames. This page is about what happens after a failure: how fast the tree grows back.
Rapid Spanning Tree (RSTP, IEEE 802.1w, now part of 802.1D-2004) builds the same tree as classic STP (802.1D-1998), but it repairs it in milliseconds instead of 30 to 50 seconds. It does this in two ways: every switch keeps a spare path ready (the alternate port), and two neighbours agree with a quick proposal/agreement handshake instead of waiting on timers.
The page has four switches in a square and a shared hub on SW4 with PC B behind it. PC A is on SW3. Every switch has the default priority 32768. A BPDU hop takes 10 ms on the page, and a cut cable is noticed by both ends at once.
Electing the root bridge
Every switch has a bridge ID: a 2-byte priority (default 32768, set in steps
of 4096) followed by its MAC address. The lowest bridge ID wins and becomes the root bridge. At
power-on each switch believes it is the root and says so in its BPDUs: "root = me, cost 0". As soon as a
switch hears a better root, it stops claiming and passes the better one on. Here all priorities are equal,
so the lowest MAC decides: SW1 (32768.aa00.0000.0001) wins. (Demo 1: RSTP power-on)
Leaving the choice to the MACs is bad practice: the oldest switch often has the lowest MAC. Network engineers set the priority of the switch they want as root. Set SW4 to 4096 and it wins, whatever its MAC. (Demo 2: SW4 priority 4096)
Port roles from path cost
Each port has a path cost that depends on its speed. 802.1D-2004 uses 20000 for 1 Gb/s, 200000 for 100 Mb/s and 2000000 for 10 Mb/s. A BPDU carries a priority vector: root ID, root path cost, sender bridge ID, sender port ID. Smaller is better, field by field. Each switch gives every port one role:
| Role | Meaning | On the page |
|---|---|---|
| Root port | the port with the cheapest path to the root; one per switch (the root has none) | SW2 p1, SW4 p1, SW3 p2 |
| Designated | on each link, the end that is closer to the root; it forwards onto that link | both SW1 ports, SW2 p2, SW4 p2 and p3 |
| Alternate | a second path to the root through another switch; it discards | SW3 p1 |
| Backup | a second port of the same switch on the same shared segment; it discards | SW4 p4 on the hub |
| Edge | a port to an end host, which sends no BPDUs | SW3 p3 to PC A |
SW3 shows why cost beats hop count. Its direct link to SW1 is an old 100 Mb/s link, cost 200000. The way round, SW3 → SW4 → SW2 → SW1, is three 1 Gb/s hops: 20000 + 20000 + 20000 = 60000. So the long way is SW3's root path, and its direct link becomes the alternate port. On SW4, p3 and p4 both go to the hub. p3 has the lower port ID, so p3 is designated and p4 is backup. Classic STP has only root, designated and blocked; the alternate and backup names are new in RSTP.
Port states and edge ports
RSTP has three states. Discarding: drop data frames (BPDUs still go through). Learning: still drop them, but fill the MAC table. Forwarding: pass frames. Classic STP has five: disabled, blocking, listening, learning, forwarding. RSTP merges disabled, blocking and listening into discarding. Root and designated ports end up forwarding; alternate and backup ports stay discarding. An edge port has only a PC behind it, so it forwards at once: there is no loop to fear (Cisco's name for it is PortFast).
A link fails: the alternate port takes over
When the cable between SW3 and SW4 is cut, SW3 loses its root port. But it has kept the BPDU that SW1 sends on the direct link every hello time (2 s), so it already knows another way to the root. The alternate port p1 becomes the root port and starts forwarding in the same instant. No message is needed and no timer runs. PC A's path changes from A → SW3 → SW4 → B to A → SW3 → SW1 → SW2 → SW4 → B, and the page counts an outage of 0.000 s. (Demo 3: link SW3–SW4 fails) A real switch takes a few milliseconds to notice the loss of signal.
Proposal and agreement
When the cable comes back, the two new ports know nothing. Each one thinks it is designated, stays discarding and sends a BPDU with the proposal flag: "I want to forward on this link". SW4's BPDU is better (root SW1, cost 40000), so SW3 makes p2 its root port again (40000 + 20000 = 60000 beats 200000) and p1 goes back to alternate. Before it agrees, SW3 syncs: it puts every other non-edge designated port into discarding, so that no loop can form below it. Then it answers with an agreement, and SW4's p2 forwards as soon as it arrives. (Demo 4: the link comes back)
The sync moves down the tree: a switch that blocked its designated ports proposes on them in turn, and the next switch syncs and agrees. Each hop costs one round trip, so a whole network settles in well under a second. The handshake needs a point-to-point link: on a shared segment (the hub) many switches could answer, so the designated port there falls back to the old timers, 15 s learning and 15 s forwarding. You can see it in Demo 1: every other port forwards within tens of milliseconds, but SW4 p3, and so PC B, waits 30 s.
Classic STP: timers only
Classic STP has no alternate port and no handshake. A port that may forward has to wait: 15 s listening (forward delay), then 15 s learning, then forwarding. The timers are there so that the news of the change can reach every switch before any new path opens. The same cut costs PC A 30 s. (Demo 5) Even the repair hurts: when the cable comes back, SW3's old root port blocks at once, and the new one needs 30 s again. (Demo 6)
| Event | Classic STP (802.1D-1998) | RSTP (802.1w) |
|---|---|---|
| Own root port link fails, a spare path exists | listening + learning: 30 s | alternate port forwards at once |
| A link further away fails | wait for max age (20 s), then 30 s: 50 s | new information is sent at once; old information ages out after 3 missed hellos (6 s) at worst |
| A link comes back | 30 s | proposal/agreement: one round trip per hop |
| A PC plugs in | 30 s (unless PortFast) | edge port: at once |
| Who sends BPDUs | the root; others relay | every switch, every hello (2 s) |
The 50 s case: when the failure is not on a switch's own link, a blocked port may still hold the old, better information. Classic STP keeps it until it is max age (20 s) old, and only then starts listening and learning. RSTP switches send their own BPDUs every hello, react to news at once, and drop information after three missed hellos.
Topology change
After the tree changes, the MAC tables are wrong: SW1 and SW2 never saw PC A before, and SW4 learned PC A on the dead link. A switch whose non-edge port starts forwarding sends BPDUs with the TC (topology change) flag, and every switch that gets one flushes the MAC entries it learned on its other ports. The frames that follow are flooded once and learned again. The page does not draw this step. How MAC learning works is on the Ethernet switch page; a hub, which just repeats every bit, is compared with a switch on Hub vs Switch vs Router.
See also VLANs for several networks on the same switches (with one spanning tree per VLAN group in MSTP), and VRRP and HSRP for the same "keep a spare ready and switch over fast" idea for the default gateway. STP builds a shortest-path tree towards the root, not a minimum spanning tree; for the difference see Prim vs Kruskal.
See also 802.1X and RADIUS: a Port That Opens Only After Login for the other reason an access port may refuse to forward: it waits for a RADIUS server to authorize the device.
See also Link Aggregation with LACP, which bundles parallel links into one port-channel so spanning tree sees a single port and blocks none of them.
What the page leaves out
Hello BPDUs every 2 s (the page sends a round of them only when two neighbours disagree), the TC flush, the extended system ID in the priority (VLAN number added to it), MSTP and per-VLAN spanning tree, the port priority (the page breaks ties on the port number only), the details of the 802.1D-2004 state machines (the page uses message age only to drop stale information after 20 switches), BPDU guard, root guard and loop guard, and mixing RSTP with old STP switches (the port falls back to STP). Checking who may use a port at all is 802.1X, on 802.1X and RADIUS.