Table of Contents
Preventing Loops in Switched Networks
Spanning Tree Protocol, often called STP, exists to solve one very specific and very serious problem in Ethernet switching: switching loops. When multiple switches are connected with redundant links, frames can circle around forever, broadcasts can multiply, and the entire network can become unusable. STP is the mechanism that allows you to build redundancy without creating these loops.
In this chapter you focus on how STP works conceptually, not on all switching details or VLAN topics, which are covered elsewhere.
Why Loops Are Dangerous in Ethernet
Ethernet switching at layer 2 has no built in way to detect a loop. A switch forwards frames based on MAC addresses and, by default, keeps sending broadcasts and unknown unicast frames out of all relevant ports. If there is any physical loop between switches, a broadcast frame can travel around that loop repeatedly. Each time it passes through a switch, that switch may forward it again, which can create a “broadcast storm.”
In addition, switches learn MAC addresses on ports. When a frame loops, a switch may see the same MAC address on different ports at different times. This constant change confuses the MAC address table so that unicast frames may be forwarded out the wrong ports or even flooded like broadcasts. These issues combined can easily overwhelm links and switch CPUs.
Redundant links are still very desirable, because they provide alternate paths when a cable or switch fails. STP allows you to keep the physical redundancy, but it logically disables some paths so that there is only one active loop free path between any two switches at a time.
Basic Idea of Spanning Tree
Spanning Tree Protocol turns a potentially looped physical topology into a logical tree. A tree has no cycles, so frames cannot loop forever. STP does this by placing some switch ports into a special blocking state so that those ports do not forward regular data traffic.
The physical links still exist and STP continues to monitor them. If an active path fails, STP can unblock some of the previously blocked ports so that a different path becomes active. In this way the protocol provides both loop prevention and automatic failover.
At a high level, STP does three important things:
- It elects one switch as the root bridge.
- It calculates the best loop free paths from every switch to the root bridge.
- It disables any extra paths that would create loops, by blocking some ports.
The result is a spanning tree that covers all switches in the network, with exactly one active path from every switch back to the root bridge.
Bridge IDs and Root Bridge Election
To build a tree, all the switches must agree which one is at the top. STP calls this top switch the root bridge. Every other decision STP makes is based on the path to the root bridge.
Each switch that runs STP has a bridge ID. Classic STP bridge ID has two main parts:
- Bridge priority, a configurable value.
- The switch MAC address, which acts as a tiebreaker.
In simplified form:
$$
\text{Bridge ID} = (\text{priority},\ \text{MAC address})
$$
Lower bridge ID values are better. If you want a particular switch to become the root bridge, you configure a lower priority value on that switch than on the others.
Important rule:
The switch with the lowest bridge ID becomes the root bridge.
Root bridge election happens through STP messages and can change if you add a new switch with a lower bridge ID or if the current root fails. In most designed networks the root is manually chosen by setting its priority lower than all others.
STP Messages: Bridge Protocol Data Units (BPDUs)
STP uses special control frames called Bridge Protocol Data Units, or BPDUs, to share information about the tree. BPDUs are sent by switches to each other, not by regular hosts.
BPDUs include information such as:
The ID of the root bridge that the sender believes is the current root.
The sender’s own bridge ID.
The cost to reach the root from the sender.
Each switch compares BPDUs it receives with what it already knows. If it learns that a better root bridge exists, or that there is a better path to that root, it updates its view and may change which ports are active or blocked.
Only certain ports send BPDUs in regular operation, but all STP enabled ports can listen to BPDUs. If a new or different BPDU arrives, it can trigger recalculation of the spanning tree.
Path Cost and Root Path Selection
STP decides how to reach the root bridge based on a concept called cost. Every link has an STP cost value that is related to the speed of that link. Higher bandwidth links have a lower cost. The total path cost from a switch to the root is the sum of the costs of all the links in that path.
In a simplified form:
$$
\text{Root path cost} = \sum \text{(link costs from switch to root)}
$$
Important rule:
Each non root switch chooses the path with the lowest total cost to reach the root bridge.
Standard STP defines reference costs for common link speeds. For example, a fast link such as 1 Gbps has a lower cost than a 100 Mbps link. Vendors may use updated cost values, but the general principle remains that faster links are more preferred routes toward the root.
If a switch learns multiple possible paths to the root through different ports, it compares the total cost for each path. The port that offers the lowest total cost to the root becomes that switch’s main connection to the root.
STP Port Roles
Once the root bridge is chosen and the best paths are known, STP assigns a role to each STP enabled port on each switch. These roles describe how the port participates in the spanning tree.
The main STP port roles are:
Root port. On every non root switch, exactly one port is chosen as the root port. This port has the best path to the root bridge. It is the switch’s primary upstream connection toward the root. The root bridge itself does not have a root port.
Designated port. On every link, exactly one port is chosen as the designated port for that segment. The designated port is the one that forwards traffic from that link toward the root. Typically, on a shared segment, the port belonging to the switch with the best path to the root is the designated port.
Non designated or blocked port. Ports that are neither root ports nor designated ports become non designated. These ports are placed into a state where they do not forward user data traffic. They exist to prevent loops, but they can become active if the topology changes.
On the root bridge, all active ports are designated ports because traffic should be able to flow out of the root onto each segment. On other switches there is one root port, some designated ports, and some ports that end up blocked in order to avoid loops.
Important rule:
Every network segment has exactly one designated port, and every non root switch has exactly one root port. All other loop creating ports are blocked.
These roles create a clear tree structure. From any switch, you can follow the root port toward the root, and from the root you can follow designated ports to reach any other switch.
STP Port States
STP not only assigns roles, it also defines port states that control whether a port forwards data, learns MAC addresses, or only listens for control information. Port states allow STP to transition slowly when the topology changes, which helps avoid temporary loops.
Classic STP uses these main states:
Disabled. The port is administratively shut down and does not participate in STP or data forwarding.
Blocking. The port does not forward user data or learn MAC addresses. It only listens for BPDUs. This state is used on non designated ports to prevent loops.
Listening. The port is preparing to become active. It does not forward data or learn MAC addresses yet, but it listens and sends BPDUs to understand the STP topology. This state is temporary during topology changes.
Learning. The port does not forward user data yet, but it starts to learn MAC addresses from incoming frames. This allows the switch to build its MAC table before beginning to forward frames, which improves efficiency when the port finally becomes active.
Forwarding. The port forwards data traffic, learns MAC addresses, and participates fully in STP. Root ports and designated ports normally reach this state.
The transitions from blocking to forwarding are gradual. When STP decides a port should start forwarding, it first moves the port from blocking to listening, then to learning, and finally to forwarding. Each step has a timer so that the network has time to converge and avoid loops.
Basic STP Convergence
Convergence is the process by which all switches reach agreement on the spanning tree after a change in the network. A simple sequence for an initial convergence looks like this:
At startup, all switches assume they are the root and begin sending BPDUs.
Switches compare the received BPDUs. They discover that some other switch has a lower bridge ID and is therefore a better root. Eventually, all switches agree on a single root bridge.
Each non root switch calculates the lowest cost path to the root and selects its root port.
For each link, a designated port is chosen based on which side offers the best path to the root.
Ports that are neither root nor designated enter the blocking state.
Ports that should forward data move through listening and learning states and then start forwarding.
This process can take noticeable time in traditional STP because of the state timers. During convergence, traffic may be briefly disrupted as ports change roles and states. More advanced versions of STP are designed to speed up convergence, but the basic idea remains the same: after a change, STP recalculates the tree and reassigns roles and states.
Handling Link or Switch Failures
One of the benefits of using STP is that redundant paths are ready to be used when something fails. When a link or a switch in the active tree goes down, ports lose their connection and stop receiving BPDUs. This signals that the current path to the root is no longer valid.
If a switch loses its root port, it looks for another port that can reach the root, usually one that was previously blocked. If such a port exists, STP recalculates the path costs and roles. A previously blocked port may become the new root port, or a different port may become designated for a segment. The port then moves through the listening and learning states until it reaches forwarding.
In this way, the spanning tree changes shape in response to failures. Some links that were previously unused become active, while others may become blocked. The goal is always to maintain a single logical loop free tree that covers all remaining switches.
Basic Comparison with Rapid STP
In many modern networks you will find a variation called Rapid Spanning Tree Protocol, often referred to as RSTP. RSTP keeps the same high level concepts, such as root bridge, port roles, and costs, but it improves how quickly the network converges after a change.
While classic STP waits for timers before making some decisions, RSTP uses more aggressive communication between switches to detect changes and respond faster. It also refines port roles and states to reduce the time that ports spend in intermediate states.
You do not need to know all the details of RSTP here, but it is important to recognize that both STP and RSTP exist to provide loop free topology with redundancy, and that RSTP is an evolution designed for quicker recovery.
Practical Design Considerations
In real networks, engineers usually choose one or two central switches in a part of the network, such as a core or distribution layer, to be the root bridge for a given spanning tree. They manually configure a low priority on those switches so that they win root election. This gives predictable traffic flows because all STP calculations will prefer paths toward those central devices.
Another common practice is to design physical connections in a somewhat hierarchical way, so that fewer redundant loops exist and STP decisions are simpler. Where redundancy is necessary, engineers understand which ports are expected to be blocked and which should remain active. This reduces surprises and makes troubleshooting easier.
Spanning Tree Protocol is only one part of a complete switching design, but it is a crucial protection mechanism whenever there are multiple inter switch links. The concepts you have learned here about root bridge, BPDUs, cost, port roles, and states are the foundation for understanding more advanced switching and redundancy behaviors.