Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 64

STP: Preventing Loops and Understanding Port States

Recommended resources

Hello! In our last lesson, we focused on Network Address Translation (NAT) at Layer 3, configuring a Linux router to connect a private local network to the public internet. This ensures devices can talk to the outside world. Now, we'll turn our attention to making that local network robust and reliable at Layer 2.

To ensure high availability, network designers often build redundant paths between switches. However, without a control mechanism, this simple act of adding a backup cable can create a catastrophic loop, bringing the entire network to a halt.

This lesson tackles that exact problem. We will investigate the Spanning Tree Protocol (STP). Your learning outcome is to describe how Spanning Tree Protocol (STP) prevents switching loops and identify its effect on port states. Understanding this is non-negotiable for anyone managing network infrastructure, and it's a concept you'll frequently explain in technical interviews.

1. The Problem: Why Redundancy Can Be Dangerous

At Layer 3, protocols like OSPF have built-in mechanisms to handle multiple paths. However, at Layer 2, Ethernet switches operate differently. By default, they flood broadcast frames out of all ports. If you create a loop by connecting switches with redundant links, this behavior becomes destructive.

This leads to three critical issues:

  1. Broadcast Storms: A single broadcast frame gets duplicated and forwarded endlessly between the looped switches, consuming all available bandwidth and overwhelming switch CPUs.
  2. MAC Address Table Instability: A switch sees the same device MAC address arriving on multiple ports, causing it to constantly update its MAC table. This "MAC flapping" makes it impossible to forward unicast frames correctly.
  3. Duplicate Frame Transmission: A single unicast frame can be delivered to the destination host multiple times, causing errors in applications.

To see these issues in action, let's watch a clear visualization of what happens in a looped Layer 2 network without STP.

Spanning Tree Protocol Explained | Step by Step

This video from CertBros provides an excellent animation of how a simple redundant topology creates broadcast storms, MAC table instability, and duplicate frames.

Watch from 02:00 to 05:14. Focus on understanding the three distinct problems that arise from a Layer 2 loop. This is the 'why' behind STP's existence.

As the video demonstrates, the solution is to logically block a port on one of the redundant links to break the loop. The Spanning Tree Protocol is the automated process that switches use to do this safely and dynamically.


2. The Solution: How STP Creates a Loop-Free Path

STP's goal is to create a single, logical path through a network of switches, resulting in a tree-like topology (a "spanning tree") with no loops. It does this by electing a central reference point and then calculating the best path from every switch to that point. Any links that are not part of this best path are put into a blocking state.

The entire process can be broken down into three main steps, which are orchestrated by switches exchanging special messages called Bridge Protocol Data Units (BPDUs).

Step 1: Elect One Root Bridge

The first step is for all switches to elect a single Root Bridge. This switch becomes the logical center of the spanning tree. The election is won by the switch with the lowest Bridge ID (BID).

The BID is composed of two parts:

  • Bridge Priority: A configurable value (default is 32768).
  • MAC Address: The switch's unique hardware address.

The switch with the lowest priority becomes the root. If priorities are tied (as they are by default), the switch with the lowest MAC address wins. All ports on the Root Bridge are placed in a forwarding state.

Step 2: Elect Root Ports on Non-Root Bridges

Next, every other switch (non-root bridge) must determine its single best path to the Root Bridge. The port on that path is called the Root Port.

The "best" path is determined by the lowest total Path Cost. Each link has a cost based on its bandwidth (e.g., 1Gbps = cost of 4, 100Mbps = cost of 19). The switch calculates the cumulative cost of all links leading to the Root Bridge. The port that lies on the path with the lowest cumulative cost becomes the Root Port. Root Ports are also placed in a forwarding state.

Step 3: Elect Designated Ports and Block the Rest

Finally, on any network segment that connects two switches, one of the ports must be elected as the Designated Port. This is the port responsible for forwarding traffic onto that segment. The port on the switch with the lower path cost to the Root Bridge wins and becomes the Designated Port. Like Root Ports, Designated Ports are placed in a forwarding state.

Any port that is not a Root Port or a Designated Port is a Non-Designated Port. These ports are put into a blocking state, which breaks the loop. This port doesn't forward user data, but it still listens to BPDUs so it can take over if the active path fails.

