Loops and bonded links
Spanning tree
The protocol that lets switches be cabled in loops by electing a root and blocking the ports that close a loop.
The problem
Switches cabled in a loop for a spare way flood broadcasts round it for ever, unless one port stops passing frames.
- coreSwitch, floorA and floorB send each other BPDUs, small frames to
01:80:c2:00:00:00, every hello time. - The lowest bridge ID (a priority, then the MAC) becomes root: coreSwitch, priority 4096 (
1000in hex). - Every other switch keeps its root port, its cheapest way to the root: floorB's is the cable to coreSwitch, cost 100.
- On the floorA–floorB cable, floorA is nearer the root, so its end forwards and floorB's end blocks. The loop is cut.
- When the floorA–coreSwitch cable is pulled, floorB's blocked port goes listening, learning, then forwarding: about 10 s here.
What you'd see
$ bridge link show
14: floorA@if15: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br0 state blocking priority 32 cost 100
17: coreSwitch@if16: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br0 state forwarding priority 32 cost 100
20: bob@if21: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br0 state forwarding priority 32 cost 2
- Run on floorB. Each port is named after what is at the other end of its cable.
floorA … state blocking: the port that would close the loop. It passes no frames, but still hears BPDUs.coreSwitch … state forwarding: floorB's root port, its way to the root.cost 100: what each cable between switches adds to the path cost.
Guess first
floorB's priority is set to 0, the lowest there is. Which switch is root now?
Show answer
floorB: the lowest bridge ID wins, and priority comes before the MAC. The tree is rebuilt around it, so a different port blocks.
In short
Switches exchange BPDUs (to 01:80:c2:00:00:00, every hello time) and elect as root the one with the
lowest bridge ID: a priority (default 32768), then its MAC address. Each other switch keeps its root port, the
one with the cheapest total path cost to the root; each cable gets one designated port, the end nearer the
root; every other port blocks. A blocked port still hears BPDUs, and takes over when a cable fails, after the max
age and two forward delays (30 to 50 seconds with standard timers). Rapid spanning tree (802.1w) does it in about
a second. Choose the root with its priority, or the tree will be built around whichever switch has the lowest MAC.
How spanning tree works, step by step
Ethernet frames have no hop limit. Cable three switches in a triangle and a single broadcast is flooded round the loop for ever, copied again at every switch: a broadcast storm. Spanning tree (IEEE 802.1D) keeps the spare cables but uses none of them, by building a tree in four steps.
- Elect a root. Every switch has a bridge ID: a 2-byte priority (default 32768, set in steps of 4096) and
its MAC address. Each starts out claiming to be root, sending BPDUs to the multicast MAC
01:80:c2:00:00:00. A switch that hears a lower bridge ID stops claiming and passes that one on. The lowest wins: priority first, the MAC only breaks a tie. - Pick a root port on every other switch. It is the port with the lowest total path cost to the root. Each link adds its port's cost; the 802.1D-1998 values are 100 for 10 Mb/s, 19 for 100 Mb/s, 4 for 1 Gb/s and 2 for 10 Gb/s. Ties go to the lower sender bridge ID, then the lower sender port ID.
- Pick one designated port per cable: the end nearer the root, which forwards onto that cable. Ties go to the lower bridge ID. Every port on the root is designated.
- Block everything else. A blocked port drops data frames and learns no MAC addresses, but keeps listening to BPDUs.
The root sends a BPDU every hello time (2 seconds). Each one carries the root ID, the sender's cost to the root, the sender's own ID and port, and the timers. When a blocked port stops hearing the BPDUs that justified blocking it, for the max age (20 seconds), it moves through listening and learning, each lasting the forward delay (15 seconds), and then forwards. So classic STP takes 30 to 50 seconds to recover. Rapid spanning tree (802.1w) replaces the waiting with a proposal and agreement between neighbours, and an alternate port already chosen in advance: about a second.
What it looks like on a Linux machine
A Linux bridge runs 802.1D spanning tree. In the spanning tree lab three bridges form a ring, with valleySwitch at priority 4096. Each port is named after what's at its other end:
$ bridge link show
3: valleySwitch@if4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br0 state forwarding priority 32 cost 100
5: peakSwitch@if6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br0 state blocking priority 32 cost 100
That is midSwitch: its cable to peakSwitch is on nobody's cheapest path, and midSwitch, the higher bridge ID, blocks its end.
ip -d link show br0 prints the election from each switch's point of view, as designated_root 1000.…
(priority 4096 in hex, then the MAC), root_port and root_path_cost 100. A capture shows the BPDUs:
$ tcpdump -vnni eth0 ether dst 01:80:c2:00:00:00
STP 802.1d, Config, Flags [none], bridge-id 8000.d2:65:0f:8e:21:b3.8002, length 35
message-age 1.00s, max-age 20.00s, hello-time 2.00s, forwarding-delay 15.00s
root-id 1000.9e:3b:52:c1:07:4a, root-pathcost 100
The lab shortens the timers so you can watch a cable fail: midSwitch's port stays blocking until the max age runs
out, then goes listening, learning and forwarding, while pings stop and start again.
What goes wrong, and how you see it
No spanning tree on a looped network. One ARP request becomes hundreds of frames a second, MAC tables flap as the same address arrives on different ports, and CPUs and links fill up. The lab switches STP off to show it. A storm does not stop by itself; pulling a cable does.
The wrong root. Left at default priorities, the oldest switch, with the lowest MAC, often becomes root, and traffic crosses a closet switch instead of the core. Set the root's priority on purpose, and a backup root just above it.
A host port that waits. A PC plugged into a classic STP port waits 30 seconds before forwarding, long enough for DHCP to give up. Edge ports (PortFast) skip listening and learning; BPDU guard shuts one that hears a BPDU.
A slow failover. Recovery that takes 30 to 50 seconds means classic STP is running. RSTP, or its per-VLAN variants, cut it to about a second.
Questions people ask
Why not just remove the extra cables? Because they are the backup. Spanning tree leaves them connected and unused until a working link fails.
What is the difference between STP and RSTP? Same election, faster recovery. RSTP has three port states (discarding, learning, forwarding) instead of five, alternate ports that take over at once, and handshakes instead of timers.
Does link aggregation need spanning tree? No. A bond of several cables counts as one link, so STP sees no loop in it and blocks none of its members.
Read next
- Broadcast storm A broadcast copied round a loop of switches for ever, because Ethernet frames have no hop limit.
- Switch Joins devices into one network, forwarding each frame toward the port its destination MAC was seen on.
- MAC address table A switch's record of which port each MAC address was last seen on, learned from source addresses.
- Edge port A spanning-tree port that faces a single host, so it forwards the instant the link is up — plus the guards that protect it.
- Link aggregation Several cables between the same two devices joined into one logical link, with one MAC and one address.