Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Eliminating Layer 2 Loops with Spanning Tree Protocol

Tech May 17 2

Introduction to Loop Prevention in Ethernet Networks

In modern Ethernet switching environments, redundancy is critical for maintaining high availability. Network architects often deploy multiple physical links between switches to ensure backup paths exist. However, introducing redundant physical connections creates logical loops at Layer 2. These loops can lead to severe issues such as broadcast storms, MAC address table instability, and multiple frame transmissions, ultimately degrading performance or causing total network outages.

The Spanning Tree Protocol (STP) was developed to address these challenges. STP allows switches to communicate with one another to detect loops within the topology. Once a loop is identified, the protocol logically disables specific redundant interfaces, transforming the physical mesh into a loop-free logical tree structure. This ensures that only one active path exists between any two network nodes while keeping backup paths available for failover scenarios.

Core Components of Spanning Tree

To function correctly, STP relies on several key identifiers and metrics exchanged between devices.

Bridge Identifier (BID)

Every switch participating in STP is identified by a unique Bridge ID. According to the IEEE 802.1D standard, the BID consists of two parts: a 16-bit Bridge Priority value and a 48-bit MAC address. The priority occupies the most significant bits. During the election process, the switch with the lowest numerical BID is selected as the logical center of the tree.

Root Bridge

The Root Bridge serves as the reference point for the antire spanning tree. All path calculations originate from this device. The election is deterministic: the switch advertising the lowest BID becomes the Root. If priorities are identical, the MAC address acts as the tie-breaker, with the lower MAC address winning. For network stability, administrators should manually configure the intended core switch with the lowest priority value.

Path Cost and Root Path Cost (RPC)

Each STP-enabled interface maintains a Cost value, which is inversely proportional to the link bandwidth. Higher bandwidth links have lower costs. The Root Path Cost is the cumulative sum of costs from a specific switch's interface back to the Root Bridge. STP uses this metric to determine the most efficient path through the network.

Port Identifier (PID)

Interface are identified by a Port ID, composed of a priority value (defaulting to 128 on many platforms) and a port number. This identifier is used as a final tie-breaker during port role elections if all other metrics are equal.

Bridge Protocol Data Unit (BPDU)

Switches exchange BPDUs to share topology information. There are two primary types:

  • Configuration BPDU: Used for initial topology calculation and maintaining the tree structure.
  • TCN BPDU (Topology Change Notification): Triggered when the network structure changes to inform switches to update their MAC tables.

When comparing BPDUs to determine the best path, switches evaluate fields in the following order: lowest Root Bridge ID, lowest Root Path Cost, lowest Sender Bridge ID, and lowest Sender Port ID.

Topology Election Mechanism

STP converges through a systematic election process:

  1. Root Bridge Election: All switches exchange BPDUs. The device with the superior BID assumes the Root role. Only one Root Bridge exists per STP instance.
  2. Root Port Selection: Every non-root switch selects one Root Port. This is the interface offering the lowest path cost to the Root Bridge.
  3. Designated Port Selection: On each network segment, one switch is chosen to forward traffic towards the downstream devices. The interface on this switch becomes the Designated Port. Typically, all interfaces on the Root Bridge are Designated Ports.
  4. Blocking Non-Designated Ports: Any interface that is neither a Root Port nor a Designated Port is placed into a Blocking state. This logically breaks the loop.

Port States and Transitions

Interfaces transition through specific states to prevent temporary loops during topology changes. The standard states include:

State Behavior
Disabled Administratively down or failed. No BPDUs or data frames are processed.
Blocking Receives BPDUs to monitor the topology but does not forward data or learn MAC addresses.
Listening Prepares to forward data. Processes BPDUs but does not learn MAC addresses or forward user traffic.
Learning Populates the MAC address table based on incoming frames but still does not forward user traffic.
Forwarding Fully operational. Sends and receives data frames and processes BPDUs.

Handling Topology Changes

STP is dynamic and reacts to network failures. The convergence time depends on the type of failure.

Root Bridge Failure

If the Root Bridge fails, it stops transmitting BPDUs. Non-root switches wait for the Max Age timer (default 20 seconds) to expire before declaring the stored information stale. They then initiate a new election process. The total recovery time can exceed 50 seconds due to the Listening and Learning states (Forward Delay timers).

Direct Link Failure

When a switch detects a physical link failure on its Root Port, it can immediately promote an alternate port. However, this port must still transition through Listening and Learning states, typically requiring 30 seconds (2 × Forward Delay) before forwarding traffic.

Indirect Link Failure

If a link fails elsewhere in the network, switches may not detect the physical loss immediately. They rely on BPDU timeouts. This scenario often results in the longest convergence time, similar to a Root Bridge failure.

MAC Address Table Integrity

During reconvergence, frames may be misdirected because MAC address tables still reference old paths. TCN BPDUs help accelerate the aging process of MAC entries, ensuring switches flush stale data and relearn locations based on the new topology.

Deployment and Verification

The following example demonstrates how to configure STP on a three-switch topology to ensure Switch-Alpha acts as the Root, Switch-Beta as the backup, and a specific link on Switch-Gamma is blocked.

Configuration Steps

1. Configure the Primary Root (Switch-Alpha)
Set the bridge priority to the lowest value to guarantee election as Root.

<Switch-Alpha> system-view
[Switch-Alpha] stp mode stp
[Switch-Alpha] stp enable
[Switch-Alpha] stp priority 0

2. Configure the Backup Root (Switch-Beta)
Assign a priority lower than the default but higher than the primary root.

<Switch-Beta> system-view
[Switch-Beta] stp mode stp
[Switch-Beta] stp enable
[Switch-Beta] stp priority 4096

3. Configure Access Switch (Switch-Gamma)
Leave the priority at default. To force a specific port to block, adjust the path cost on the interface.

<Switch-Gamma> system-view
[Switch-Gamma] stp mode stp
[Switch-Gamma] stp enable
[Switch-Gamma] interface 10GE1/0/2
[Switch-Gamma-10GE1/0/2] stp cost 20000

Verification

After configuration, verify the port roles and states on Switch-Gamma. The interface with the higher cost should transition to the Alternate (Blocking) state.

<Switch-Gamma> display stp brief
MSTID  Port                        Role  STP State  Protection
  0    10GE1/0/1                   ROOT  FORWARDING  NONE
  0    10GE1/0/2                   ALTE  DISCARDING  NONE

The output confirms that 10GE1/0/1 is the Root Port forwarding traffic, while 10GE1/0/2 is blocked to prevent loops, ensuring a stable Layer 2 environment.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.