Spanning Tree and Loop Prevention
Redundant switch links create loops, and Ethernet frames never expire. Learn broadcast storms, how the Spanning Tree Protocol elects a root bridge and blocks redundant links, port roles and states, costs, RSTP and PVST, PortFast and BPDU Guard, EtherChannel, and when to avoid STP altogether. Then elect a root and compute path costs.
1. The problem: loops
Good network designs connect each access switch to two upstream switches so a cable or switch failure does not cut anyone off. But a layer 2 network with redundant links forms a loop, and Ethernet has a nasty property: frames have no time-to-live. A broadcast frame entering a loop is flooded out of every port, comes back around, and is flooded again, forever. Each pass multiplies the copies. Within seconds the links are saturated and switch CPUs are busy, so legitimate traffic stops. This is a broadcast storm. Loops also cause MAC table instability (the same source MAC seen on different ports) and duplicate frames.
The Spanning Tree Protocol (STP, IEEE 802.1D) solves it: switches automatically agree on a loop-free tree by blocking redundant links, and unblock them if an active link fails.
2. How STP builds the tree
- Elect a root bridge. Every switch has a Bridge ID = priority (default 32768, in steps of 4096) followed by its MAC address. Switches exchange BPDUs (bridge protocol data units); the lowest Bridge ID wins. With equal priorities the lowest MAC address wins, which is usually the oldest switch, rarely the one you want.
- Each non-root switch picks a root port: the port with the lowest total path cost to the root. Ties break on the lowest sender bridge ID, then lowest port ID.
- Each network segment picks a designated port: the port on that segment with the best path to the root, which forwards traffic towards it.
- Everything else is blocked (alternate or non-designated ports): they listen for BPDUs but do not forward data.
The result is one active path between any two switches, rooted at the root bridge.
Costs
Cost depends on link speed (classic short values): 10 Mbps = 100, 100 Mbps = 19, 1 Gbps = 4, 10 Gbps = 2. Newer “long” costs scale to higher speeds (for example 20,000,000,000 divided by the speed in kbps). A path cost is the sum of the costs of the links towards the root.
Port states and roles
Classic STP states: blocking, listening, learning, forwarding (and disabled). Moving from blocking to forwarding takes about 30 to 50 seconds (max age 20 s, plus forward delay 15 s twice), which is far too slow for modern networks.
Rapid STP uses states discarding, learning, forwarding and roles root, designated, alternate (a backup path to the root) and backup.
3. Variants
| Protocol | Standard | Notes |
|---|---|---|
| STP | 802.1D | One tree for all VLANs; slow convergence |
| PVST+ / Rapid PVST+ | Cisco | One tree per VLAN, allows load sharing by choosing different roots per VLAN |
| RSTP | 802.1w | Fast convergence (about 1 second) |
| MSTP | 802.1s | Maps groups of VLANs onto a few instances; scalable |
Most modern networks run RSTP or Rapid PVST+.
4. Design and protection
- Choose the root deliberately: set a low priority on the core/distribution switch (
spanning-tree vlan 10 priority 4096orroot primary) and a slightly higher one on its backup, so the tree is predictable and the root is in the middle of the network. - PortFast (edge port) on ports facing PCs and servers: they go straight to forwarding, skipping the listening and learning delay. Never enable it on links to other switches.
- BPDU Guard on edge ports: if a BPDU arrives (someone plugged in a switch), the port shuts down.
- Root Guard on ports that must never lead to the root, preventing a rogue switch with a low priority from taking over.
- Loop Guard / UDLD protect against unidirectional links that stop BPDUs arriving.
- Storm control limits broadcast, multicast and unknown-unicast traffic as a last defence.
- Keep the topology simple: avoid daisy-chaining many switches, which makes long paths and slow, confusing trees.
5. EtherChannel and alternatives to blocking
STP wastes redundant links by blocking them. Ways to use all links:
- EtherChannel / link aggregation (LACP, 802.3ad) bundles several physical links into one logical link, so STP sees a single link and all members forward traffic (load balanced by hash).
- Multi-chassis link aggregation (MLAG, vPC, stacking) lets a downstream switch connect to two upstream switches with one logical bundle, with no blocked ports.
- Routed access / layer 3 to the access layer: routing instead of switching removes loops altogether and uses ECMP.
- Data-centre fabrics use VXLAN/EVPN over routed underlays, with no spanning tree between leaves.
6. Troubleshooting
Symptoms of a loop: very high CPU on switches, links at 100 percent with blinking lights, MAC flapping messages, users losing connectivity everywhere. Steps:
- Find the loop: look at port utilisation and the counters that jump fastest; unplug suspect links or shut ports until the storm stops.
show spanning-tree(root bridge, root port, which ports are forwarding or blocking),show spanning-tree inconsistentports, and logs for BPDU Guard or loop messages.- Check for a rogue switch, unmanaged switches (which do not run STP) and wrongly cabled patch panels.
- After recovery, fix the cause (enable BPDU Guard and storm control, correct the root placement).
7. Hands-on: root election and path costs
elect_root(bridges): each bridge is{"name", "priority", "mac"}. Return the name of the bridge with the lowest priority, breaking ties with the lowest MAC address (compare the text; the addresses use the same format).root_path_costs(links, root):linksare(switch_a, switch_b, cost). Return a dictionary of the lowest total path cost from each switch to the root (root costs 0).stp_cost(speed_mbps): 10 gives 100, 100 gives 19, 1000 gives 4, 10000 gives 2.blocked_link_count(total_links, switches): a loop-free tree hasswitches - 1links, so the rest are blocked.
A user plugs a cheap unmanaged switch into two wall sockets in a meeting room, and the whole building loses connectivity within a minute. What happened and what stops it happening again?
The unmanaged switch has no STP, so it forwarded broadcasts between the two ports and created a loop that the real switches saw as a storm. Prevent it with BPDU Guard and PortFast on edge ports (a BPDU from a managed switch shuts the port), storm control to limit broadcast traffic, and port security or 802.1X to control what may connect. Find the loop by looking at the busiest ports and unplugging the offending cable.
Can You Explain It?
Close the lesson and answer these without looking back. The goal isn't the wording — it's whether you've formed a usable mental model.
- Explain why a loop causes a broadcast storm and how STP prevents it.
- Describe how the root bridge is elected and how to make a chosen switch the root.
- List three protections you would configure on access ports and what each does.
8. Key takeaways
- Redundant layer 2 links create loops and broadcast storms; Ethernet frames have no TTL.
- STP elects a root bridge (lowest bridge ID), picks root ports by lowest path cost, and blocks the rest.
- Use Rapid PVST+/RSTP for fast convergence, and set the root deliberately.
- Protect edges with PortFast, BPDU Guard and storm control; use Root Guard where the root must not move.
- Bundle links with EtherChannel/MLAG, or route at the access layer, to use all your capacity.
🧪 Want to practise this on your own computer? See how to set up your lab →