STP or Spanning Tree Protocol is a protocol used on switches that prevents Layer 2 Switching Loops and Layer 2 Broadcast Storms. This is very necessary in large networks that are designed to have redundancy like backup switches and multiple paths across the network. Redundancy is a great thing for a network to have, but it can create physical loops by having more than one physical path or link across the network. When there are redundant links or paths for data to travel, then the data can circulate back around the network repeatedly in a layer 2 switching loop. This has the potential to effectively shutdown the network, because unlike packets at Layer 3 which have TTL values and a limited lifespan on the network, Layer 2 broadcasts do not have TTL values and will therefore loop endlessly on the network.
STP solves this problem by automatically blocking redundant or duplicate paths (ports) from switch to switch, thus closing the loops. If a path to a switch becomes unavailable, STP can reopen a closed port creating a new path. For this process to work, switches in the network communicate with one another, and share STP messages, called BPDUs, or Bridge Protocol Data Units.
The Root Bridge
The root bridge is the switch that is at the top of the Spanning Tree. The Spanning Tree Algorithm (STA) calculates the costs of all the paths in the network starting from the root bridge. The root bridge is determined by an election process in which the switch with the lowest bridge ID (BID) is elected root bridge. The BID is determined by two factors:
1. The BID is the lowest bridge priority number plus the VLAN ID, which by default would be 32768 plus the default VLAN 1, would be 32768+1=32769
2. or, if the BIDs on all the switches are identical. The switch with the lowest MAC Address becomes the root bridge
How to pick the root bridge?
It is a good idea to not leave the root bridge election process up to the mere chance of a switch having the lowest MAC address. Ideally the root bridge should be a robust switch at the center of the network close to resources that users will need access to. The thing to remember is that the root bridge will block ports in order to close off loops, creating paths for traffic to flow. These layer 2 paths need to be as short as possible. For STP to function properly, the end-to-end layer 2 network diameter should be no more than 7 switches. If there are more than 7 switches for data to traverse in the network, then the STP timers will not function correctly and ports could start flapping from forwarding to blocking which would create loops in the network.
The root bridge calculates which paths are the best paths in the network and which paths are redundant paths, needing to be blocked. This is done by assigning a cost value based on the speed of the switchports. So, if the root bridge is connected to another switch by way of a gigabit port, the cost for that link is assigned a value of a 4, and if that switch is connected to another switch by way of a fast ethernet port then that link is assigned a value of a 19. The entire path cost is 4 + 19 = 23.
The cost of the link is based on the speed of the port. Here is the list of port costs:
10 Gig = 2,
1 Gig = 4,
100 Mb = 19,
10 Mb = 100
switch#debug spanning-tree events
Lowering the bridge priority number can change the root bridge election process because the switch with the lowest bridge priority number is elected the root bridge. The default bridge priority number is 32768. In the first command below, you can manually change the bridge priority number. The following two commands automatically set the priority number to a lower than default predefined number
switch(config)#spanning-tree vlan 1 priority <num>
switch(config)#spanning-tree vlan 1 root primary
switch(config)#spanning-tree vlan 1 root secondary
The following commands allow you to manually change the cost of a port, which is normally based on port speed. This can change which ports, and therefore paths, that the root bridge will elect to block in order to close a loop.
switch(config-if)#spanning-tree cost <num>
The following commands change the port priority number which can change which ports, between two designated ports, on the same segment, will be put into a blocking state. To do this, you will need to lower the priority number to a number lower than the default of 128.
switch(config-if)#spanning-tree port-priority <num>
or in Packet Tracer,
switch(config-if)#spanning-tree vlan <num> port-priority <num>
In part 1, I demonstrate how STP prevents Broadcast Storms in large networks with redundant switches
In part 2, I discuss the BID -bridge ID, and the STP root bridge election process