How STP Works

Spanning Tree provide a loop-free network. When a switch supported STP recognize a loop in the network topology, it blocks one or more redundant ports. Spanning Tree Protocol continually explore the network, so when the network topology changes, STP automatically reconfigure switch ports to  void the failure by blocking certain port.
Spanning tree algorithm aware switches (bridges) exchange configuration messages periodically. The configuration message is a multicast frame called BPDU (Bridge Protocol Data Unit) or Hello message. According to BPDU, these STP aware will construct a loop free network with “tree” architecture. STP operation is listed as the following:

1. Select a root bridge
Only one switch/ bridge can be selected as the root bridge in a given network. All other decisions in the network, such as which port is blocked and which port is put in forwarding mode, are made regarding this root bridge. The root bridge is the “root” of the constructed “tree”.

1) One of the important field included in the BPDU is the bridge ID. Each bridge has unique bridge ID. The root bridge is the bridge with the lowest bridge ID in the spanning tree network.
2) The bridge ID includes two parts, bridge priority (2 bytes) and bridge MAC address (6 bytes). The 802.1d default bridge priority is 32768. For example, a switch with default priority 32768 (8000 hex), MAC address is 00:A0:C5:12:34:56, its bridge ID is 8000:00A0:C512:3456.
3) On the root bridge, all its ports are designated ports. Designated ports are always in the forwarding state. While in forwarding state, a port can receive and send traffic.

2. Select a root port for the non-root bridge
For the non-root switch/bridge, there will be one root port. The root port is the port through which this non-root switch / bridge communicates with the root bridge (the “leaf” side of the “tree”).
1) The root port is the port on the non-root bridge with the lowest path cost to the root bridge. The root port is normally in forwarding state.
2) Path cost is the total cost of transmitting a frame on to a LAN through that port to bridge root. It is assigned according to the bandwidth of the link. The slower the media, the higher the cost. Some of the path costs specified in the IEEE 802.1d specification are listed below.
plugin-ES-2024A_5
3) When multiple ports have the same path cost to root bridge, the port with lowest port priority is selected as root port.

3. Select a designated port on each segment
For each LAN segment (collision domain), there is a designated port. The designated port has the lowest cost to the root bridge. Designated ports are normally in the forwarding state to forward and receive traffic to the egment. If more than one port in the segment have the same path cost, the port on which bridge has lowest bridge ID is selected as a designated port.
1) How STP works
After STP determines the lowest cost spanning tree, it enables all root ports and designated ports, and disables all other ports. Network packets are therefore only forwarded between root ports and designated ports, eliminating any possible network loops. STP-aware devices exchange Bridge Protocol Data Units (BPDUs) periodically. When the bridged LAN topology changes, a new spanning tree is constructed.
Once a stable network topology has been established, all bridges listen for Hello BPDUs (Bridge Protocol Data Units) transmitted from the root bridge. If  a bridge does not get a Hello BPDU after a predefined interval (Max Age), the bridge assumes that the link to the root bridge is down. This bridge then
initiates negotiations with other bridges to reconfigure the network to re-establish a valid network topology.
For example:
plugin-ES-2024A_6

plugin-ES-2024A_71. Switch A bridge ID = 8000:00A0:C511:1111, Switch B bridge ID = 8000:00A0:C522:2222, Switch C bridge ID = 0001:00A0:C533:3333. Switch C has the lowest bridge ID, so Switch C is the root bridge. All ports of the root bridge are designated ports, so Port 1 is designated port.
2. For non-root bridge Switch A, Port 1 path cost to root bridge is 19, Port 2 path cost is 119, 100 (Switch A Port 2) + 19 (Switch B Port 1). For Switch B, Port 1 path cost is 19, Port 2 path cost is 119. Root port = Port 1 of Switch A and Switch B because it has the lowest path cost to the root bridge Switch C.
3. On Segment A, both Port 2 of Switch A and Switch B have the same path cost to root bridge. Since Switch A has lower bridge ID than Switch B, the designated port is selected on Switch A. So Port 2 of Switch A is designated port.

Blocking = Port 2 of Switch B, the non designated port on the segment.
Forwarding = All designated ports and root ports.

sursa: ftp://ftp2.zyxel.com/ES-2024A/support_note/ES-2024A_3.60.pdf

What is Spanning Tree Protocol

Spanning Tree Overview

Spanning-Tree Protocol (STP) is a Layer 2 protocol designed to run on bridges and switches. The specification for STP is defined in IEEE 802.1d. The main purpose of STP is to ensure that you do not run into a loop situation when you have redundant paths in your network. STP detects/disables network loops and provides backup links between switches or bridges. It allows the device to interact with other STP compliant devices in your network to ensure that only one path exists between any two stations on the network.
The redundant topology without STP will cause the following problem:

1. Broadcast storm: Without Spanning Tree loop avoidance mechanism, each switch will endlessly flood broadcast packets to all ports. This situation is called broadcast storm.

1) When Host sends a broadcast frame, like an ARP request to Router,
the frame will be received by Switch A.

2) Switch A identify the destination MAC address field (broadcast
FF:FF:FF:FF:FF:FF) in the frame and determine to flood it onto Segment B.

3) When the broadcast frame arrives at Switch B, Switch will repeat
above process, flood it to Segment A.

4) The broadcast frame will endlessly travel around the loop network
even Router has already received this frame.
ES-2024 Series Switch Support Notes
plugin-ES-2024A_3

2. Filtering Database Instability:
When multiple copies of a frame arrive at different ports of a switch, the MAC entry instability in Filtering Database will occur.

1) Host sends an unicast frame to Router (source MAC address is Host’s MAC, destination MAC address is Router’s MAC). Both Switch A and Switch B will receive this frame and learn MAC address of Host on Port 2.

2) Switch A has not yet learned the MAC address of Router. So Switch
A will flood a copy of the received frame to Segment B.

3) When the copy of the frame from Switch A arrives at Switch B,
Switch B will remove the first entry (Host MAC address on Port 2) in
Filtering Database and add a new mapping of Host MAC address
on Port 1. Switch B incorrectly learn Host MAC address on Port 1.
Switch B can’t forward frames properly because the instability of
mapping MAC address to Port.
plugin-ES-2024A_4

sursa:ftp://ftp2.zyxel.com/ES-2024A/support_note/ES-2024A_3.60.pdf