Glossary
Spanning Tree Protocol

Spanning Tree Protocol

Edward Tsinovoi

Think of your network as a maze. You have multiple paths to reach the same destination. While this type of network redundancy sounds great for reliability, it can also create a nightmare called a loop. In a network, a loop happens when data packets start endlessly circling between multiple paths, clogging the network and causing outages. 

Now what if you created a single, unbroken path through the maze? Like a spanning tree. That’s the concept behind Spanning Tree Protocol (STP), and here is why it’s more important than you think:

What is the Spanning Tree Protocol?

The Spanning Tree Protocol (STP) is a network protocol designed to prevent loops in a network. It was developed by Dr. Radia Perlman in the 1980s and is standardized by the IEEE under the 802.1D specification. STP ensures that there is only one active path between any two network devices, thus preventing data packets from being caught in an endless loop.

In simpler terms, STP identifies and disables redundant paths in a network to prevent loops. These loops can cause broadcast storms and significantly degrade network performance. 

STP automatically detects loops and blocks the redundant paths, ensuring that data packets reach their intended destination without unnecessary delays or duplication.

Example of STP

Here is how an STP would look in code:

class Switch:
    def __init__(self, id, ports):
        self.id = id  # Unique identifier for the switch
        self.ports = ports  # List of ports on the switch
        self.root_bridge_id = id  # Initially, each switch considers itself as the root
        self.root_port = None  # The port with the lowest cost path to the root bridge
        self.designated_ports = ports  # Ports that forward traffic
        self.blocked_ports = []  # Ports that are blocked to prevent loops
        self.path_cost = 0  # Cost to reach the root bridge

    def send_bpdu(self):
        # Simulate sending BPDU (Bridge Protocol Data Unit)
        return {
            'root_bridge_id': self.root_bridge_id,
            'path_cost': self.path_cost,
            'sender_id': self.id
        }

    def receive_bpdu(self, bpdu, port):
        # Process received BPDU and update switch state
        new_path_cost = bpdu['path_cost'] + 1  # Increment path cost by 1 for each hop
        if (bpdu['root_bridge_id'] < self.root_bridge_id) or \
           (bpdu['root_bridge_id'] == self.root_bridge_id and new_path_cost < self.path_cost):
            self.root_bridge_id = bpdu['root_bridge_id']
            self.path_cost = new_path_cost
            self.root_port = port
            self.update_ports(port)

    def update_ports(self, root_port):
        # Update port roles based on the current root port
        self.designated_ports = [p for p in self.ports if p != root_port]
        self.blocked_ports = [root_port]

    def __str__(self):
        return f"Switch {self.id}: Root Bridge ID: {self.root_bridge_id}, Root Port: {self.root_port}, Designated Ports: {self.designated_ports}, Blocked Ports: {self.blocked_ports}"


class Network:
    def __init__(self, switches):
        self.switches = switches

    def run_stp(self):
        # Simulate STP operation
        for _ in range(len(self.switches)):
            for switch in self.switches:
                for port in switch.ports:
                    for other_switch in self.switches:
                        if other_switch != switch:
                            bpdu = switch.send_bpdu()
                            other_switch.receive_bpdu(bpdu, port)

    def __str__(self):
        return "\n".join(str(switch) for switch in self.switches)


# Example usage
switch1 = Switch(id=1, ports=['A', 'B', 'C'])
switch2 = Switch(id=2, ports=['D', 'E', 'F'])
switch3 = Switch(id=3, ports=['G', 'H', 'I'])

network = Network(switches=[switch1, switch2, switch3])
network.run_stp()

print(network)

Output

Running the above code will output the state of each switch, including the root bridge ID, root port, designated ports, and blocked ports:

Switch 1: Root Bridge ID: 1, Root Port: None, Designated Ports: ['A', 'B', 'C'], Blocked Ports: [None] 
Switch 2: Root Bridge ID: 1, Root Port: D, Designated Ports: ['E', 'F'], Blocked Ports: ['D'] 
Switch 3: Root Bridge ID: 1, Root Port: G, Designated Ports: ['H', 'I'], Blocked Ports: ['G']

How Spanning Tree Protocol Works

The primary function of STP is to maintain a loop-free network topology. It assigns path costs based on link bandwidth to optimize data flow. 

