7. Spanning Tree
How switches find the loops in a network, switch them off, and switch them back on when a cable dies
The office from the Ethernet article has grown to three floors, each with its own receptionist. They pass messages between floors using the stairwells, and because nobody wants a single blocked stairwell to cut a floor off, there are three of them: one between floors 1 and 2, one between 2 and 3, and one straight from 1 to 3.
Then somebody on floor 1 shouts "announcement for everyone!" The floor-1 receptionist, following her rule, repeats it up both stairwells. The floor-2 receptionist repeats it to her floor and up her other stairwell to floor 3. The floor-3 receptionist does the same with the copy that came straight from floor 1, sending it down to floor 2. A few seconds later, both copies arrive back on floor 1, where the receptionist, who has no way of knowing she's heard this before, dutifully repeats them again. By lunchtime nobody can hear anything over the announcements.
The fix the receptionists agree on is simple: lock one stairwell door. Messages still reach every floor, just by a slightly longer route, and nothing can go round in a circle. And if one of the open stairwells is ever blocked, they unlock the spare.
That's the whole idea of the Spanning Tree Protocol. At the end of the VLANs article, our house had two switches joined by one trunk, a single point of failure. This article adds the redundant link, watches it break the network, and then follows, number by number, how the switches agree which port to switch off.
Loops: Why Redundancy Bites
Here is the house network, grown a little. Switch A in the living room and switch B in the study are joined, as before, by the trunk between their ports 8. A third switch, C, now feeds a new desk in the loft, and to make sure the loft never loses its connection, it is cabled to both of the other switches: C's port 1 goes to A's port 6, and C's port 2 goes to B's port 7. All three inter-switch links are 1 Gb/s trunks carrying VLANs 10, 20 and 30.
The loft gets one new device, joining the series' cast of locally administered MACs:
- Loft PC (new): MAC
02:00:00:aa:bb:08, IPv4192.168.10.50, on C's port 3 in VLAN 10.
Three switches, three links, and a triangle: a loop. Now let the laptop on A's port 1 send one ordinary broadcast, say an ARP request for the router.
Follow the two copies with the two rules of a learning switch: learn from the source, flood broadcasts out of every other port.
-
A floods the broadcast
Switch A learns
…aa:bb:01→ port 1 and floods the frame within VLAN 10: to the NAS, to the router on port 7, and out of both inter-switch ports, 8 (towards B) and 6 (towards C). Two copies are now loose in the triangle. -
B and C flood their copies onwards
B receives copy 1 on port 8, learns the laptop → 8, and floods it to the desktop and out of port 7 to C. At the same moment, C receives copy 2 on port 1, learns the laptop → 1, and floods it to the loft PC and out of port 2 to B.
-
The copies cross, and the tables flip
B now receives copy 2 on port 7. Same source MAC, different port: B overwrites its entry, laptop → 7, and floods again, giving the desktop a duplicate and sending copy 2 back to A on port 8. C does the mirror image with copy 1: laptop → 2, a duplicate for the loft PC, and out of port 1 to A.
-
Back home, and round again
A receives copy 1 on port 6 and copy 2 on port 8. Each time it relearns: the laptop is on port 6... no, port 8. The one place A never thinks the laptop is any more is port 1, where it actually is. A floods both copies again, out of 1, 2, 7 and the other inter-switch port, and the whole cycle repeats.
Three distinct things have gone wrong at once:
- The copies never die. Ethernet has no TTL or hop count. An IP packet caught in a routing loop dies after at most 255 hops; an Ethernet frame caught in a switching loop goes round until somebody pulls a cable.
- MAC tables flap. Every lap rewrites the laptop's entry on every switch. When the
router replies to the laptop, switch A looks up
…aa:bb:01, finds port 6 or port 8, and sends the reply into the loop instead of to the laptop. - Hosts receive duplicates. Every lap delivers another copy of the same broadcast to every host in the VLAN. Each one has to be processed, and protocols that never expected duplicates get confused.
Accumulation, then explosion
In a triangle, each switch has exactly two ports in the loop, so a copy that arrives on one leaves on the other: the number of circulating copies doesn't grow, it just never shrinks. But every broadcast that anybody sends adds two more immortal copies. A lap through three switches takes microseconds, so each copy is delivered to each host many thousands of times a second, and within seconds the links are full.
It gets dramatically worse as soon as a switch has three ports in loops. Suppose we had also run a second cable between A and B. Now a copy arriving at A leaves on its two other loop ports, so every pass through A or B doubles the number of copies:
A switch with loop ports turns each arriving copy into copies. Here A and B have three loop ports each, so they double every copy that passes through them, while C, with only two, just passes copies on. The growth is still exponential: one broadcast becomes more than a million frames (about ) after a couple of dozen hops, 25 in the simulator below.
There is a hard ceiling, of course: a 1 Gb/s link can carry at most about 1.49 million minimum-size frames per second (64 bytes plus 20 bytes of preamble and inter-frame gap is 672 ns each). The storm reaches it almost immediately. From then on every link in the loop is full, switches drop frames at random, and their CPUs, which have to look at every broadcast, stop answering. This is a broadcast storm, and it is one of the few ways a single cable plugged into the wrong port can take down a whole building.
So why have loops at all?
Because the alternative is fragility. Without the A–C and B–C links, losing one cable or one switch cuts part of the network off. Every serious network wants redundant paths, and Ethernet simply can't use them the obvious way. What we need is a way to keep the spare links plugged in but idle, and bring them into service the moment they're needed, without anybody configuring which ones by hand.
The Idea: Switch Off the Loops
In graph terms, switches are nodes and links are edges. A spanning tree is a subset of the edges that still connects every node (it spans the graph) but contains no cycles (it is a tree). A tree over n nodes always has exactly n − 1 edges, so our three switches need two links; the third must go. Four switches fully meshed with six links would keep three and idle three.
The spanning tree idea
Every switch keeps all its cables plugged in, but on some ports it neither sends nor receives data frames: the port is blocked. The blocked ports are chosen so that the links still in use form a tree. If a link in the tree fails, the switches recompute and unblock just enough ports to make a new tree.
The difficulty is doing this without a central controller: every switch has to reach the same answer by talking only to its neighbours, keep agreeing as cables come and go, and cope with switches being added by people who have never heard of any of this.
In 1985, Radia Perlman, then at Digital Equipment Corporation, published exactly such an algorithm: "An Algorithm for Distributed Computation of a Spanning Tree in an Extended LAN". DEC's bridges used it, and it became the basis of the IEEE 802.1D bridging standard, first published in 1990. Perlman summed it up in a poem, "Algorhyme", a parody of Joyce Kilmer's "Trees". It begins
I think that I shall never see
A graph more lovely than a tree.
and ends
A mesh is made by folks like me,
Then bridges find a spanning tree.
which is as good a specification as any. (The protocol still says "bridge" everywhere, because in 1985 the boxes that joined LAN segments were called bridges. A switch is a many-port bridge, and this article uses the words interchangeably.)
The algorithm has three moves:
- Elect a root. All the switches agree on one of them as the root of the tree.
- Find the shortest way to the root. Every other switch picks its one port that leads to the root most cheaply: its root port.
- Give every link exactly one way in. On each link, the switch closest to the root owns it, and its port there is the link's designated port. Any port that is neither a root port nor a designated port is blocked.
Everything else in this article is detail: how switches exchange the information to do this, how they break ties, how long they wait before trusting the answer, and how the 2001 rewrite made it fast.
BPDUs: How Switches Talk
Switches running spanning tree exchange small messages called BPDUs, Bridge Protocol Data Units. A BPDU is an ordinary Ethernet frame with a few special properties:
- It goes to the multicast address
01:80:c2:00:00:00. This is in the block01:80:c2:00:00:00–0fthat the IEEE reserves for link-local control protocols: an 802.1D switch receives and processes these frames itself but never floods them, so a BPDU only ever travels one link. - Its source address is the MAC of the port that sent it (switches have one per port as well as one for the box).
- It uses the old 802.3 format the Ethernet article mentioned: the two
bytes after the source MAC are a length, not an EtherType, followed by a 3-byte
LLC
header,
42 42 03, which identifies spanning tree. - Designated ports send one every hello time, 2 seconds by default.
What's in a BPDU
The core of every BPDU is four values, which together describe the sender's view of the tree, plus some timers. Here they are in the order they appear:
| Field | Bytes | What it means |
|---|---|---|
| Protocol ID | 2 | Always 00 00 |
| Version | 1 | 0 = classic STP, 2 = Rapid STP, 3 = Multiple STP |
| Type | 1 | 00 configuration, 80 topology change notice, 02 rapid |
| Flags | 1 | Topology change bits; in RSTP, also role, state, proposal, agreement |
| Root bridge ID | 8 | Who the sender believes is the root |
| Root path cost | 4 | The sender's cost to reach that root |
| Sender bridge ID | 8 | Who is sending this BPDU |
| Sender port ID | 2 | Which of the sender's ports it came out of |
| Message age | 2 | How long since the root originated this information |
| Max age | 2 | When stored information expires (20 s) |
| Hello time | 2 | How often BPDUs are sent (2 s) |
| Forward delay | 2 | How long each waiting state lasts (15 s) |
The four timers are all in units of 1/256 of a second, so 20 seconds is 0x1400 (5,120).
A classic configuration BPDU is 35 bytes; a rapid (RSTP) BPDU adds a one-byte "version 1
length" field, always zero, for 36. A topology change notice is just the first four bytes.
The bridge ID
Every switch has an 8-byte bridge ID, and the lowest one in the network wins the election for root. Originally it was simply a 2-byte priority (default 32768) followed by the switch's 6-byte MAC address. Because the MAC comes second, the priority decides first, and the MAC only breaks ties. Since MAC addresses are unique, two bridge IDs are never equal.
When networks started running a separate spanning tree per VLAN, a switch needed a different bridge ID in each tree, but it only had one MAC address. The fix, introduced by IEEE 802.1t in 2001 and called the extended system ID, splits the 16-bit priority into two:
where is the 4-bit priority, from 0 to 15.
The configurable part is now only four bits, which is why switch priorities can only be set in multiples of 4096: 0, 4096, 8192, … up to 61440. The default, 32768, is 8 × 4096. The bottom twelve bits are the VLAN or instance number the tree belongs to, which is why a Cisco switch reports its default priority in VLAN 1 as 32769. Where there is only one tree, the extension is 0.
Ports get a similar treatment. A port ID is 16 bits: a 4-bit port priority (default
128, settable in steps of 16) and a 12-bit port number. Port 8 at the default priority is
0x8008, usually written 128.8. (The 1998 standard used 8 bits for each half; the
default values encode the same way under both.)
Switch A's BPDU, byte by byte
Our three switches run one rapid spanning tree for the whole house, so their extended system IDs are 0. Their bridge IDs are:
| Switch | Priority | MAC | Bridge ID (hex) |
|---|---|---|---|
| A | 4096 | 02:00:00:5a:00:0a |
1000.0200.005a.000a |
| B | 32768 (default) | 02:00:00:5a:00:0b |
8000.0200.005a.000b |
| C | 32768 (default) | 02:00:00:5a:00:0c |
8000.0200.005a.000c |
We've lowered A's priority to 4096 deliberately, because A is the switch next to the router
and we want it at the centre of the tree. Once the network has settled, this is the BPDU
that A sends out of port 8 every two seconds, from that port's MAC,
02:00:00:5a:0a:08:
An RSTP BPDU from switch A, port 8: 64 bytes
0000 01 80 c2 00 00 00 02 00 00 5a 0a 08 00 27 42 42
0010 03 00 00 02 02 3c 10 00 02 00 00 5a 00 0a 00 00
0020 00 00 10 00 02 00 00 5a 00 0a 80 08 00 00 14 00
0030 02 00 0f 00 00 00 00 00 00 00 00 00 af db 9a f2
Ethernet header
destination, source, length 0x0027 = 39 ·
LLC
42 42 03 ·
BPDU
36 bytes · 7 bytes of padding to reach the 64-byte minimum ·
FCS
CRC-32 0xF29ADBAF, sent least-significant byte first.
| BPDU bytes | Field | Value here |
|---|---|---|
00 00 |
Protocol ID | Spanning tree |
02 / 02 |
Version / type | Rapid spanning tree BPDU |
3c |
Flags | 0011 1100: role designated, learning, forwarding |
10 00 02 00 00 5a 00 0a |
Root bridge ID | A (priority 4096, extension 0) |
00 00 00 00 |
Root path cost | 0: the sender is the root |
10 00 02 00 00 5a 00 0a |
Sender bridge ID | A again |
80 08 |
Sender port ID | 128.8 |
00 00 |
Message age | 0: fresh from the root |
14 00 · 02 00 · 0f 00 |
Max age · hello · forward delay | 20 s · 2 s · 15 s |
00 |
Version 1 length | Always 0 for RSTP |
In plain words, this BPDU says: "I'm A, port 8. The root is A, and from here it costs 0 to reach it." Every BPDU in the network says the same kind of thing, and the whole algorithm comes down to switches comparing such statements.
The Algorithm, Step by Step
Which BPDU is better?
When a switch has two BPDUs to choose between, it compares them as a priority vector, field by field, like words in a dictionary. The first field that differs decides, and lower is better every time:
-
Root bridge ID
A BPDU naming a better (lower) root wins outright.
-
Root path cost
Same root? The cheaper path to it wins.
-
Sender bridge ID
Same cost? The BPDU from the lower bridge ID wins.
-
Sender port ID
Same sender? (Two cables to the same switch.) The lower port ID on the sender wins.
-
Receiving port ID
Still tied? This only happens when the same sender port is heard on two of your own ports, through a hub or a dumb switch. Your own lower port ID wins.
A BPDU that wins this comparison is called superior; one that loses is inferior. Every decision in spanning tree is one of these comparisons.
Path costs
The root path cost is the sum of the costs of the ports a path enters on its way to the root. Each port has a cost based on its speed, and there are two tables of recommended values, which is a common source of confusion:
| Link speed | 802.1D-1998 (short) | 802.1D-2004 (long) |
|---|---|---|
| 10 Mb/s | 100 | 2,000,000 |
| 100 Mb/s | 19 | 200,000 |
| 1 Gb/s | 4 | 20,000 |
| 10 Gb/s | 2 | 2,000 |
| 100 Gb/s | — | 200 |
| 1 Tb/s | — | 20 |
The short values come from the 1998 standard and are 16-bit numbers chosen by hand; with 10 Gb/s at 2, they left no room for anything faster. The long values, introduced by 802.1t and adopted in 802.1D-2004, are 32-bit and follow a formula: 20 Tb/s divided by the link speed. Every switch in a network should use the same table, and most managed switches let you choose (Cisco calls the setting the "path cost method"). Our worked example uses the short values, so every 1 Gb/s link costs 4. (With long values every link would cost 20,000 and the result would be identical, because all three links have the same speed.)
The three rules
-
Elect the root bridge
At power-up, every switch assumes it is the root and sends BPDUs saying so. Whenever a switch hears a superior root ID, it stops claiming and starts passing on the better one. The switch with the lowest bridge ID in the network is the only one that never hears a better claim: it becomes the root. All of the root's ports are designated ports.
-
Each other switch picks one root port
For each port, the switch takes the best BPDU heard there and adds the port's own cost. The port with the best result is the root port: lowest root path cost, then lowest sender bridge ID, then lowest sender port ID, then lowest own port ID.
-
Each link picks one designated port; everything else blocks
On each link, the port offering the best BPDU, the one that would be the best way onto the tree for anything on that link, is designated and forwards. A port that is neither root nor designated blocks: it keeps listening to BPDUs, but sends and receives no data.
In pseudocode, the decision every switch makes whenever its information changes:
# A priority vector, compared field by field (lexicographically). Lower is better.
# (root_id, root_cost, sender_id, sender_port, receiving_port)
function recompute(bridge):
best = (bridge.id, 0, bridge.id, 0, 0) # "I am the root"
bridge.root_port = None
for p in bridge.ports where p.up and p.heard != None:
h = p.heard # best BPDU received on p
via_p = (h.root_id, h.root_cost + p.cost, h.sender_id, h.sender_port, p.id)
if via_p < best:
best = via_p
bridge.root_port = p
bridge.root_id = best.root_id
bridge.root_cost = best.root_cost
for p in bridge.ports where p.up:
mine = (bridge.root_id, bridge.root_cost, bridge.id, p.id)
if p == bridge.root_port:
p.role = ROOT
else if p.heard == None or mine < p.heard:
p.role = DESIGNATED # I'm the best way in for this link
else if p.heard.sender_id == bridge.id:
p.role = BACKUP # hearing myself: a second port on one segment
else:
p.role = ALTERNATE # somebody else serves this link
function every_hello_time(bridge): # every 2 s
for p in bridge.ports where p.role == DESIGNATED:
send(p, BPDU(bridge.root_id, bridge.root_cost, bridge.id, p.id))
Two details are worth noticing. A switch adds the cost of the port a BPDU arrives on, not the port it leaves from, so the cost a BPDU carries is always the sender's distance from the root. And only designated ports send BPDUs: a root port or a blocked port just listens. (Classic STP is stricter still: only the root originates BPDUs every hello time, and the other switches relay them, adding their cost, as they arrive.)
Pick a comparison
Or edit any field. Both BPDUs are as heard by one switch; the receiving port is that switch’s own port, used only in step 5.
Field by field, lower wins
Worked Example: Three Switches Agree
Here is the house again, with every number the switches need. All three links are 1 Gb/s trunks with cost 4, and every port has the default port priority, 128.
| Link | Ends | Port IDs | Cost |
|---|---|---|---|
| A–B | A port 8 ↔ B port 8 | 128.8 ↔ 128.8 | 4 |
| A–C | A port 6 ↔ C port 1 | 128.6 ↔ 128.1 | 4 |
| B–C | B port 7 ↔ C port 2 | 128.7 ↔ 128.2 | 4 |
We'll write each BPDU as [root · cost · sender · port], so A's BPDU above is [A · 0 · A · 128.8]. Power all three switches on at once:
-
Round 1: everyone claims to be root
Each switch sends its own claim out of every inter-switch port. A sends [A · 0 · A · 128.8] on port 8 and [A · 0 · A · 128.6] on port 6. B sends [B · 0 · B · 128.8] and [B · 0 · B · 128.7]. C sends [C · 0 · C · 128.1] and [C · 0 · C · 128.2].
-
Everyone hears A, and A wins
A's bridge ID,
1000.…, beats both8000.…IDs at step 1 of the comparison: the priority field alone settles it. A hears only inferior claims, so A is the root, and its ports 6 and 8 are designated. B hears [A · 0 · A · 128.8] on port 8: root A, cost 0 + 4 = 4. That beats C's claim on port 7, so B's root port is 8, at cost 4. Likewise C hears A on port 1: C's root port is 1, at cost 4. -
Round 2: B and C pass the news along
B now tells C, on port 7, [A · 4 · B · 128.7]. At the same moment C tells B, on its port 2, [A · 4 · C · 128.2]. Both have the same root and the same cost: a tie.
-
The B–C link: a tie-break decides
Each switch compares what it hears with what it would send itself. Step 1: root A, equal. Step 2: cost 4, equal. Step 3: sender bridge ID. B and C have the same priority, 32768, so it comes down to the MAC addresses:
…5a:00:0bis lower than…5a:00:0c. B's port 7 is designated for the B–C link. C's port 2 hears a better offer than its own, and getting to the root through it would cost 4 + 4 = 8, worse than the 4 through port 1. So C's port 2 is neither root nor designated: it blocks.
| Port | Role | Classic STP state | Why |
|---|---|---|---|
| A6, A8 | Designated | Forwarding | All the root's ports are designated |
| B8 | Root | Forwarding | Cost 4 directly, against 8 via C |
| B7 | Designated | Forwarding | Ties with C at cost 4; B's lower bridge ID wins |
| C1 | Root | Forwarding | Cost 4 directly, against 8 via B |
| C2 | Alternate | Blocking | Hears a better BPDU from B; not its best way to the root |
Notice what decided the blocked port: not anything about C's port 2 itself, but the fact
that B's MAC address ends in 0b and C's in 0c. Swap the switches' MACs and the block
moves to B's end of the same cable. That's the sort of accident spanning tree is full of,
and why you should always choose the root, and ideally a backup root, on purpose.
What if nobody had set a priority?
With all three switches left at 32768, the root is simply the lowest MAC address. Our
switches' MACs all start with 02, the locally administered prefix the
series uses. A real switch's burned-in MAC starts with its vendor's OUI, and very many
OUIs start with 00. Plug in an old spare switch with a MAC like
00:00:5e:00:53:01 (a documentation address, standing in for any real
vendor's) and, at the same default priority, it beats every switch
in the house at step 1 and becomes the root of the whole network, with every path
reshaped to run through the cupboard where it lives. A priority of 4096 on A means no
default-priority switch can ever take over.
When tie-breakers go deeper
Suppose that instead of switch C, we had simply run a second cable between A and B for redundancy: A's port 5 to B's port 9. B now hears the root on two ports:
-
On port 8: [A · 0 · A · 128.8]
Root path cost through port 8 = 0 + 4 = 4.
-
On port 9: [A · 0 · A · 128.5]
Root path cost through port 9 = 0 + 4 = 4.
-
Compare
Root: tie. Cost: tie. Sender bridge ID: tie, it's A both times. Sender port ID: 128.5 beats 128.8. B's root port is 9, and port 8 becomes an alternate port and blocks, even though 8 is the lower number on B. What counts is the port number on the sender, A.
To make B prefer the cable on port 8, you'd lower the port priority on A's port 8, say
to 64, making its port ID 64.8 = 0x4008, which beats 128.5 = 0x8005. Changing anything
on B wouldn't help, which surprises many people the first time. (Better still, as we'll see
at the end, is to bundle the two cables with link aggregation so that both carry traffic.)
Port States and Timers in Classic STP
Knowing which port to block is half the problem. The other half is when it's safe to start forwarding. Imagine B decided its port 7 should forward a split second before C heard the news and blocked its port 2: for that split second, there'd be a loop. The original 802.1D answer is to be patient. A port that is going to forward first spends time in two intermediate states:
| State | BPDUs | Learns MACs? | Forwards data? |
|---|---|---|---|
| Blocking | Receives only | No | No |
| Listening | Sends and receives | No | No |
| Learning | Sends and receives | Yes | No |
| Forwarding | Sends and receives | Yes | Yes |
| Disabled | None (port shut down) | No | No |
Listening gives news time to spread through the whole network, so every switch has settled on its roles before anyone forwards. Learning fills the MAC table before data flows, so the port doesn't start by flooding everything it receives. Each lasts one forward delay. Three timers, all carried in every BPDU and dictated by the root so every switch uses the same values, run the whole show:
| Timer | Default | What it does |
|---|---|---|
| Hello time | 2 s | How often the root sends BPDUs |
| Max age | 20 s | How long a port keeps information it hasn't heard refreshed |
| Forward delay | 15 s | How long each of listening and learning lasts |
The defaults are calculated for a network at most seven switches across. They also make classic STP slow:
- Power-up: 30 seconds. Every port, including the one your laptop is plugged into, spends 15 s listening and 15 s learning before it forwards. That's long enough for a laptop's DHCP request to time out, which is why so many people met spanning tree first as "the network doesn't work for half a minute after I plug in".
- Direct failure: 30 seconds. Cut the A–C cable. C notices its root port go down immediately, and its blocked port 2 still holds a perfectly good BPDU from B, [A · 4 · B · 128.7]. So port 2 becomes C's new root port, at cost 8, straight away. But it still has to go through listening and learning: the loft is cut off for 30 s.
- Indirect failure: 50 seconds. Restore A–C and cut A–B instead. B loses its root port and has nothing else that leads to A, so it concludes it must be the root and sends [B · 0 · B · 128.7] to C. C's blocked port 2 holds the better information from before, [A · 4 · B · 128.7], and classic STP ignores inferior BPDUs on such a port. It waits for the stored information to age out after max age, 20 s. Only then does port 2 take over as designated for the B–C link, telling B where the root really is: B adopts port 7 as its root port, at cost 8. Port 2 now listens for 15 s and learns for 15 s. The study is cut off from the rest of the house for 20 + 15 + 15 = 50 s.
(Strictly, the stale information expires max age after the root sent it, and it was already a second or two old when C stored it, so the real wait is a little under 20 s. The round number is the one everyone quotes, and the one the simulator below uses.)
Cisco added proprietary extensions to shave these times: PortFast for host ports, and UplinkFast and BackboneFast for exactly the direct and indirect failures above. The ideas behind them were good enough that the IEEE folded their equivalents into the rapid version of the protocol, which we'll get to shortly.
Topology Changes and Stale MAC Tables
There's a subtler problem after a failure. In the indirect case, once C's port 2 forwards,
the tree is fine, but some MAC tables are now wrong. Entries pointing at a dead port are
not the problem: a switch flushes a port's entries the moment its link goes down, so A's
entries on port 8 are already gone. The trouble is entries pointing at a port that still
works but no longer leads to the host. Switch C learned the desktop (…aa:bb:05) on
port 1, back when frames from the study came round through A. Now the desktop is
reached through port 2, but C's table still says port 1. Left alone, that entry would
last the full aging time of 300 seconds from the Ethernet article: C
would keep sending the desktop's frames out of port 1 to A. A no longer knows the desktop,
so it floods them to its own hosts and the router, but never back out of port 6, the port
they came in on. They never reach the study: five minutes of lost frames, unless the desktop
happens to send something first and C relearns it.
Classic STP handles this with a topology change procedure:
-
Notice
A switch that sees a port go from forwarding or learning to blocking or down, or start forwarding, sends a Topology Change Notification (TCN) BPDU, the 4-byte one, out of its root port, repeating it every hello time.
-
Relay
The next switch up acknowledges it by setting the TCA flag in its next configuration BPDU, and sends a TCN of its own towards the root. Hop by hop, the news reaches the root.
-
Broadcast
The root sets the TC flag in all its BPDUs for max age + forward delay, 35 s by default, so every switch in the tree hears about it.
-
Forget faster
While they're receiving BPDUs with TC set, switches use the forward delay, 15 s, as their MAC aging time instead of 300 s. Entries for hosts that are actively talking get refreshed; stale ones vanish quickly, and frames to those hosts are flooded until they're relearned on the right port.
The aging time is shortened rather than the table flushed outright, so hosts that are actively sending keep their entries throughout. In the A–B failure the procedure starts at the moment of the cut, because port 8 going down is itself a topology change: C's stale entry for the desktop ages out 15 s later, long before port 2 forwards, rather than lingering for 300 s. The simulator below shows exactly this.
Rapid Spanning Tree (802.1w)
By the late 1990s, 30 to 50 seconds of silence after every hiccup was unacceptable, and vendors were shipping incompatible fixes. The IEEE's answer was Rapid Spanning Tree Protocol, IEEE 802.1w, published in 2001 and folded into 802.1D-2004, which replaced the original algorithm entirely. RSTP keeps the bridge IDs, port IDs, path costs and superior-BPDU comparisons exactly as they were. What it changes is how quickly a switch is allowed to act on them.
Roles and states, separated
Classic STP mixed up two questions: what a port is for, and what it's doing right now. RSTP separates them. A port has a role:
| Role | Meaning | Usual state |
|---|---|---|
| Root | This switch's best path to the root | Forwarding |
| Designated | The best path to the root for its link | Forwarding |
| Alternate | A different path to the root, via another switch: a ready-made spare root port | Discarding |
| Backup | A second port on a link this switch already serves, as through a hub | Discarding |
and, separately, a state. Blocking, listening and disabled all did the same thing to data frames, so RSTP merges them into one:
| Classic STP state | RSTP state |
|---|---|
| Disabled, blocking, listening | Discarding |
| Learning | Learning |
| Forwarding | Forwarding |
In our house, C's port 2 is now an alternate port: blocked, but known to lead to the root through B. That one word carries real information: if C's root port fails, the alternate can take over instantly, without asking anyone.
Every switch says hello
In RSTP every switch sends its own BPDUs on its designated ports every hello time, instead of waiting to relay the root's. That turns BPDUs into keepalives: a port that misses three hellos in a row (6 s) knows its neighbour is gone, even if the link light stays on, rather than waiting 20 s for max age. In practice, most failures are faster still: a cable pulled or a fibre cut takes the link down, and the switch knows within milliseconds.
The flags byte, nearly empty in classic STP, now carries the sender's role and state:
| Bit | Flag |
|---|---|
| 0 | Topology change |
| 1 | Proposal |
| 2–3 | Port role: 01 alternate/backup, 10 root, 11 designated |
| 4 · 5 | Learning · forwarding |
| 6 | Agreement |
| 7 | Topology change acknowledgment (classic only) |
A's 3c = 0011 1100 from earlier decodes as role 11 (designated), learning and
forwarding.
Edge ports
A port with a laptop on it can't create a loop: laptops don't forward frames. RSTP lets
such ports be configured as edge ports, which go straight to forwarding when the link
comes up, with no waiting at all. This is the standard's version of Cisco's PortFast, and
on Cisco switches the same command, spanning-tree portfast, configures it. If a BPDU ever
arrives on an edge port, it immediately loses its edge status and becomes an ordinary
spanning tree port, since something that speaks spanning tree is evidently plugged in.
RSTP also cares about the link type. A full-duplex port is assumed to be point-to-point: exactly one other switch on the far end. Only on point-to-point links can the fast handshake below be used.
Proposal and agreement
The waiting in classic STP exists because a switch can't know whether everybody else has heard the news. RSTP replaces the waiting with asking. On a point-to-point link, a designated port that wants to forward sends a BPDU with the proposal flag set. The switch on the other end, before agreeing, makes sure it can't create a loop: it puts all its other non-edge ports into discarding (a step called sync), then replies with an agreement. The proposing port goes straight to forwarding. The synced switch then repeats the handshake on its own designated ports, so a wave of proposals and agreements ripples outwards from the root, one link at a time, at the speed of the BPDUs rather than of the timers.
At power-up, our three switches converge this way. A proposes on ports 6 and 8; B and C each make the port the proposal arrived on their root port, sync and agree; A's ports forward. B then proposes on port 7, and C, finding that port 2 is an alternate, discards there and answers so that B's port 7 can forward. The whole house is up in well under a second, instead of 30.
The failures again, with RSTP
Direct failure (A–C cut). C's root port goes down. C already has an alternate port, port 2, with B's BPDU stored on it, so it makes port 2 the new root port at cost 8 and puts it straight into forwarding. No proposal is needed: an alternate port leads to the root by definition, so it can't form a loop. The loft is back as soon as C notices the link is down.
Indirect failure (A–B cut). This is where RSTP's new rule about inferior BPDUs pays off.
-
B claims the root
B's port 8 goes down. B has no alternate port, so, just as in classic STP, it falls back on "I am the root" and immediately sends [B · 0 · B · 128.7] out of port 7.
-
C believes it, and knows better
In classic STP, C's port 2 would sit on its old information for 20 s. RSTP has a different rule: inferior information from the same designated bridge and port that a port has been listening to replaces what it had, because that bridge is the authority on its own situation. So C accepts that B is no longer connected to A. But C still is, through port 1, and its own [A · 4 · C · 128.2] is now the best offer on the B–C link. Port 2 becomes designated, starts in discarding, and sends a proposal.
-
B agrees
To B, root A beats root B, so port 7 becomes its root port, at cost 4 + 4 = 8. B syncs (its remaining ports are edge ports, so there's nothing to block) and sends an agreement. C's port 2 moves to forwarding. B's port 7 was already forwarding and stays that way.
Topology changes in RSTP
RSTP also streamlines topology change handling. Only a non-edge port moving to forwarding counts as a change (a laptop plugging in doesn't, and a port going down doesn't either, since its MAC entries are flushed locally anyway). The switch that sees it flushes the MAC entries it learned on its other non-edge ports, and sends BPDUs with the TC flag set out of its root port and designated ports for a few seconds: about twice the hello time, 4 s in Cisco's description and in 802.1w (the 2004 standard reportedly uses slightly less on point-to-point links). Every switch that receives one does the same: flushes the entries on all its non-edge ports except the one the TC arrived on, and passes it on. There is no TCN-to-the-root-and-back detour; the news floods outwards from where the change happened.
In the A–B failure, C's port 2 starting to forward is the change. C flushes what it had learned on its other port, port 1, and that matters: C had learned the desktop there, back when frames from the study came round through A, and now the desktop is reached through port 2. C then sends TC BPDUs out of both ports. A receives one on port 6 and flushes its other non-edge ports (its entries on port 8 had already gone when that link died); B receives one on port 7 and has nothing else to flush but edge ports. The next frames relearn everything along the new paths.
Speaking to the old protocol
An RSTP switch that receives a version-0 BPDU on a port falls back to classic STP behaviour
on that port, so mixed networks work, but with classic timers wherever an old switch is
involved. The version number in that 02 byte is how they tell each other apart.
Run the protocol
Cables
Tap a link on the diagram, or use these buttons, to cut it or plug it back in.
Priorities
Ports
| Port | Role | State | Timer | Stored BPDU |
|---|
MAC tables
Where each switch would send a frame for three of the hosts. The hosts stay quiet unless you send a broadcast, so entries are learned or refreshed only when you do.
| Switch | Laptop …bb:01 | Desktop …bb:05 | Loft PC …bb:08 |
|---|
Event log
Spanning Tree Meets VLANs
Our house carries three VLANs over the same three trunks. So far, one tree serves them all: in every VLAN, C's port 2 blocks. That's the original 802.1Q model, a Common Spanning Tree, and it has a cost: the B–C link sits idle for every VLAN, all the time, even though it's perfectly healthy.
One tree per VLAN: PVST+
Cisco's answer, from the 1990s onwards, was to run a separate spanning tree in every VLAN: PVST+ (Per-VLAN Spanning Tree Plus), and later Rapid PVST+, which is RSTP per VLAN. Each VLAN has its own root, its own BPDUs, and its own blocked ports, and this is exactly where the extended system ID earns its keep: one switch, one MAC address, but a different bridge ID in every VLAN.
The payoff is load sharing. Make A the root for VLANs 10 and 30 and B the root for VLAN 20, each set to 8192 as the backup root for the other's VLANs, and leave C at the default. These are the priority fields, priority plus VLAN ID:
| VLAN | Switch A | Switch B | Switch C |
|---|---|---|---|
| 10 | 4106 (root) | 8202 | 32778 |
| 20 | 8212 | 4116 (root) | 32788 |
| 30 | 4126 (root) | 8222 | 32798 |
So switch A's bridge ID is 100a.0200.005a.000a in VLAN 10 and 2014.0200.005a.000a
in VLAN 20. VLANs 10 and 30 work out as in the worked example, except that the tie on the
B–C link is now settled by priority rather than MAC: B's 8202 beats C's 32778, and C's
port 2 blocks. In VLAN 20, B is the root. A's root port is 8 and C's is 2, both at cost 4,
and the A–C link is a tie at cost 4: A's 8212 beats C's 32788, so A's port 6 is designated
and C's port 1 blocks.
On Cisco switches, the configuration is a couple of lines:
Rapid PVST+ with load sharing
! on every switch
spanning-tree mode rapid-pvst
! on switch A
spanning-tree vlan 10,30 priority 4096
spanning-tree vlan 20 priority 8192 ! backup root for VLAN 20
! on switch B
spanning-tree vlan 20 priority 4096
spanning-tree vlan 10,30 priority 8192 ! backup root for VLANs 10 and 30
The 8192 backup-root settings matter most when a root fails: the other main switch takes over, rather than whichever has the lowest MAC. Day to day, they also settle the ties on C's links in the main switches' favour, as in the table above.
PVST+ has two costs. Every VLAN means another set of BPDUs on every trunk port and another
copy of the state machines in every switch, which adds up with hundreds of VLANs. And it is
Cisco's: its per-VLAN BPDUs go to a Cisco multicast address, 01:00:0c:cc:cc:cd, while
it speaks standard BPDUs only for VLAN 1, so other vendors' switches see one tree.
A few trees for many VLANs: MSTP
The IEEE's answer is Multiple Spanning Tree Protocol, 802.1s, published in 2002 and merged into 802.1Q in 2005. Most networks only need a handful of different tree shapes, so MSTP runs a small number of instances and maps each VLAN to one of them. Our house needs just two:
| Instance | VLANs | Root |
|---|---|---|
| 0 (the IST) | everything not mapped elsewhere | A |
| 1 | 10, 30 | A |
| 2 | 20 | B |
All the instances travel in a single BPDU per port (version 3), with a short record per instance, so the overhead doesn't grow with the number of VLANs. A group of switches that agree on the mapping form an MST region; they check by comparing a region name, a revision number and a digest of the VLAN-to-instance table carried in the BPDU. A region can hold up to 64 instances besides instance 0, and from outside, the whole region looks like a single big switch to its RSTP or STP neighbours. Get the mapping wrong on one switch and it quietly becomes its own region, which is the classic MSTP misconfiguration.
Protecting the Tree
Spanning tree trusts every BPDU it receives. That's what makes it zero-configuration, and it's also a weakness: any device that sends BPDUs takes part in the election. A switch somebody brings from home, a misconfigured virtual machine bridge, or a deliberately crafted BPDU with priority 0 can claim to be the root, and the whole tree reshapes around it: traffic is pulled through the wrong place, and every change triggers another round of reconvergence and MAC flushing. Accidental loops are even more common: a cable from one wall socket to another, or a cheap unmanaged switch with both ends plugged back into the network. Managed switches have a small set of guards for all of this; the names here are Cisco's, and other vendors offer the same ideas under other names.
| Feature | Put it on | When it fires |
|---|---|---|
| BPDU Guard | Edge ports (hosts) | Any BPDU arrives: the port is shut down (err-disabled) |
| Root Guard | Designated ports facing switches that must never be root | A superior BPDU arrives: the port blocks until it stops |
| Loop Guard | Root and alternate ports | BPDUs stop arriving: the port blocks instead of forwarding |
BPDU Guard follows from the edge-port idea: a port configured for a laptop has no business hearing a BPDU, so if it does, something is wrong, and the safest response is to shut the port down until an administrator looks. It also catches the "both ends of a cable in the same switch" loop, since the port hears its own BPDU come back.
Root Guard protects your choice of root. On A's ports 6 and 8, a superior BPDU, one claiming a better root than A, would mean somebody downstream is trying to take over. Root guard puts the port into a "root-inconsistent" blocked state for as long as those BPDUs keep coming, and lets it recover automatically when they stop.
Loop Guard protects against the opposite failure: silence. Suppose the fibre from B to C breaks in one direction only, so C stops hearing B's BPDUs while B can still hear C. After max age, or three missed hellos in RSTP, C's port 2 would conclude that nobody is serving the link, become designated, and start forwarding into a loop. Loop guard says a root or alternate port that stops hearing BPDUs should block ("loop-inconsistent") rather than promote itself. Cisco's UDLD attacks the same one-way-link problem from a different angle.
For the house, that adds up to:
Guarding switch A
interface range GigabitEthernet0/1 - 3 ! laptop, NAS, printer
spanning-tree portfast
spanning-tree bpduguard enable
!
interface GigabitEthernet0/6 ! to C
spanning-tree guard root
interface GigabitEthernet0/8 ! to B
spanning-tree guard root
!
interface GigabitEthernet0/7 ! trunk to the router
spanning-tree portfast trunk
And on B and C, spanning-tree guard loop on their uplinks (B's port 8,
C's ports 1 and 2). A's port 7, to the router, is made an edge port too. The router doesn't bridge,
so it can't form a loop. Left as an ordinary port, it would end up designated, but
with no RSTP neighbour to answer a proposal, it would take two forward delays (30 s)
to start forwarding. Because it's a trunk, Cisco needs the trunk keyword
(portfast edge trunk on newer software). The simulator above treats every
host-facing port, including A7, as an edge port in the same way.
There's also a BPDU filter, which stops a port sending or processing BPDUs at all. It has legitimate uses at the boundary between two organisations' networks, but on an ordinary port it simply switches loop protection off, so treat it with suspicion. And many switches offer storm control, a rate limit on broadcast and multicast per port, as a last line of defence if a loop happens anyway.
Beyond Spanning Tree
Link aggregation: more than one cable, one link
Remember the second cable between A and B, where spanning tree blocked one of the pair? Link aggregation is the better answer. IEEE 802.3ad (2000), now maintained as 802.1AX, bundles several parallel links into one logical link, and its LACP protocol lets the two switches agree which ports belong to the bundle. Spanning tree sees the bundle as one port with one port ID, so there's nothing for it to block; inside the bundle, each flow is hashed (by MAC or IP addresses and ports) onto one member, so both cables carry traffic and a single member failing just shrinks the bundle. Aggregation and spanning tree are complementary: aggregation handles parallel links between two switches, spanning tree handles loops through several.
Why data centres moved on
Spanning tree's basic bargain, a tree that uses only n − 1 of the links, is fine for a house and tolerable for an office. In a data centre built for bandwidth, idling half the links is unacceptable, and a broadcast storm or a slow reconvergence in a network carrying thousands of servers is a disaster. Two approaches took over:
- Fix Layer 2 with routing ideas. TRILL (RFC 6325, 2011), which Perlman herself helped design, and IEEE Shortest Path Bridging (802.1aq, 2012) both run the link-state routing protocol IS-IS between switches, so frames take shortest paths over all links. TRILL even adds a hop count to its header, so a looping frame eventually dies, just like an IP packet.
- Don't stretch Layer 2 at all. Most modern data centres use a leaf-spine topology: every leaf switch (top of rack) connects to every spine switch, and the links between them are routed, not switched. Routing protocols happily use many equal-cost paths at once (ECMP), and IP has a TTL. Where tenants still need a shared Layer 2 segment, it's carried over the routed fabric by an overlay like VXLAN.
Spanning tree hasn't gone anywhere, though. It still runs on nearly every campus and office switch, quietly protecting against the loop someone will eventually plug in, and in RSTP or MSTP form it converges fast enough that most people never notice it working. The ideas that replaced it in the data centre, shortest paths, link states, hop counts, are borrowed from routing, which is where this series goes next.
Where to Go Next
- Routing Basics: what happens one layer up, where loops are handled by TTLs and routing tables, and packets cross between networks.
- Link-State Routing: the flooding-and-Dijkstra approach behind IS-IS, and therefore behind TRILL and Shortest Path Bridging.
- VLANs: the trunks spanning tree protects, and why per-VLAN trees need the extended system ID.
- Ethernet & Switching: the learning switch whose flooding makes loops so dangerous.
- ARP: one of the broadcasts that a loop multiplies.
Key Takeaways
- Redundant links between switches form loops. Ethernet has no TTL, so broadcasts circulate forever (and multiply wherever a switch has three or more loop ports), MAC tables flap, and hosts get duplicates: a broadcast storm.
- Spanning tree (Perlman, DEC, 1985; IEEE 802.1D) keeps every cable plugged in but blocks just enough ports that the active links form a tree, and recomputes when links fail.
- Switches exchange BPDUs, sent to
01:80:c2:00:00:00with LLC header42 42 03, carrying root bridge ID, root path cost, sender bridge ID, sender port ID and timers. - A bridge ID is a priority plus a MAC. With the extended system ID the priority is 4 bits (multiples of 4096, default 32768) plus a 12-bit VLAN or instance number.
- Lowest bridge ID becomes root. Each other switch picks one root port by lowest root path cost, then lowest sender bridge ID, sender port ID and own port ID. Each link gets one designated port; every other port blocks.
- Path costs come in two tables: short (1 Gb/s = 4) and long (1 Gb/s = 20,000). In our triangle, A is root and C's port 2 blocks, decided by B's lower MAC.
- Classic STP uses blocking, listening, learning and forwarding with hello 2 s, max age 20 s and forward delay 15 s: 30 s to forward after power-up or a direct failure, about 50 s after an indirect one. TCNs make switches age MAC entries in 15 s instead of 300.
- RSTP (802.1w, now 802.1D-2004) adds alternate and backup roles, discarding/learning/forwarding states, edge ports, and the proposal/agreement handshake, cutting convergence to well under a second.
- PVST+ and Rapid PVST+ (Cisco) run one tree per VLAN; MSTP (802.1s) maps many VLANs onto a few instances. Both let different VLANs use different links.
- BPDU Guard, Root Guard and Loop Guard protect the tree from rogue BPDUs, unexpected roots and one-way links.
- Link aggregation (LACP) turns parallel cables into one link STP doesn't need to block; data centres replaced STP with routed leaf-spine fabrics, ECMP, TRILL or SPB.
Spanning tree keeps one network loop-free by making it a tree. Between networks, loops are allowed, and paths can be as tangled as the internet itself, because IP packets carry a TTL and routers choose a next hop for every destination. Next up: Routing Basics, and the routing table that makes those choices.