Let's watch this entire process unfold.

Spanning Tree Protocol Explained | Step by Step

The same CertBros video gives a clear, step-by-step walkthrough of the STP election process, covering the Root Bridge, Root Ports, and Designated Ports.

First, watch the high-level overview from 05:14 to 07:24. Then, watch the detailed breakdown from 09:01 to 15:17. Follow how the switches use the BID and Path Cost to make their decisions.

For a more detailed textual explanation with practical command outputs, which you might see on a real Cisco switch, refer to the following resource.

Spanning Tree Protocol Explained

This article from howtonetwork.com provides a comprehensive text-based walkthrough of the STP convergence process. It includes tables and sample 'show spanning-tree' outputs.

Read the sections 'What is STP – Spanning Tree Protocol?' for a recap of the terms, and 'What is STP Convergence?' for a detailed breakdown of the three election steps. Pay attention to the command outputs, as they show you how to verify STP's operation on a device.


3. The Effect: STP Port States and Timers

You now know that STP blocks ports to prevent loops. But a port doesn't just go from "blocking" to "forwarding" instantly. To prevent temporary loops during a topology change, a port must transition through several states. This directly addresses the second part of our learning goal: identifying the effect of STP on port states.

The five STP port states are:

Spanning Tree Protocol Port States
This chart summarizes the five STP port states. Note that user data is only forwarded in the 'Forwarding' state. All other active states are transitional, designed to safely bring a port online without creating a loop.

The transition between these states is not instant. It's governed by three key timers:

  • Hello Timer: How often the Root Bridge sends out BPDUs (default: 2 seconds).
  • Forward Delay: The time a port spends in the Listening and Learning states (default: 15 seconds for each).
  • Max Age: How long a switch will wait without hearing a BPDU from the root before it decides the topology has changed (default: 20 seconds).

This means a port moving from Blocking to Forwarding takes 30 seconds (15 for Listening + 15 for Learning). If a primary link fails, the network must wait for the Max Age timer to expire (20s) before the backup port even starts this 30-second transition, leading to a total outage of 50 seconds. This is a significant delay in modern networks.

STP Port State Transitions
This flowchart illustrates the STP port state transitions. A port must move sequentially from Blocking through Listening and Learning before it can enter the Forwarding state. The total time for this transition is governed by the Forward Delay timer.

To get a deeper, more practical look at these states and timers, the following video is extremely helpful.

CCNP ENCOR // STP States & Timers (Spanning Tree Protocol) // ENCOR 350-401 Complete Course

This video from Jeremy's IT Lab, while from a CCNP course, provides an exceptionally clear explanation of the STP states and the timers that control them. It includes a CLI demonstration showing the state transitions in real-time.

Watch from 00:18 to 03:42 to understand each port state's function. Then, watch from 06:54 to 09:57 for a detailed look at the Forward Delay timer and to see a live demonstration of a port transitioning through the Listening, Learning, and Forwarding states.

This long convergence time is the main drawback of the original STP standard (802.1D). Newer versions, like Rapid Spanning Tree Protocol (RSTP), significantly reduce this time to a few seconds. While we won't cover RSTP in detail today, it's important to know it exists as the modern evolution of this concept.


Conclusion

In this lesson, you've explored the critical role of the Spanning Tree Protocol in maintaining a stable and loop-free Layer 2 network. You have addressed a fundamental challenge in network design: how to build in physical redundancy without causing network-crippling loops.

Key Takeaways:

  • The Problem: Redundant Layer 2 links create broadcast storms, MAC table instability, and duplicate frames.
  • The STP Solution: STP builds a logical loop-free topology by electing a Root Bridge and calculating the single best path from all switches to it.
  • The Mechanism: The process relies on electing a Root Bridge (lowest BID), Root Ports (lowest path cost to root), and Designated Ports (lowest path cost on a segment). All other ports are blocked.
  • Port States: To prevent temporary loops, ports transition through several states: Blocking -> Listening (15s) -> Learning (15s) -> Forwarding. This process takes 30-50 seconds, which is a major drawback of classic STP.

In our next lesson, we will move from the logical configuration of switches to their physical handling. You will learn how to physically install a rack-mount switch or router and establish initial console access, the very first step in bringing a new network device online in a data center.