STP - Spanning Tree Protocol Explained With Examples

This tutorial explains what the STP protocol is and how it removes loops from the network. Learn STP features and functions in detail with examples.

STP (Spanning Tree Protocol) automatically removes layer 2 switching loops by shutting down the redundant links. A redundant link is an additional link between two switches. A redundant link is usually created for backup purposes.

Just like every coin has two sides, a redundant link, along with several advantages, has some disadvantages. The biggest disadvantage of a redundant link is that it creates a loop between switches. If a loop exists between two switches, they do not work properly.

This tutorial is the second and last part of the article "Layer 2 switching loop: - causes and solutions". The first part of this tutorial is the following.

Layer 2 Switching Loops in Network Explained

For this tutorial, I assume that you know what the layer 2 switching loop is and what problems it creates in the network. If you don’t know what the layer 2 switching loop is and how it works, I suggest you take a pause here and check the first of this tutorial first.

What is the STP?

STP is a protocol. It actively monitors all links of the network. To finds a redundant link, it uses an algorithm, known as the STA (spanning-tree algorithm). The STA algorithm first creates a topology database then it finds and disables the redundant links. Once redundant links are disabled, only the STP-chosen links remain active. If a new link is added or an existing link is removed, the STP re-runs the STA algorithm and re-adjusts all links to reflect the change.

The below section explains how all it happens and what terminology and components the STP uses in its operation.

BPDUs

BPDUs (Bridge Protocol Data Unit) are multicast frames which switches use to share information about themselves and their connections. Besides sharing information, switches also use BPDUs to learn the network topology, to learn which switch is connected with which switches, and to learn whether any layer 2 switching loop exists in the learned topology or not.

Root Bridge

A Root Bridge is the starting point of the STP network topology. To elect a Root Bridge from all switches of the network, STP uses two parameters; a variable known as bridge priority and the MAC addresses of participating switches. A switch that has the lowest bridge priority value, is elected as the root bridge. If the bridge priority value is the same in all switches, the switch which has the lowest MAC address is elected as the Root Bridge.

By default, the bridge priority value is set to 32768 in all Cisco switches. Unless you change this value, a switch that has the lowest MAC address is elected as the Root Bridge. If you want a specific switch to be elected as the Root Bridge, you can set the bridge priority value of that switch to less than 32768.

The selection process of the Root Bridge happens each time when a network change occurs like a new switch is added in the network topology, or an existing switch is removed or the current Root Bridge is failed. If other switches of the network do not receive BPDUs from the Root Bridge within 20 seconds, they assume that the Root Bridge has failed. If the current Root Bridge fails, remaining switches automatically start the election process to choose a new Root Bridge again.

Non-Root Bridge

Except the Root Bridge, all remaining switches of the network are considered as the Non-Root Bridges. Non-Root Bridges receive updates from the Root Bridge and update their STP databases relatively.

Port Cost

Based on the connected media link, STP assigns a value to each port of the network. This value is known as the port cost value. STP uses this value to choose the single best path when multiple links are available between two switches. It selects the port which has the lowest port cost value.

There are two sets of the port cost value. The following table lists both.

Bandwidth Old Cost Value New Cost Value
10 Gbps 1 2
1 Gbps 1 4
100 Mbps 10 19
10 Mbps 100 100

Some old series switches, like the Catalyst 1900, use the old cost value. Cisco has already discontinued these old series switches. New series switches, like the 2960, use the new cost value. In the port selection process, the lower cost value is always preferred over the higher cost value. For example, if two ports; F0 and F1 have cost value 2 and 4 respectively. The port F0 will be selected.

Path Cost

Path cost is an accumulated value of the port costs from the Root Bridge to other switches in the network. It is always calculated from the Root Bridge. Default path cost at the Root Bridge is 0. BPDU contains the path cost information.

When the Root Bridge advertises BPDU out from its interfaces, it sets the path cost to 0. The switch which receives this BPDU increments the path cost by adding the port cost value of the port on which the BPDU arrived. For example, if the switch receives the BPDU on the Gigabit interface then the accumulated path cost will be 4.

0 (Value which it received from the Root Bridge) + 4 (Port cost value of the interface on which it received the BPDU) = 4

Now, this switch sets the accumulated path cost (4) in the BPDU and forwards it. The next switch which is connected with this switch follows the same rule. For example, if the next switch receives this BPDU on the Fast Ethernet port, for that switch, the accumulated path cost will be 23.

4 (Value which is received) + 19 (Port cost value of the incoming port) = 23.

