Classic Spanning Tree (802.1d) has 3 steps of convergence to provide a loop free switched network.
1. Elect one root bride
2. Elect one root port per non root bridge
3. Elect one designated port per network segment
But the logic behind all the above 3 steps are very simple and straight forward.
It is the logic of identifying the superior BPDU (Bridge Protocol Data Unit - L2 data frames which contains STP information).
Following is the format of a BPDU..
Switches compare the BPDUs received on their ports and determine which one is better. They are compared in the following order, lower the value is better..
1. Root Bridge ID
2. Root Path Cost
3. Sender Bridge ID
4. Sender Port ID
5. Receiver Port ID (not included in the BPDU, evaluated locally)
Also each port in STP stores the superior BPDU that it has either sent or received. Only the Designated Ports send superior BPDUs while Root Ports & Alternate Ports only receive BPDUs.
Note:- There are 2 types of BPDUs called Configuration BPDUs and TCN BPDUs but in initial convergence, only the Configuration BPDUs will be involved.
Let's power on a switched network..
MAC Addresses are;
SW1: aabb.cc00.1000
SW2: aabb.cc00.2000
SW3: aabb.cc00.3000
SW4: aabb.cc00.4000
SW5: aabb.cc00.5000
STEP 1 : Root Bridge Election
When the switches are turned on each switch thinks it is the root bridge and sends BPDUs out of its ports to neighboring switches..
Because each switch claims itself as root bridge at the initial stage, Root ID & Bridge ID are same.
Bridge ID is composed of Bridge Priority & MAC Address..
The Root Bridge is the switch which generates the most Superior BPDUs
Of cause it is the switch with the lowest Bridge ID. Finding the lowest Bridge ID has the following order..
1. Lowest Bridge Priority.
2. Lowest MAC Address.
Default Bridge Priority value is 32768 + system extension ID.
System extension ID is nothing but the VLAN number..
Priority can be changed by the increments or decrements of 4096..
I did not change the priority of above switches, so SW1 which has the lowest MAC address became the root bridge..
When 2 switches exchange their BPDUs and realize one is superior than the other, the inferior BPDU sending switch determines itself as the non-root switch and stops sending it's own BPDUs out of its ports and start relaying the Root's BPDUs downstream to the other switches.
If we see the
show spanning-tree output of SW1;
You can see it says "This bridge is the root" & all the ports are Designated Ports..
STEP 2 : Root Port Election per Non Root Bridge
Non Root Bridges elect a one port which is best to reach the Root Bridge based on link cost.
Cost calculation is just like in OSPF..
Root port is the port which receives the Superior BPDU from the Root switch..
Because the Root ID of the BPDU is same now, the Root Path Cost will be the used to find the Superior BPDU at 1st. If the Root Path Cost is equal, Sender Bridge ID, Sender Port ID, Receiver Port ID will be used to find the superior BPDU..
Let's see a
show spanning-tree output of a non root bridge like SW5;
E0/3 has become the Root Port. Why is that?
You can see the cost value of E0/3 is 19 but this is not the actual value the decision was taken. Cumulative cost to the Root Bridge was the real value which was taken into the calculation. This 19 is the local value for this switch. To see the root path cost we will have to see the
show spanning-tree detail output for the E0/3 interface..
You can see it says "cost to the root path is 119"
Let's see a
show spanning-tree output of another non root bridge like SW2;
E0/3 has become the Root Port and E0/0 has become the Alternate. Why is that?
In the diagram, you can see the Cost & the Upstream Bridge ID is equal, so the last tie breaker is the Upstream Bridge Priority.. You cannot decide it by looking the above output because it shows the local interface priorities. So you have to see the
show spanning-tree detail output..
You can see the port priority values which were taken to the calculation now on the above output.
STEP 3 : Designated Port Election per Network Segment
Each network segment has one Designated Port to forward traffic. These ports are the downstream ports from the Root Bridge. Other ports in the segment (if not a Root Port) becomes Alternate Ports which blocks the traffic.
Although Alternate Ports block data traffic they still receive BPDUs..
When all ports of switches become either forwarding or blocking, we call STP is converged and data can be transmitted on wards..
Designated port is the port which sends the Superior BPDU into the segment..
Because the Root ID of the BPDU is same now, the Root Path Cost, Sender Bridge ID, Sender Port ID, Receiver Port ID will be used to find the superior BPDU..
So considering the above rules, following ports will be blocked..
Finally the diagram will logically look like the following..
STP Port States, Timers & Roles
When switches are powered on, STP goes from
Blocking > Listening > Learning > Forwarding
States..
Here is a quick summary of the Port States..
Timers are set by the Root Switch..
Hello Time is the time frequency which the Configuration BPDUs are sent.. Default is 2s.
Max Age is the time which a BPDU is considered valid by the Root switch.. Default is 20s.
MessageAge is the BPDU's age since it was originated by the Root switch. At the root switch it is set to 0 and other switches increment this value by 1 before forwarding further..
Therefore remaining lifetime of a BPDU after receiving by a switch is (MaxAge - MessageAge)
Forward Delay is the time which it takes to move from Listening State to Learning State and Learning State to Forwarding State.. Default is 15s.
Aging Time is the time which a CAM table entry will be valid.. Switches flush the MAC Address entry from it's CAM table after this time expires.. Default is 300s.
Port Roles are
Designated, Root & Alternate..
Designated & Root Ports are opened for the traffic to flow through.. Alternate Ports will be in Blocking State and will be changed to
Listening > Learning > Forwarding when a Designated Port is failed and STP decides to forward traffic through it..
It will take 20 + 15 + 15 = 50 Sec to forward traffic by default..
If you want to know how Classic Spanning Tree deals with topology changed please go
here.