Spanning Tree Protocol (STP)
Purpose & Layer 2 Loops
- Concept: Network redundancy is essential for 24/7 uptime, but creating multiple paths at Layer 2 introduces the risk of Layer 2 loops.
- The Problem: Ethernet frames do not have a Time to Live (TTL) field like IP packets. If a loop exists, broadcast and unknown unicast frames will loop indefinitely.
- Consequences of Loops:
- Broadcast Storm: Network becomes completely congested with looping broadcasts, dropping legitimate traffic.
- MAC Address Flapping: Switches continuously update their MAC address tables as the same source MAC arrives on different interfaces, corrupting the table.
- The Solution: Spanning Tree Protocol (STP) (IEEE 802.1D) prevents loops by logically disabling redundant paths, placing them in a blocking state. If an active path fails, STP recalculates and transitions the blocked port to a forwarding state.
Bridge ID & Root Bridge Election
- Concept: STP elects a single Root Bridge (the logical center of the network). All switches use Bridge Protocol Data Units (BPDUs) to communicate and elect the root.
- Bridge ID (BID): An 8-byte field used to determine the root. The switch with the lowest BID wins.
- Traditional BID: 2 bytes Bridge Priority + 6 bytes MAC Address.
- Extended BID (PVST+): 4 bits Bridge Priority + 12 bits Extended System ID (VLAN ID) + 6 bytes MAC Address.
- Default Values:
- Default Bridge Priority:
32768. - Because the Extended System ID adds the VLAN ID (default VLAN 1), the actual default priority for VLAN 1 is 32769 (32768 + 1).
- Bridge priority can only be changed in increments of 4096.
- Default Bridge Priority:
Things to note: When a switch powers on, it assumes it is the root bridge and sends BPDUs. It only yields if it receives a “superior” BPDU (lower BID). Once converged, only the Root Bridge generates BPDUs; all other switches merely forward them.
Port Roles & Selection Process
STP assigns specific roles to switch ports to build a loop-free topology.
| Port Role | Quantity | Description | State |
|---|---|---|---|
| Root Port (RP) | 1 per non-root switch | The port with the lowest cost path to the Root Bridge. | Forwarding |
| Designated Port (DP) | 1 per collision domain | The port on a segment closest to the Root Bridge. Forwards BPDUs down the tree. | Forwarding |
| Non-Designated Port | 0 or more per switch | Redundant ports that do not meet RP or DP criteria. Prevents loops. | Blocking |
Root Port Selection Tiebreakers
If a switch has multiple paths to the Root Bridge, it selects the Root Port using these sequential tiebreakers:
- Lowest Root Path Cost: Cumulative cost of all outgoing interfaces to the Root Bridge.
- Lowest Sender Bridge ID: The BID of the neighboring switch that sent the BPDU.
- Lowest Sender Port ID: The Port ID (Port Priority + Port Number) of the neighboring switch’s interface.
Designated Port Selection Tiebreakers
If two switches are connected to the same segment, they must elect one Designated Port:
- Lowest Root Path Cost: The switch with the lowest total cost to the Root Bridge wins.
- Lowest Bridge ID: If costs tie, the switch with the lowest local BID wins. (The losing port becomes a Non-Designated/Blocking port).
STP Path Costs
| Link Speed | STP Cost |
|---|---|
| 10 Mbps | 100 |
| 100 Mbps (FastEthernet) | 19 |
| 1 Gbps (GigabitEthernet) | 4 |
| 10 Gbps | 2 |
Port States & Timers
Ports transition through specific states to prevent accidental loops during topology changes.
| State | Duration | Forwards Traffic? | Learns MACs? | Processes BPDUs? |
|---|---|---|---|---|
| Disabled | N/A | No | No | No (Administratively down) |
| Blocking | Indefinite | No | No | Receives only (Does not forward) |
| Listening | 15 seconds | No | No | Sends/Receives |
| Learning | 15 seconds | No | Yes | Sends/Receives |
| Forwarding | Indefinite | Yes | Yes | Sends/Receives |
stateDiagram-v2
[*] --> Disabled : Admin Down
Disabled --> Blocking : No Shutdown
Blocking --> Listening : Topology Change / Init
Listening --> Learning : 15s (Forward Delay)
Learning --> Forwarding : 15s (Forward Delay)
Forwarding --> Blocking : Topology Change
Listening --> Blocking : Topology Change
STP Timers
- Hello Timer (2 seconds): How often the Root Bridge sends Configuration BPDUs.
- Forward Delay Timer (15 seconds): Time spent in Listening and Learning states. (Total 30 seconds).
- Max Age Timer (20 seconds): How long a switch waits without receiving BPDUs before assuming the topology has changed and recalculating.
- Total Convergence Time: A blocked port takes 50 seconds to transition to forwarding (20s Max Age + 15s Listening + 15s Learning).
Things to note: Timers are set by the Root Bridge and pushed to all other switches via BPDUs. Changing timers on a non-root switch has no effect.
BPDU Details
- PVST+ Destination MAC:
0100.0ccc.cccd(Cisco proprietary, runs per-VLAN, supports 802.1Q). - Standard 802.1D Destination MAC:
0180.c200.0000. - BPDU Fields: Protocol ID (0000), Version (0 for classic), Type (00 for Config BPDU), Root ID, Root Path Cost, Bridge ID, Port ID, and Timers.
- Port ID: Consists of Port Priority (default 128) and Port Number.
Optional Features (STP Toolkit)
Features designed to optimize STP and protect the network from user errors.
- PortFast: Bypasses Listening and Learning states, moving directly to Forwarding.
- Rule: Only enable on ports connected to end hosts (PCs, servers). Enabling on a switch port can cause a loop.
- Global Command:
spanning-tree portfast defaultenables it on all access ports (not trunk ports).
- BPDU Guard: Shuts down the port (err-disabled) if a BPDU is received. Protects PortFast-enabled edge ports from unauthorized switches.
- Root Guard: Prevents a port from becoming a Root Port if it receives a superior BPDU. Maintains the intended Root Bridge placement.
- Loop Guard: Prevents alternate/root ports from transitioning to forwarding if BPDUs stop being received (prevents loops caused by unidirectional link failures).
STP Configuration
Configuring the Root Bridge
Instead of manually calculating priorities in increments of 4096, use the macro commands:
Switch(config)# spanning-tree vlan 1 root primary
Sets priority to 24576, or 4096 less than the current lowest priority in the network.
Switch(config)# spanning-tree vlan 1 root secondary
Sets priority to 28672 (acts as the backup root).
Load Balancing with PVST+
By making different switches the Root Bridge for different VLANs, you can block different links per VLAN, utilizing all physical links.
! On SW1: Primary for VLAN 10, Secondary for VLAN 20
Switch(config)# spanning-tree vlan 10 root primary
Switch(config)# spanning-tree vlan 20 root secondary
! On SW2: Primary for VLAN 20, Secondary for VLAN 10
Switch(config)# spanning-tree vlan 20 root primary
Switch(config)# spanning-tree vlan 10 root secondary
Modifying Interface Parameters
You can manipulate the tiebreakers by changing the Cost or Port Priority on a per-VLAN basis.
Switch(config-if)# spanning-tree vlan 1 cost 19
Switch(config-if)# spanning-tree vlan 1 port-priority 64
Note: Port priority must be configured in increments of 32 (0, 32, 64… 224).
Enabling Edge Port Protections
! Interface level
Switch(config-if)# spanning-tree portfast
Switch(config-if)# spanning-tree bpduguard enable
! Global level (Applies to all access ports)
Switch(config)# spanning-tree portfast default
Switch(config)# spanning-tree portfast bpduguard default
Things to note: If a port is shut down by BPDU Guard, you must manually recover it by issuing shutdown followed by no shutdown on the interface, after removing the offending device.
Complete Command Summary
! --- STP Mode Configuration ---
Switch(config)# spanning-tree mode pvst
Switch(config)# spanning-tree mode rapid-pvst
! --- Root Bridge Election ---
Switch(config)# spanning-tree vlan [vlan_id] root primary
Switch(config)# spanning-tree vlan [vlan_id] root secondary
Switch(config)# spanning-tree vlan [vlan_id] priority [priority_value]
! --- Interface STP Parameters ---
Switch(config-if)# spanning-tree vlan [vlan_id] cost [1-200000000]
Switch(config-if)# spanning-tree vlan [vlan_id] port-priority [0-224 in increments of 32]
! --- PortFast Configuration ---
Switch(config-if)# spanning-tree portfast
Switch(config)# spanning-tree portfast default
! --- BPDU Guard Configuration ---
Switch(config-if)# spanning-tree bpduguard enable
Switch(config)# spanning-tree portfast bpduguard default
! --- Interface Recovery ---
Switch(config-if)# shutdown
Switch(config-if)# no shutdown