1 - Basic concepts
Spanning Tree Protocol (STP) is a OSI layer 2 protocol (Data Link).
Its function is to deal with loops presence due to redundant links existence. This protocol grants switches the ability to enable or disable automatically the connection links guaranteeing a loop-free topology.
Imagine two LANs interconnected. Host n is sending a Frame F to an unknown destination.
This is what would happen:
- Bridge A sends this frame to LAN 2.
- Bridge B sees frame F in LAN 2 (with unknown destination) and sends it to LAN 1.
- Bridge A does the same and the loop begins.
When there are loops in our network topology, data link layer devices resend frames via broadcast and multicast, since there's no TTL field in layer 2 like in layer 3. A great amount of bandwidth gets consumed and, in many cases, the network is no longer working. Solution consists in allowing redundant physical links existence, but creating a loop-free logic topology. STP allows only one active way at a time between two network devices (this prevents loops from happening) but keeps redundant links as reserved to activate them in case something fails in the main way.
Spanning tree is valid until any change happens in the topology which it detects automatically. STP maximum duration is 5 minutes. When one of these changes ocurred, root bridge redefines STP topology or a new root brigde is elected.
2 - How it works
This algorithm changes a mesh physical network with loops to a logic tree-shaped loop-free one. Bridges talk to each other using configuration messages called Bridges Protocol Data Units (BPDU).
3 - Ports status
- Blocking Port can only receive BPDUs. Frames are discarded and MAC address table doesn't get updated.
- Listening: The switch processes BPDUs and awaits possible new information that would cause it to return to the blocking state.
- Learning: While the port does not yet forward frames (packets) it does learn source addresses from frames received and adds them to the filtering database (switching database).
- Forwarding: A port receiving and sending data, normal operation. STP still monitors incoming BPDUs that would indicate it should return to the blocking state to prevent a loop.
- Disabled: Not strictly part of STP, a network administrator can manually disable a port
4 - STP in theory
Algorithm has three parts and it requires that every switch has an ID and that it may know each port status:
- Bridge with smaller ID gets elected as root.
- Each bridge calculates shortest path to root bridge and marks that port as a root port.
- For each LAN, every bridge connected to it must agree in which of them will be the designated. In order to it, BPDUs are interchanged. Designated bridge will be:
- Closest to root bridge or...
- Closest to root bridge and with lowest ID.
4 - STP in real life
Bridges syncronize sending each other packets called BPDU that contain:
- Sender ID
- Suspected root bridge ID.
- Distance between switch and root bridge.
If you still have some doubts, please, take a look at the following video that Radia Perlman, inventor of the STP algorithm, created.