You must be thinking what is spanning tree protocol and why we need it? The Spanning tree protocol is a layer 2 protocol i.e. data link layer of the OSI model. This protocol helps to avoid switching loops in the network that may lead to more bandwidth utilization, mac database instability, and broadcast storms.
Whenever there are two or more than two links between the switches for the redundancy purpose then STP comes into the picture.
It uses a spanning tree algorithm and by using it blocks the redundant links and according to the STP election process, some ports will remain active. The STP is enabled by default on all the cisco switches. There are different types of STP available:
1D: This is known as the common spanning tree protocol and it is enabled by default in all the switches. It is a standard developed by IEEE wherein the single root bridge is elected for the whole topology and other switches become a non-root bridge. It takes 30 seconds to complete the whole process of the STP election.
PVST+: This is the Per Vlan Spanning tree+ and it is cisco proprietary and by default enabled for the Cisco switches. It helps to elect the root bridge per VLAN basis. It is helpful in load balancing as we have separate root bridge for each VLAN.
1w: This is the rapid spanning tree protocol and it is an IEEE standard that works faster than a common spanning tree and comes with the less STP port states. The election process is the same here also that is one root bridge for the whole topology.
Rapid Per VLAN spanning tree+: This is the Cisco proprietary protocol and works faster than PVST and the process is the same as PVST that is the root bridge for each VLAN. It requires more CPU resources than normal STP.
1s: This is the multiple spanning tree protocols and it helps to choose the single root bridge for the group of VLANs. Hence, it provides more load balancing and faster than other versions of STPs.
How STP Root Bridge Election Process works
Let’s understand this by using the following diagram:
In the diagram we have three switches 0,1,2 and you can see the port Fa0/1 of switch2 is in blocking state. After the STP election process, that port moved into the blocking state, and if the link between switch 0 and 1 or switch 0 and 2 goes down then the port f0/2 will go into the forwarding state.
Want to know which switch becomes the root bridge?
As you can see Switch1 becomes the root bridge and all the ports of the root bridge always remain in the Designated role and state be forwarding. There are several factors through which STP chooses Switch1 as a root bridge. Following are the factors when it comes to choosing the root bridge:
- When all the three switches come online then BPDU(Bridge Protocol Data Unit) messages are shared among the three switches. BPDU includes the Bridge id and Bridge id is a combination of Bridge Priority and mac address and it is of 8 bytes total.
- The switch with the lowest Bridge Id becomes the Root Bridge. By default, the bridge priority of all the cisco switch is 32768 and if the bridge priority is the same for all the cisco switch then the mac address will be tie-breaker, the switch with low mac address will become the root bridge.
Bridge ID= Bridge Priority(2 Bytes) + Mac Address(6 Bytes)
- All the ports of Root bridge will always be in forwarding state and role always be designated.
- We may change the priority of the cisco switch if we want to make the particular switch as a root bridge.
Note: Root ID is the Root Bridge information and Bridge Id is the Information of local switch(Non-root Bridge)
The Switch0 is the non-root bridge and one port of every non-root bridge must be Root port and always in forwarding state. Now, you must be thinking about how switch0 elects the Fa0/2 as a Root port. To select the Root port see the following points:
- There are some STP costs available according to the link type:
10 Gbps-2, 1Gbps-4, 100Mbps-19, 10Mbs-100.
In our topology, we are using fast Ethernet of 100Mbps so the cost would be 19. Now, the switch0 will look for the path to reach the root bridge that is Switch1 with minimum cost. The path from f0/1 of switch0 would cost the total 38 but the path from f0/2 would cost the total 19 so that’s why switch elect f0/2 as the root port.
- After selecting the root port, now we need to select the Designated and non-Designated port. Again, we check the least cost to reach the root bridge but in our topology to reach the root bridge the cost is the same for both the ports( f0/1 in switch0 and f0/1 in switch2) i.e.19. Now we need to check the Bridge id of the sender switches i.e switch0 and switch2.
- As we can see in the above screenshots, the mac address of switch0 is lowest than the mac address of switch2. Now, in that case, the port f0/1 of switch0 will become the designated port and be in forwarding state. However, the port f0/1 of switch2 will become the non-designated port and moved to a blocking state. When the link between switch0 and switch1 goes down then the link between switch0 and switch2 will get started and f0/1 of switch2 will be moved to the forwarding state.
Difference between Root Port and Designated Port
- Every switch has only one root port and may have multiple designated ports.
- The root port is directly connected to the root bridge while the designated port is the port not directly connected to the root bridge but still forwarding traffic from the other network segment.
- Root ports listen to all the BPDUs coming from the root bridge while designated ports transmit BPDUs ahead.
STP Port States
There are five STP port states:
A disabled port is a shutdown port that means it is administratively down. In this state, the port is not allowed to do anything and we can say, STP does not run in this state.
Once that port is administratively enabled, the port will be moved to the blocking state(BLK). The port still cannot do much, no frame forwarding, no frame receiving, and therefore no dynamic learning of MAC address. In this state a port is allowed to accept BPDUs from the neighboring switches.
Listening is the next state. In this state the port is listening for BPDUs and a port is allowed to send BPDUs and can accept BPDUs, permits port to take part in the root bridge election. A port in listening mode cannot forward or receive frames and thus the port cannot learn the mac address.
Learning is the next state. A learning port is not forwarding frames but it is learning mac addresses and adding them to the switch mac address table. A port in the learning state continues to do the process of sending and receiving BPDUs.
Forwarding is the next state. A forwarding state allows a port to forward and receive frames, send and receive BPDUs and continue to learn mac address. This is the only state where the port is forwarding frames.
How BPDU generated and How BPDU works
There are three switches in our topology mentioned above and when all these switches come online then they start to share BPDU messages and after every two seconds BPDU messages are shared among the switches to select the root bridge. These BPDU messages include the Bridge Id information of every switch and helps to decide who should be the root bridge. After the selection of the root bridge, the root bridge will be allowed to generate BPDUs. All the non-root bridges will not be allowed to generate BPDUs, they can only receive and forward the BPDUs.
The Root bridge election process never ends if the new switch comes with the lowest Bridge Id then it would become the Root Bridge. Every switch in the topology sends out BPDU messages after every 2 seconds which includes the bridge id, port cost, root path cost, and many others. The non-root bridge switches in the toplogy may generate only TCN BPDUs( Topology change notification). Whenever there is a change in the topology then non-root bridges may start to generate the TCN BPDUs to inform the root bridge that is something added or removed from the topology.
Important Spanning Tree Protocol Timers and their default values
The available STP timers are Hello timer, Max age timer, and forward delay timer.
The Hello timer is by default 2 seconds. Every switch sends out BPDU messages after every 2 seconds with the values like root cost, path cost, bridge id. We may change the default hello timer and set it accordingly and to deploy the Stp timer deployment in the whole topology, you need to change it in the root bridge.
Command to change default Hello timer:
Switch1(config)#spanning-tree vlan 1 hello-time 6
In the above command, we change the hello timer to 6 seconds.
The Max-Age time timer is by default 20 seconds and it indicated for how long a switch will keep the BPDU message before discarding it.
Switch1(config)#spanning-tree vlan 1 max-age 24 In the above command we change the max-age timer to 24 seconds.The forward delay timer is by default 15 seconds. A forward delay timer is the length of the listening and learning STP states with a default of 15 seconds for every individual state.
Switch1(config)#spanning-tree vlan 1 forward-time 18
In the above command, we change the forward-timer to 18 seconds.
Topology changes in the Spanning Tree Protocol
As mentioned above in the article already that whenever there is a change in the network topology then the non-root bridges generate Topology change notification BPDUs and Topology change Acknowledgement BPDUs until it reaches the root bridge. Once the root bridge receives the Topology change notification then the root bridge generates the Configuration BPDUs with TCN bit set and it will be forwarded to every non-root bridge in the whole topology so that all may get aware of the topology change.
Spanning Tree Protocol Convergence
The STP convergence takes place when port goes to either blocking or forwarding state. When the switches first come online then it takes 30 seconds to select the root bridge and decides which port needs to be blocked. In the above topology, Fa0/2 port of switch3 is in a blocking state. If the port f0/1 goes down then Fa0/2 will move into the forwarding state and it will take 30 seconds to come into the forwarding state. If the port connected to switch is in blocking state and another port is in forwarding state then when the forwarding state port goes down then other blocking state port will take only 30 seconds to come up like in switch3.
If the port between switch0 and switch1 goes down then it will take a total 50 seconds for the port fa0/2 to come up. This is because the link between switch0 and switch1 is not directly connected to the fa0/2 port of switch3. When the port goes down then the max-age timer starts and it will wait for 20 seconds to receive BPDUs and when BPDUs are not received then port fa0/2 goes into listening and learning state which takes 30 seconds and the total is 50 seconds.
Where to place the Spanning Tree Protocol Root Switch(Root Bridge)
During the STP root bridge selection process, the switch with the lowest priority id becomes the root bridge and if the priority value is the same for all switches then the mac address comes into the picture. The switch with the lowest mac address becomes the root bridge. The default priority of the cisco switch is 32768.
Here, priority is 32769 because the switch comes under vlan 1 so plus 1 is added for the VLAN.
You need to take care of some things while placing the root bridge if you are thinking that the STP selection process will select root bridge automatically then it may lead to many problems as if your network topology has old switches and generally old switches have lowest mac address and thus as per the STP election process that old switch may become the root bridge.
As we know old switches have less CPU and hardware resources so they would not be able to handle the large network traffics and thus it may lead to packet loss, slow speed, and many more.
To avoid this, you need to make a high-end configuration switch as root bridge manually and the root bridge should be placed in the central area of the network so that it may provide the best path to all the other devices. Generally, the root bridge switch available in the distribution layer as the switches working in the distribution layer have high configuration and can optimize network traffics.
As we can see in the above screenshot, port fa0/1 of switch2 is blocked and if you go with normal STP and thus if the link fails between switch1 and switch2 then it will be approximately 50 minutes before the blocked port may come up and that is bad for the network topology.
With the help of Uplink fast feature, the port fa0/1 can be used to forward the frames without any delay i.e. immediately. The concept of the uplink group is used which includes the root port and the other blocked port like in the case of switch2.
When the port fa0/1 goes down then the port fa0/1 comes up immediately without going to listening and learning state. If there are multiple blocked ports available then the port with the lowest cost to reach the root bridge will be chosen.
We cannot enable uplink feature per-port or per-VLAN basis. It is enabled globally in the cisco switch. When the switch detects that the original root port comes up and thus the original root port would become the available path and port which comes up from the blocked state would go again into a blocked state.
When we apply the uplink feature on the switch, it does perform two immediate actions. First is, it increases the priority of switch to 49152, and second is, it increases the port cost by 3000. When the link between switch1 and switch2 goes down and thus there will be invalid entries available to send frames to PC0 and in that case, switch2 sends and floods dummy frames to switch0 and those dummy frames include every single mac address entry in switch2.
The flooding updates the mac address of switch0 and then frames are forwarded correctly.
Command to enable Uplink fast:
What is Spanning Tree Protocol Backbone Fast
The Backbone feature is a cisco proprietary and it helps the network to recover from the indirect link failures as soon as possible and the specific word here is Indirect. In the above screenshot, if the link between switch1 and switch4 goes down then it would take up to 50 seconds to come to forwarding state for fa0/1 port in switch0. In our case, switch1 is the root bridge, when the link goes down then switch4 assumes itself as the root bridge and thus switch0 receives BPDUs
from the switch1 and switch4 as the BPDUs from the root bridge. Now, switch0 does compare the priority in each BPDUs received from the switch1 and switch4 and will notice that switch4 has lower Bridge ID and thus makes switch 4 BPDUs inferior and at last ignores the BPDUs receives from Switch4. Once the max-age timer of switch0 comes to zero then the port leading to switch4 will be moved into the listening state and thus start to relay the information coming from the switch1(higher BPDU).
Backbone does not help to move the port immediately to forwarding but it does reduce the convergence time from 50 seconds to 30 seconds by eliminating the Max-Age timer.
Command to enable backbone fast
What is Spanning Tree Protocol Portfast
This Portfast feature is helpful when we want to move port from blocking mode to forwarding mode. It avoids the listening and learning state of the port. We usually enable Portfast for the end hosts that are connected to the switches and ports are known as access ports connected to end hosts. In which scenario we enable Portfast in the end hosts? If the end hosts are having some problems to get the IP address from the DHCP server and thus we enable Portfast to avoid listening and learning state. You cannot enable Portfast for the trunk ports.
Command to enable Portfast
Switch2(config)# spanning-tree portfast
We may enable it per-port basis also.
How to configure and verify Spanning Tree Protocol Portfast
When the pc first connects with the switch then it goes to perform the listening and learning state to make the port in forwarding mode. You can see in the screenshot, first, it goes to the listening state for the 15 seconds, and afterward learning state for the 15 seconds that is total would be 30 seconds.
If we enable the Portfast feature then the port fa0/2 will be moved into the forwarding state without any delay.
How to enable and disable spanning tree protocol
We may enable or disable the Spanning tree protocol. However, the STP is by default ON in all the Cisco switches. It is recommended to disable the Spanning tree protocol as it creates switching loops and may lead to mac database instability and broadcast storms.
Following are the commands to enable and disable the spanning tree protocol :
Enter configuration commands, one per line. End with CNTL/Z.
Switch0(config)#spanning-tree vlan ?
WORD vlan range, example: 1,3-5,7,9-11
Switch0(config)#spanning-tree vlan 1
Enter configuration commands, one per line. End with CNTL/Z.
Switch0(config)#no spanning-tree vlan ?
WORD vlan range, example: 1,3-5,7,9-11
Switch0(config)#no spanning-tree vlan 1
Switch0(config)#do sh spanning-tree
No spanning tree instance exists.
After disabling you may verify it by using the command Show spanning-tree.