Spanning Tree Protocol (STP) is a Layer 2 protocol that runs on bridges and switches to prevent loop by shutting down selected bridge interfaces that make a loop happen. Loop happen when we have 2 path between two network devices. So spanning Tree job is to make sure only one active path between them.
You may wonder what is loop? loop is the action that happen again and again never end. It's the same as your everyday life for example eat->work->sleep->eat-> work->sleep …until die.
What happen when the link between Sw1 to Sw2 was cut? Host1 will not able to communicate with Host2. So you may think to add another link between Sw1 and Sw2 for protection. Now we add one more link between SW1 and SW2.
Let's think what will happen when Host1 send packet to Host2. The following action will happen:
1. Because Host1 think Host2 is in the same network, Host1 send arp request for layer3 address.
2. SW1 check in it's CAM table (Content Addressable Memory is a cisco term for what's more generically called Mac address table). But Mac address of Host2 is not in it's CAM table, so SW1 send broadcast to all interface(F0/1 and F0/2).
3. SW2 receive arp request about Host2 on F0/1 and it's check Layer2 address in CAM table but not found Mac address of Host2 in list in Mac address table. So it broadcast arp request to all port(F0/2 and F0/3) except the port it's receive arp reqest. You see ! Now arp reqest send back to SW1 and SW1 do the same as step2. Both switch will do the same again and again.
How it work?
The goal of STP is to disabling unwanted links and blocking ports that could cause layer 2 loop. There are three step process. Firstly STP find the reference point which is the root bridge. Then other switch(non-root bridge) select one of it's port that is shortest path to the root bridge to become a root port(RP). After that it find designated port(DP) in each segment. The port that's non-root port and non-designated port become a blocking port.
Root bridge election
All non-root bridge decision is base on the root bridge. It's the reference point how other switch build the topology. That why STP algorithm need to elect the root bridge first. The one who has lowest bridge ID become the root bridge. Firstly all switch promote it self to be a root bridge and send hello bridge protocol data unit(BPDU) message. If a switch receive a superior Hello(Hello with a lower bridge ID), it stop originate hello BPDU and it consider the switch that originate the superior hello to be it's root bridge. For instead it start forwarding the superior Hello receive from the superior switch.
The orignal IEEE 802.1d bridge ID held three fields:
- The 2-byte priority fields from 0 to 61440 in increment of 4096. We can change the default value in the configuration. It's effect the result of STP election process.
- A 6-byte MAC address field, which was included as a tiebreaker.
- System ID Extension increment from 0 to 4095. It's also called MAC address reduction, because we don't need separate MAC address on each switch for each STP instance.
Note: All port of root bridge is designated port
Root Port Selection
Root Port(RP) is a lowest path cost to Root Bridge. Normally Root Port is the port towards facing the root bridge. Similar to OSPF, the cost of RP is based on inverse bandwidth. The higher bandwidth, the lower cost.
- 10Mbps link is 100
- 100Mbps link is 19
- 1Gbps link is 4
- 10Gbps link is 2
- choose lowest upstream BID
- choose lowest upstream Port ID
If the cost of links are the equal, it will choose the one with the lowest upstream BID. If upstream BID still the same, it will choose the one with the lowest upstream Port ID.
Firstly Root Bridge create and sends Hello BPDU every hello timer (2 seconds default). Each switch that receive a Hello BPDU will update the following fields in the Hello: the cost, the forwarding switch's bridge ID, forwarder's port priority, and forwarder's port number. And it continue forward the Hello. The port cost will calculate by adding the cost value listed in the Hello message and the switch's STP port costs (The port that Hello message was received). Then the swich will examine which port has the least cost to the root bridge and it promote to be root port.
Let's in the topology below
Because link between each switch is Ethernet(10Mbps) so the cost is 100. On SW2 has two link that can reach root bridge on e0/0 and e0/1. Interface e0/0 is direct connected to the root and port cost is 100. Interface e0/1 can reach root bridge by go through SW3. When Hello receive on e0/0 of SW3, it update the port cost 0+100 then it forward to SW2. SW2 calculate port cost by adding port cost in Hello that it receive and port cost of the interface that receive hello message (100+100=200). So SW2 will choose eth0/0 to be a Root Port(RP) because it has lowest port cost. SW3 will act the same as SW2. On SW4 has two link that has the same cost to SW2. So it check lowest upstream BID. hmm BID is still the same. So check another tiebreaker, the upstream Port ID. Because interface e0/1 has lowest upstream Port ID, e0/1 become the Root Port(RP).
Designated Port Selection
Designated Port(DP) is the port that used to forward frames on to a LAN segment or a port that is downstream facing away from Root Bridge. The election is similar the Root Port.
- Lowest root path cost
- Lowest BID
- Lowest Port ID
Ok let's see in the diagrame below
Now let's see the link between SW2 ans SW3 which port will be the designated port. SW1 advertise port cost 0 to SW3 and SW3 add cost 100 to reach the Root Bridge. SW3 advertise 100 to downstream. The same as SW3, SW2 advertise 100 to downstream. The path cost is the same. Now let's consider the BID. SW2 has lower BID so port of SW2 will become designated port(e0/1).
All other port beside Designated Port and Root Port will become blocking port. Blocking port cannot send traffice, discard all other traffice, but able to receive BPDUs.
In STP there are five different port state:
- Blocked: A port in blocked state will remain there for 20 seconds by default(The maximum age timer). During this state, the port is not able to forward the frame and also discard the frame receive from attached segment. The port is only listening to and processing BPDUs on its interfaces.
- Listening: The port will stay in this state for the length of the forward delay timer (by default is 15 sec). After blocking state, the Root Port or Designate port will move to Listening state. In this state the port still not able to forward the frame and discard the frame it receive from attached segment. At this state, the port still listening to BPDU and directs them to the switch system module for processing.
- Learning: At this state the port sill listening for and processing BPDUs on the port. The port start to process user frames, When processing user frames, the switch is examining the source address in the frame and updating it's CAM tables. But the switch is still not forwarding these frames out desinated ports. After the length of the forward delay timer(15 sec by default), the swich port move from learning to forwarding state.
- Forwarding: is a normal state. The port will process BPDU, update it's CAM table with frame it's receive and forward user frame through the port.
- Disabled: The port with disable state does not participate in frame forwarding or STP operation. The port in this state could be because the port has been manually shout down by administrator, manually remove from STP or because of physical layer problem.