STP uses the Spanning-Tree Algorithm (SPA) to create a topology database of the nework. To prevent loops, SPA places some interfaces into forwarding state and some interfaces into blocking state. How does STP decides in which state to port will be placed? A couple of criteria exist:
1. all switches in a network elect a root bridge (switch). All working interfaces on the root bridge are placed in forwarding state. The switch with the lowest switch ID will become the root bridge.
2. all other switches, called “nonroot bridges”, determine the best path to get to the root bridge. The port used to reach the root bridge (root port) is placed in forwarding state.
3. on the shared Ethernet segments, the switch with the lowest cost to reach the root bridge is placed into forwarding state.
4. all other interfaces are placed in blocking state and will not forward frames.
An example will help you understand the concept.
Let’s say that SW1 advertised the lowest switch ID and is elected as the root bridge. All ports on SW1 are placed into forwarding state. SW2 and SW3 choose ports with the lowest cost to reach the root bridge to be the root ports. These ports are also placed into forwarding state. On the shared Ethernet segment between SW2 and SW3, port Fa0/1 on SW2 has the lowest cost to reach the root bridge. This port is placed into forwarding state. To prevent loops, port Fa0/1 on SW3 is placed into blocking state.
BPDUs (Bridge Protocol Data Units) are used by switches to share information with each other and learn the topology of the network. With BPDUs, the loops in the network are detected. BPDUs are compared and used to elect a root switch. Hello BPDUs are the most common messages. They list the switch ID of the sender and the root bridge ID.