OBJECTIVE
The purpose of this post is to present an introduction to the STP Topology Calculation.
After all devices on the network are enabled with STP, each device considers itself the root bridge. Each device only transmits and receives BPDUs but does not forward user traffic. All ports are in the Listening state. After exchanging configuration BPDUs, all devices participate in the selection of the root bridge, root port, and designated port.
BPDU Exchange
As shown in Figure 1, the quadruple marked with {} indicates a set of ordered vectors: root BID (S1_MAC and S2_MAC indicates the BIDs of two devices), total path costs, sender BID, and Port ID. Configuration BPDUs are sent at intervals set by the Hello timer.

Figure 1 - Exchange of initialization messages
STP algorithm imlementation
1. Initialization
As each bridge considers itself the root bridge, the value of the root BID field in the BPDU sent by each port is recorded as its BID. The value of the Root Path Cost field is the accumulative cost of all links to the root bridge; the sender BID is the ID of the local bridge; the Port ID is the PID of the local bridge port that sends the BPDU.
2. Root bridge selection
During network initialization, every device considers itself as the root bridge and the root bridge ID as the device ID. Devices exchange configuration BPDUs to compare the root bridge IDs. The device with the smallest BID is elected as the root bridge.
3. Root port and designated port selection
Table 1 lists the process of selecting the root port and designated port.
| No. | Procedure |
| 1 | The non-bridge device uses the port that receives the optimal configuration BPDU as the root port. Table 2 lists the process of selecting the optimal configuration BPDU. |
| 2 | The device calculates a BPDU for each designated port based on the BPDU and path cost of the root port.
|
| 3 | The device compares the calculated BPDU with the BPDU on the port:
|
Table 1 - lists the process of selecting the root port and designated port
| No. | Procedure |
| 1 | Each port compares the received BPDU with its BPDU:
|
| 2 | The device compares BPDUs on all the ports and selects the optimal BPDU. |
Table 2 - Selecting the optimal BPDU
STP Calculation Example
When the root bridge, root port, and designated port are selected successfully, the whole tree
topology is set up. The following example describes STP calculation.

Figure 2 - STP networking and topology after
As shown in Figure 2, priorities of DeviceA, DeviceB, and DeviceC are 0, 1, and 2, and the path costs between DeviceA and DeviceB, between DeviceA and DeviceC, and between DeviceB and DeviceC are 5, 10, and 4 respectively.
1. Initial state of each device
Table 3 lists the initial state of each device.
| Device | Port Name | BPDU |
| DeviceA | PORT A1 | {0, 0, 0, Port A1} |
| PORT A2 | {0, 0, 0, Port A2} | |
| DeviceB | PORT B1 | {1, 0, 1, Port B1} |
| PORT B2 | {1, 0, 1, Port B2} | |
| DeviceC | PORT C1 | {2, 0, 2, Port C1} |
| PORT C2 | {2, 0, 2, Port C2} |
Table 3 - Initial state of each device
2. Comparison and result
Table 4 lists the comparison and result.
The fields in the BPDU represent {root bridge ID, accumulative root path cost, sender BID, transmit port ID PID}.
| Device | Comparison | BPDU After Comparison |
| DeviceA |
|
|
| DeviceB |
|
|
|
| |
| DeviceC |
|
|
|
| |
|
| |
|
|
Table 4 - Topology calculation and result
After the topology becomes stable, the root bridge still sends configuration BPDUs at intervals set by the Hello timer. Each non-root bridge forwards the received configuration BPDUs by using its designated port. If the priority of the received BPDU is higher than that on the non-root bridge, the non-root bridge updates its own BPDU based on the information carried in the received BPDU.
STP Topology Changes
Figure 3 shows the packet transmission process after the STP topology changes.

Figure 3 - Diagram of packet transmission after the topology changes
After the network topology changes, a downstream device continuously sends TCN BPDUs to an upstream device.
After the upstream device receives TCN BPDUs from the downstream device, only the designated port processes them. The other ports may receive TCN BPDUs but do not process them.
The upstream device sets the TCA bit of the Flags field in the configuration BPDUs to 1 and returns the configuration BPDUs to instruct the downstream device to stop sending TCN BPDUs.
The upstream device sends a copy of the TCN BPDUs to the root bridge.
Steps 1, 2, 3 and 4 are repeated until the root bridge receives the TCN BPDUs.
The root bridge sets the TC bit of the Flags field in the configuration BPDUs to 1 to instruct the downstream device to delete MAC address entries.

TCN BPDUs are used to inform the upstream device and root bridge of topology changes.
Configuration BPDUs with the TCA bit being set to 1 are used by the upstream device to inform the downstream device that the topology changes are known and instruct the downstream device to stop sending TCN BPDUs.
Configuration BPDUs with the TC bit being set to 1 are used by the upstream device to inform the downstream device of topology changes and instruct the downstream device to delete MAC address entries. In this manner, fast network convergence is achieved
--- End