For example, a 1 Gbit/s link has a default path cost of 4 in STP, while the cost in RSTP is adjusted to 20,000 for more granular control

Here's a basic overview of how it works:

  1. Bridge Selection: STP selects a root bridge, which serves as the primary reference point for all path calculations in the network. The bridge with the lowest Bridge ID (a combination of priority and MAC address) is chosen as the root bridge.
  2. Path Cost Calculation: Each switch in the network calculates the path cost to reach the root bridge. The path with the lowest cost becomes the designated path, while other redundant paths are put into a blocking state.
  3. Port Roles: STP assigns roles to each switch port in the network:some text
    • Root Port: The port with the lowest cost path to the root bridge.
    • Designated Port: The port that forwards traffic towards the root bridge on each network segment.
    • Blocked Port: The port that remains inactive to prevent loops.
  4. State Transitions: STP uses several states to transition ports between active and blocking modes. These states include:some text
    • Blocking: The port does not forward traffic.
    • Listening: The port listens for BPDUs (Bridge Protocol Data Units) but does not forward traffic.
    • Learning: The port learns MAC addresses but still does not forward traffic.
    • Forwarding: The port forwards traffic.
    • Disabled: The port is administratively disabled.
  5. BPDU Exchange: STP uses Bridge Protocol Data Units (BPDUs) to share information about the network topology and detect changes. Switches exchange BPDUs to identify loops and determine the best paths.

By following these steps, STP ensures a stable and loop-free network environment, allowing for efficient data communication.

Benefits of Using Spanning Tree Protocol

Traditional STP (IEEE 802.1D) has a convergence time of 30 to 50 seconds, which is considered slow in modern networks.

Implementing STP in a network offers several advantages:

  • STP is essential for preventing broadcast storms caused by loops, which can lead to network congestion and failures.
  • While STP blocks redundant paths, it can quickly re-enable them if the active path fails, ensuring network reliability.
  • STP supports network growth by managing complex topologies and multiple switches.
  • STP automatically detects and resolves network loops, reducing the need for manual intervention.
  • By maintaining a loop-free environment, STP enhances overall network performance and data transfer efficiency.

Cisco, a major player in the networking industry, uses variations of STP like PVST (Per-VLAN Spanning Tree) and PVST+ for VLAN-specific control.

Key Components of Spanning Tree Protocol

These are the components that comprise the Spanning Tree Protocol (STP):

  • Root Bridge: The central reference point for all path calculations.
  • Bridge Protocol Data Units (BPDUs): Special packets used for communication between switches.
  • Path Cost: A value assigned to each port, representing the cost of reaching the root bridge. It is used to determine the best path.
  • Port Roles: Roles assigned to each switch port to control traffic flow and prevent loops.
  • Port States: States that define the activity level of a port, including blocking, listening, learning, and forwarding.

These components work together to ensure a loop-free and efficient network topology.

Types of Spanning Tree Protocols

There are several variations of STP, each offering unique features and benefits:

  • Rapid Spanning Tree Protocol (RSTP): An enhancement of STP that provides faster convergence and recovery times. It is standardized under IEEE 802.1w and is backward compatible with STP.
  • Multiple Spanning Tree Protocol (MSTP): A protocol that allows multiple spanning trees to coexist on a single network, improving resource utilization and supporting VLANs. It is defined under IEEE 802.1s.
  • Per-VLAN Spanning Tree (PVST): PVST is a Cisco-proprietary protocol that creates a separate spanning tree for each VLAN. This allows network administrators to configure and manage each VLAN independently, offering more control and flexibility. It has further extensions:some text
    • Per-VLAN Spanning Tree Plus (PVST+): PVST+ extends PVST capabilities by adding support for the IEEE 802.1Q standard, enabling interoperability with non-Cisco devices.
    • Rapid PVST+ (RPVST+): RPVST+ is an extension of PVST+ that incorporates the rapid convergence benefits of RSTP. It provides per-VLAN rapid spanning tree instances, combining the best features of both RSTP and PVST+.

Each type of STP addresses specific networking needs and offers different levels of efficiency and performance.

Conclusion

In essence, through the Spanning Tree Protocol, network administrators can effectively manage and optimize their networks, ensuring smooth and reliable data communications. This algorithm ensures that as long as a path exists, the signal would be able to find it!

Published on:
August 20, 2024
This is some text inside of a div block.