Overview
One of the most important protocols deployed on Ethernet switched networks is the Spanning Tree Protocol (STP). For new network engineers, STP can be a difficult subject, because a good understanding of STP requires a thorough understanding of how Ethernet networking works. However, people are often being taught the fundamentals of Ethernet networking at about the same time as they're being taught about STP. This chicken-and-egg situation prevents some people from developing a deep understanding of how STP works.
The idea for this article came from that quandary. We're going to take a look at how STP operates, discussing how STP communicates between interconnected switches and how it makes state and role decisions. The topology for the three connected switches in our example is a very simple triangle with links connecting all the switches to each other, as shown in Figure 1.
Figure 1 Example of a switching topology.
Initial STP Communications
The first thing you need to understand is that when an STP switch boots, it expects to be the center of the STP universe, with the title of STP root bridge (or switch). The STP topology is based around the location of the current STP root bridge, and that location is very important in controlling how forwarding interfaces are selected.
Because each STP switch initially believes it's the root bridge, it sends out STP configuration Bridge Protocol Data Units (BPDUs) with information based on this assumption. These messages include the following information:
- Root bridge ID. Because the switch assumes that it's the root bridge, this ID initially will be the switch's local bridge ID.
- Root path cost. The best cost to the root bridge. Because the switch believes it's the root bridge, this cost initially will be 0.
- Sender bridge ID. The local switch's bridge ID.
- Port ID.
- Max age. The amount of time for which a BPDU is considered valid; by default, this is 20 seconds.
- Hello time. By default, two seconds with IEEE 802.1D.
- Forward delay. The length of time spent in the listening and learning states; by default, this is 15 seconds each.
As soon as each new switch receives BPDUs from the other switches, together the switches determine which switch should become the root bridge. This selection is based on the STP bridge ID, which is a combination of the STP priority, the VLAN, and the switch's MAC address.
When a switch receives a BPDU with a superior bridge ID, it immediately stops generating its own BPDUs and alters the BPDUs coming from the root switch to reflect the added root path cost and the sender bridge ID.
Using the switch displayed in Figure 1 as an example, switches SW1, SW2, and SW3 all initially send out BPDUs, each stating that it's the root bridge. Once SW2 and SW3 receive the BPDU from SW1, which has a lower bridge ID, they both stop originating their own BPDUs, they alter the SW1 BPDUs to reflect each of their costs to the SW1, and they send their bridge ID as the sender bridge ID. These updated BPDUs are then further propagated through each connected interface.
Determining Port States and Roles
Before we go forward we need to discuss STP port costs and port states.
STP Port Costs
Each STP port has an associated cost. Once the root bridge has been elected, these costs are used to determine which interfaces will be forwarding and which interfaces will be blocking, to avoid causing a bridging loop. The following list shows the most common interface costs:
- 10 Mbps = 100
- 100 Mbps = 19
- 1 Gbps = 4
- 10 Gbps = 2
The next few sections discuss how these costs are used.
STP Port States
STP has five different port states that determine the current condition of the port:
- Disabled. The port is not currently enabled or has failed.
- Listening. The port is currently sending and receiving BPDUs and is attempting to determine the root bridge and role of each port.
- Learning. The port is learning the addresses of the local user traffic; it places the learned addresses into its MAC address table. During this state, the port is not yet forwarding user traffic.
- Forwarding. The port has completed the listening and learning states and has transitioned to forwarding user data.
- Blocking. The port has been selected as a non-designated port (not a root or designated port). In this state, the port listens and forwards BPDUs, but doesn't forward user traffic.
Determining the Root Port
Once the STP root bridge has been elected, the next step is determining the role that each port will play in the network. There are three different options: root, designated, and non-designated. The first determines the root port. The root port on a device connects to the root bridge with the lowest STP cost, either directly or via intermediary switch(es).
In the earlier example, SW1 is the root switch, which means that all of its interfaces will automatically be designated ports. Only SW2 and SW3 will need to determine their root ports.
SW2 has two options: Using its Fa0/1 interface to SW1, or using its Fa0/3 interface through SW3 to SW1. Assuming that the switches are using the default costs, the path from SW2 to SW1 will have a cost of 19 (100 Mbps), and the path from SW2 to SW3 to SW1 will have a cost of 38 (19 + 19). Because the cost through its Fa0/1 interface has the lowest cost to the root, that will become SW2's root port.
SW3 also has two options: Going over its Fa0/2 interface to SW1, or via its Fa0/3 interface through SW2 to SW1. The path from SW3 to SW1 will have a cost of 19 (100 Mbps), and the path from SW3 to SW2 to SW1 will have a cost of 38 (19 + 19). This calculation results in SW3's Fa0/2 becoming its root port.
Determining the Designated Port
Once the root ports have been determined, the next step is figuring out which ports will become designated. A designated port connects a switch or switches to a non-root segment (from its perspective). If multiple switches connect to the same designated segment, only one of them will be selected as the forwarding switch.
Since both SW2 and SW3 in our example connect to the same non-root segment (their Fa0/3 interfaces), they must determine which of them will become the designated switch. The initial determination is done by measuring each switch's cost to the root. The switch with the lowest cost will become the designated switch for that segment. However, in this case SW2 and SW3 are tied, with a root cost of 19 each.
When a tie occurs in STP, the protocol follows a specific order of criteria until the tie is broken. The first step is to check for the lowest sender bridge ID. If that also is a tie (yes, it happens), the lowest sender port ID is used.
In our example, only a single link connects SW2 and SW3. The tie breaker is the sender bridge ID: SW2 has a bridge ID of 0200.2222.2222, while SW3 has a bridge ID of 0200.3333.3333; therefore, SW2 wins. SW2's Fa0/3 interface will become the designated port, and SW3's Fa0/3 interface will become a non-designated port, and begin blocking. At this point, the potential loop around the triangle is stopped.
Summary
Even going step by step through a simple topology, STP can become complicated quickly, causing a lot of confusion. A couple of practice options can help new engineers to nail down their understanding of STP:
- Buy a product like the CCNA Routing and Switching 200-120 Network Simulator, which includes a number of step-by-step STP labs.
- Buy a few switches (they're really cheap on auction sites) and alter the connecting interfaces and priorities.
With either option, you can create a number of different scenarios, investigating the resulting port roles and states to determine if what you believed would happen actually does happen.