Spanning Tree Protocol

Preventing loops in switched networks with STP, RSTP, and MSTP.

The Need for Redundancy and the Problem It Creates

In any critical network design, reliability is paramount. A single point of failure can be catastrophic, leading to downtime that costs businesses money and productivity. To build a reliable network, administrators intentionally create redundancy by adding extra links and devices. A common strategy in a switched network is to connect switches with multiple physical links. If one link fails (e.g., a cable is cut), traffic can automatically reroute through the alternative path, ensuring the network stays up and running.

However, this redundancy, while essential for uptime, introduces a dangerous and potentially network-breaking problem at Layer 2 (the Data Link Layer). The very nature of Ethernet switches, which are designed to efficiently forward traffic, creates a disastrous situation when there are physical loops in the network topology. This problem is known as a switching loop or bridging loop.

The Catastrophic Effects of a Layer 2 Loop

Ethernet frames do not have a "time-to-live" (TTL) mechanism like IP packets at Layer 3. This means that if a frame gets caught in a loop, there is nothing in its header to tell it to stop. It will be forwarded by switches forever, or until the network collapses. A switching loop leads to three immediate and devastating problems:

  • 1. Broadcast Storms

    This is the most immediate and destructive symptom. When a device sends a (for instance, an ARP request to find another device's MAC address), it enters the loop. Every switch in the loop receives the frame and, following the rules of Ethernet, forwards it out of all its other ports. This means the frame is sent back out onto the loop, where it is received again by the other switches, which then also forward it out all their ports. The single frame is infinitely duplicated, circulating faster and faster, consuming all available bandwidth and overwhelming the CPUs of every switch and connected device. The network quickly becomes unusable, a condition known as a broadcast storm.

  • 2. MAC Table Instability

    Switches learn the location of devices by inspecting the source MAC address of incoming frames. In a loop, this learning process breaks down. Consider a PC connected to Switch A. It sends a frame. Switch B might see this frame arrive on its link from Switch A. A moment later, because of the loop, the same frame might arrive at Switch B from its link with Switch C. From Switch B's perspective, the PC's MAC address appears to be in two places at once. It will rapidly and continuously update its , a phenomenon called "MAC flapping." This instability means the switch can never make a reliable forwarding decision, leading to frames being sent to the wrong ports or being unnecessarily flooded.

  • 3. Multiple Frame Transmission

    Even for a unicast frame (sent from one specific device to another), a loop can cause serious problems. If the destination MAC address is not yet in the switches' MAC tables, the frame will initially be flooded. This flood enters the loop, and the destination device might receive the same frame multiple times from different paths. This can confuse applications and protocols that are not designed to handle duplicate frames.

The Solution: Spanning Tree Protocol (STP)

It's clear that physical redundancy is necessary for reliability, but Layer 2 loops are fatal. To solve this paradox, the Spanning Tree Protocol (STP) was created. STP is a (standardized as IEEE 802.1D) designed specifically to prevent switching loops in networks with redundant paths.

The core idea of STP is brilliant in its simplicity. Instead of removing physical redundancy, STP creates a loop-free logical topology by selectively blocking certain network ports. It analyzes the entire switched network and builds a "tree" structure that spans all the switches, ensuring there is only one active path between any two devices. The redundant links are not physically disconnected; they are simply put into a standby or "blocking" state. They are kept in reserve, and if an active link fails, STP can automatically recalculate the tree and unblock one of the standby links to restore connectivity, usually within a few seconds.

How STP Works: The Election and Calculation Process

STP builds its loop-free tree through a well-defined, four-step process. All switches in the network participate by exchanging special messages called Bridge Protocol Data Units (BPDUs).

Step 1: Elect a Root Bridge

The very first thing STP does is establish a central point of reference for the entire network. This reference is called the Root Bridge. All other decisions in the STP calculation are made from the perspective of reaching the Root Bridge. To elect one, switches compare their . The BID consists of two parts:

  1. Bridge Priority: A 2-byte configurable value (default is typically 32768). Administrators can lower this value to influence which switch becomes the root.
  2. MAC Address: The 6-byte unique MAC address of the switch.

The switch with the numerically lowest Bridge ID wins the election and becomes the Root Bridge.

The priority is the most important factor. If two switches have the same priority, their MAC addresses are used as a tie-breaker (the lower MAC address wins).

Step 2: Elect a Root Port on Each Non-Root Bridge

Every switch that is not the Root Bridge (a non-root bridge) must figure out its single best path to reach the root. The port on the switch that leads to this best path is elected as the Root Port. "Best" is determined by the lowest total .

STP assigns a cost to each link, typically based on its speed:

  • 10 Gbps Link Cost: 2
  • 1 Gbps Link Cost: 4
  • 100 Mbps Link Cost: 19
  • 10 Mbps Link Cost: 100

A switch calculates the path cost by adding up the costs of all links on a path to the Root Bridge. The port that is the start of the lowest-cost path becomes the Root Port.

If a switch has two or more paths with the same cost, a series of tie-breakers is used: lowest sending switch's Bridge ID, then lowest sending port ID.

Step 3: Elect a Designated Port on Each Network Segment

Next, on every individual network segment (link between switches), one port must be elected as the Designated Port. The Designated Port is the one responsible for forwarding traffic onto that segment. All ports on the Root Bridge are always Designated Ports. For other segments, the port on the switch that has the lower path cost to the Root Bridge wins the election and becomes the Designated Port for that link.

Step 4: Place All Other Ports in a Blocking State

After all Root Ports and Designated Ports have been elected, any port that is left over is a non-designated port. These are the redundant links that create loops. STP places these ports into a blocking state. A blocked port does not forward any user data frames and does not learn MAC addresses. It only listens for BPDUs to know if the network topology changes. By blocking these specific ports, the physical loop is broken, and a single, loop-free logical path is created.

STP Timers and Port States: The Reason for the Delay

The process of converging to a stable, loop-free topology is not instantaneous. The original 802.1D STP uses several timers and port states that can lead to significant network downtime (30-50 seconds) after a topology change. A port transitioning from blocking to forwarding must pass through two intermediate states:

  • Blocking: The port is logically shut down to prevent a loop. It only listens to BPDUs.
  • Listening (15 seconds): The first state when transitioning. The switch makes sure no loops will be created before preparing to forward. It sends and receives BPDUs but does not forward data.
  • Learning (15 seconds): The port is still not forwarding user data, but it begins to learn MAC addresses from incoming frames to populate its MAC address table.
  • Forwarding: The port is fully operational, forwarding data frames and actively participating in the network.

This slow convergence (15s+15s=30s15s + 15s = 30s of waiting) was a major drawback of the original STP.

The Evolution of Spanning Tree

To address the slow convergence and other limitations of the original STP, several enhanced versions have been developed and are now widely used.

  • Rapid Spanning Tree Protocol (RSTP - IEEE 802.1w)

    RSTP is a significant improvement over STP and is backward-compatible with it. It introduces new port roles (Alternate and Backup ports for quick failover) and uses a more active proposal-agreement mechanism between switches. This allows RSTP to achieve much faster convergence times, often in less than a second after a link failure, drastically reducing network downtime.

  • Per-VLAN Spanning Tree Plus (PVST+)

    A problem with both STP and RSTP is that they calculate a single spanning tree for the entire network. In a network using VLANs, this can be inefficient. A link might be blocked for all traffic, even though it's needed for a specific VLAN. PVST+ (a Cisco enhancement) solves this by running a separate and independent STP instance for each individual VLAN. This allows for , as an administrator can configure one link to be the forwarding path for VLAN 10 and a different, redundant link to be the forwarding path for VLAN 20, using all available links efficiently.

  • Multiple Spanning Tree Protocol (MSTP - IEEE 802.1s)

    While PVST+ is very flexible, running hundreds of STP instances on a large network can consume significant switch CPU resources. MSTP offers a compromise. It allows an administrator to group multiple VLANs with similar traffic flow requirements into a single "instance." A single spanning tree is then calculated for each instance, not for each individual VLAN. This dramatically reduces the number of required STP calculations while still providing the benefits of load balancing across multiple links.

    Spanning Tree Protocol | Teleinf Edu