BigJames.netHam Radio and Tech fun
Networking,  Technology

Spanning Tree Protocol: A Practical Learning Guide

Date Published

Spanning Tree Protocol (STP) is one of those foundational networking concepts that every network engineer encounters early and keeps running into forever. It exists to solve a specific, nasty problem: Ethernet loops. Once you understand the problem it solves and the mechanism it uses to solve it, the rest of the protocol clicks into place. This document covers STP from first principles through the modern variants you'll actually encounter in production.

The Problem: Why Loops Are Catastrophic

Ethernet switches forward frames based on MAC address tables. When a frame arrives for an unknown destination, the switch floods it out every port except the one it came in on. That behavior is intentional and necessary — but add a physical loop to the topology and it turns lethal. A flooded frame circulates the loop indefinitely, cloning itself at each switch. Within milliseconds you have a broadcast storm consuming 100% of link bandwidth. MAC address tables flip-flop as the same source MAC appears on different ports in rapid succession. The network collapses. STP's job is to break that loop before it can happen, by selectively blocking one or more ports to create a loop-free logical topology while keeping the physical redundant links available as backup.

How STP Works

Root Bridge Election

Every STP-enabled switch starts by assuming it is the root bridge and advertising that belief via Bridge Protocol Data Units (BPDUs). BPDUs carry a Bridge ID (BID), which is a combination of a configurable priority value (default 32768) and the switch's MAC address. Switches exchange BPDUs and the switch with the lowest BID wins the root bridge election. In practice this means: set the priority explicitly on the switch you want to be root — don't rely on MAC addresses to decide it for you.

Port Roles

Once the root bridge is elected, every other switch determines the best path back to it. Each non-root switch selects one Root Port — the port with the lowest-cost path to the root bridge. On each network segment, one switch is elected the Designated Switch and its port toward that segment becomes the Designated Port. All other ports that would create a loop become Non-Designated Ports and are placed in a blocking state. The root bridge's ports are always Designated Ports. Path cost is based on link speed: a 10 Gbps link has a lower cost than a 100 Mbps link.

Port States and Convergence

Classic STP (802.1D) moves ports through five states: Disabled (administratively off), Blocking (receiving BPDUs, not forwarding traffic), Listening (participating in the active topology election, not forwarding), Learning (building the MAC table, not forwarding), and Forwarding (fully operational). Ports spend 15 seconds each in the Listening and Learning states — the Forward Delay timer — meaning a newly connected link takes 30 seconds before it passes traffic. This slow convergence is STP's biggest practical pain point, and the reason RSTP exists.

RSTP: The One You Should Actually Be Running

Rapid Spanning Tree Protocol (802.1w, now folded into 802.1D-2004) shrinks convergence from 30+ seconds to under a second in most topologies. It achieves this by replacing the timer-based state machine with a negotiation handshake between directly connected switches. RSTP collapses the five port states into three: Discarding (covers Disabled/Blocking/Listening), Learning, and Forwarding. It also adds new port roles: Alternate Port (a backup root port, pre-blocked and ready to take over immediately) and Backup Port (a backup designated port on a shared segment). When a link fails, the Alternate Port can transition directly to Forwarding without waiting for timers. RSTP is backwards-compatible with STP — a port connected to an STP switch falls back to classic behavior automatically.

MSTP: Multiple Instances for VLANs

Multiple Spanning Tree Protocol (802.1s) addresses a problem that arises with VLANs: in a standard STP or RSTP environment, all VLANs share a single spanning tree topology. That means redundant links are blocked for every VLAN even if some VLANs could use those links without conflict. MSTP lets you group VLANs into instances, each with its own spanning tree. VLANs 1-50 might use Instance 1 with Switch A as root, while VLANs 51-100 use Instance 2 with Switch B as root — giving you load balancing across redundant links. MSTP runs inside a region defined by a region name, revision number, and VLAN-to-instance mapping. Cisco's Rapid Per-VLAN Spanning Tree (RPVST+) is an alternative that runs a separate RSTP instance per VLAN — simpler to configure but heavier on CPU and memory in large deployments.

Practical Configuration Tips

Set Your Root Bridge Explicitly

Never leave root bridge election to chance. Use 'spanning-tree vlan <id> priority 4096' (or the 'root primary' macro) on your intended root, and 'priority 8192' on your secondary. If a new switch with a factory-default priority and a low MAC address joins the network, you don't want it winning the election and redirecting traffic through an access layer device.

PortFast and BPDU Guard on Access Ports

PortFast skips the Listening and Learning states on ports that connect to end devices (PCs, servers, phones), letting them reach Forwarding immediately. This prevents the annoying 30-second wait after a device connects or reboots. Enable it only on ports that will never connect to another switch. Pair it with BPDU Guard, which shuts down the port immediately if a BPDU is received on it — a hard protection against someone accidentally plugging in an unmanaged switch or a rogue device that starts participating in STP.

Root Guard and Loop Guard

Root Guard prevents a port from ever becoming a root port — use it on ports where you know no switch superior to your root bridge should ever appear. If a BPDU arrives claiming a better root, Root Guard puts the port into a 'root-inconsistent' blocking state instead of accepting the new root. Loop Guard protects against unidirectional link failures where a port stops receiving BPDUs but the link appears physically up. Without Loop Guard, the port would eventually transition to Forwarding and create a loop. With it enabled, the port moves to a loop-inconsistent blocking state and recovers automatically when BPDUs resume.

Quick Reference: STP vs RSTP vs MSTP

STP (802.1D): Original standard, 30-50 second convergence, one tree for all VLANs. Use only if forced by legacy hardware. RSTP (802.1w / 802.1D-2004): Sub-second convergence, same single-tree limitation, backwards compatible with STP. Use this as your baseline on any modern network. MSTP (802.1s): Multiple trees mapped to VLAN groups, load balancing across redundant links, most efficient at scale. Use this on networks with significant VLAN counts where link utilization across redundant paths matters. RPVST+ (Cisco proprietary): One RSTP instance per VLAN, easy to understand, higher overhead — fine for moderate VLAN counts in all-Cisco environments.