Root Port

The Root port is the port that directly connects to the Root Bridge, or has the shortest path to the Root Bridge. The shortest path is the path that has the lowest path cost value. Remember that, a switch can go through many other switches to get the root bridge. So it’s not always the shortest path but it is the fastest path.

Designated Ports

A designated port is the port that has the lowest port cost value to get on a given network, compared to other ports on that segment. STP marks the designated ports as the forwarding ports. Forwarding ports are used to forward the frames.

Non-Designated Ports

A non-designated port is a port that has the higher port cost than the designated port. STP marks the non-designated port as the blocking port. Blocking ports are used to remove loops.

STP port states

All ports on a STP running switch, go through the four different states; blocking, listening, learning, and forwarding. Through these states, the switch not only understands the network topology but also calculates the path cost value and based on that value elects the designated and non-designated ports. After these states, the switch is considered as the STP convergent switch. Let’s understand each state in detail.

STP Blocking state

When we power on a switch, the switch puts all of its ports in this state. In this state, the switch only listens and processes the BPDUs. Except the BPDUs, it drops all other frames. From the incoming BPDUs, it learns the network topology and determines the ports which will work as the root ports, as the designated ports, and as the blocked ports.

All ports remain in this state for twenty seconds. After twenty seconds, only the root port and designated ports move into the next state. Remaining ports stay in this state.

STP Listening state

In this state, ports still listen and process only BPDUs. All other frames except BPDUs are dropped. The switch double checks the layer 2 topology to make sure that no loops occur in the network before processing the data frames. Ports remain in this state for fifteen seconds.

STP Learning state

Only the root port and designated ports enter into the learning state from the listening state. In this state, ports still listen and process BPDUs. However, in this state, ports start processing user frames. Switch examines the source address of user frames and updates its CAM table but it does not forward any user frame to the destination port. Ports stay in this state for fifteen seconds.

STP Forwarding state

In this state, the switch listens and processes both BPDUs and user frames. It uses BPDUs to monitor the network topology. By reading the source address field of users’ frames it also builds and updates CAM table entries. This state is also referred as the convergence.

Convergence refers to a situation in which all ports of a switch have transitioned to either forwarding or blocking mode. During the STP converging, the switch does not forward any user frame. Usually, convergence takes place in fifty seconds (20 seconds of the blocking state + 15 seconds of the listing state + 15 seconds of the learning state).

STP Disable state

This state applies to all ports which are either manually shut down or removed from the STP by an administrator. All unplugged ports also remain in this state. Any port which belongs to this state does not participate in the STP operation.

STP operation in nutshell

  • All switches of the STP domain, first elect a root bridge. The root bridge acts as a point of reference for all other switches in the network. All ports of the root bridge remain in the forwarding mode.

  • Once the root bridge is elected, all remaining switches select a single port that has the shortest path cost to reach the root bridge and marked it as the root port.

  • After selecting the root port, switches determine a single designated port for each connection.

  • If multiple ports are connected with the same switch or LAN segment, the switch select only one port that has the lowest path cost and marks it as the designated port.

  • Once the root port and designated ports are selected, the switch blocks all remaining ports to remove any possible or existing loop from the network.

The following image shows how the STP changes a physically looped topology into a virtually looped free topology.

stp example

The show spanning-tree command

To view the information about the STP operation, you can use the show spanning-tree command from the privileged-exec mode. The output of this command can be divided into three subsets. The first set contains information about the Root Bridge. The second set contains information about the switch itself. The third set lists the status of active interfaces that are participating in the STP operation.

The following image shows a sample output of this command from both the root bridge switch and a member switch.

show stp status command

  • On the Root Bridge, the first and second subset display the same information.
  • On the Non-Root Bridge, the first subset shows information about the Root Bridge while the second subset displays information about the switch itself.
  • The bridge priority value is the sum of the default priority value and the VLAN ID.

STP variations

There are two different implementations of the STP protocol, DEC and 802.1d. Both implementations are not compatible with each other. When you purchase a new switch for the network, make sure it supports the same variation of the STP which your existing switches are using. Never mix the devices which are running 802.1d’s STP with the devices which are running DEC’s STP, otherwise, you might run into the layer 2 looping problems. All of Cisco’s switches use 802.1d’s STP.

That’s all for this tutorial. If you like this tutorial, please don’t forget to share it through your social network.

ComputerNetworkingNotes CCNA Study Guide STP - Spanning Tree Protocol Explained With Examples