Bridges and Switches: MAC Learning, Spanning Tree and Its Successors
How a switch learns where each MAC address lives, floods frames it can't place and forgets stale entries; why a loop of switches melts down without a spanning tree; how STP elects a root, assigns port roles and reconverges; and what RSTP, MSTP, MRP and modern data center fabrics changed.
kvi5hnuCode Sprout team member20 min readOn this page
A bridge connects LAN segments and forwards frames between them based on their MAC addresses. A switch is the same thing with many ports and forwarding done in hardware. The two words name one device, and the standards (802.1Q today) still say "bridge". This article looks at how a bridge decides where to send a frame, what goes wrong when bridges form a loop, and how the Spanning Tree Protocol and its successors prevent that.
Learning, flooding and ageing
A bridge doesn't need to be configured with the location of each station. It learns by watching traffic:
- Learn. When a frame arrives on a port, the bridge records "this source MAC address is reachable through this port" in its filtering database (also called the forwarding database, FDB, or MAC address table).
- Forward or flood. It looks up the destination MAC. If it's known, the frame goes out only on that port, and if that port is the one it arrived on, the frame is dropped (filtered). If the destination is unknown, or is a broadcast or multicast address, the bridge floods the frame out every port except the arrival port.
- Age. Each learned entry has a timer that is reset whenever the station sends again. An entry not refreshed within the ageing time is deleted, so a station that moves to another port is found again. The standard default, also used by Linux, is 300 seconds.
The lab builds a Linux bridge sw with three hosts and, to make ageing visible quickly, sets the ageing time to 10 seconds. IPv6 is disabled so that no background traffic teaches the bridge anything early. Before any traffic, the table holds no learned entries:
(no learned entries yet)
h1 already knows h2's MAC address (it was configured statically), so its first ping goes out as a unicast frame to 02:00:00:00:00:02. The bridge hasn't seen that address yet, so it floods the frame, and even h3, which has nothing to do with the conversation, receives a copy:
02:00:00:00:00:01 > 02:00:00:00:00:02, ethertype IPv4 (0x0800), length 98: 192.0.2.1 > 192.0.2.2: ICMP echo request, id 63, seq 1, length 64
Only the first request reached h3. When h2 replied, the bridge learned h2's port, and the remaining requests went only to p2. The table now holds both addresses. dev p1 is the port the address was learned on and master br0 names the bridge. The second listing, from bridge -s fdb show, adds used 1/1: the seconds since the entry was last used and last updated:
02:00:00:00:00:01 dev p1 master br0
02:00:00:00:00:02 dev p2 master br0
02:00:00:00:00:01 dev p1 used 1/1 master br0
02:00:00:00:00:02 dev p2 used 1/1 master br0
After more than 10 seconds of silence:
(learned entries have aged out after 10 s without traffic)
On Linux, bridge fdb show lists the table. The entries marked permanent (left out above) are the bridge's own port addresses. ip link set br0 type bridge ageing_time 30000 sets the ageing time in hundredths of a second.
Why loops are a disaster
Networks need redundant links so that one failure doesn't cut anyone off. But a bridge forwards a flooded frame out every port, and an Ethernet frame has no hop count or TTL (time to live, the counter in the IP header that makes looping packets expire). In a loop, a broadcast goes around forever, and each bridge with more than two ports multiplies it. The bridges' tables also go wrong, because the same source address keeps arriving on different ports. This is a broadcast storm.
The lab connected three bridges in a triangle with the spanning tree turned off and sent a single broadcast ARP request:
one broadcast ARP request sent by h2; frames received by b1 on its link from b2:
after 0.2 s: 61,521
after 0.7 s: 186,880
after 1.1 s: 297,457
after 1.6 s: 434,370
after 2.0 s: 545,332
average rate in the last second: 277,272 frames per second
after breaking the loop, frames in the next 0.5 s: 0

One frame turned into hundreds of thousands of frames per second on a single link, limited only by how fast the lab's CPU could copy them (the exact rate changes from run to run). On hardware switches, a storm fills every link at line rate within milliseconds, the switches' CPUs are flooded with broadcasts, and the whole LAN stops working. It continues until someone breaks the loop: the last line shows the counter stopping the moment one link went down. So bridges must have a way to use only a loop-free subset of the links.
The Spanning Tree Protocol
The Spanning Tree Protocol (STP) turns an arbitrary graph of bridges and links into a tree: every bridge stays reachable, but there's exactly one active path between any two of them. Ports that would close a loop are put in a blocking state, where they don't forward data frames. If an active link fails, a blocked port takes over.
STP was defined in 802.1D. In 2004, the rapid version, RSTP (Rapid Spanning Tree Protocol), replaced it in that standard, and since 2014 all of 802.1D's spanning tree material has been part of 802.1Q (current revision 802.1Q-2022). The protocol and its frames are still widely called STP.
BPDUs and bridge IDs
Bridges exchange Bridge Protocol Data Units (BPDUs), sent to the reserved multicast address 01:80:c2:00:00:00, which bridges consume rather than forward. Each bridge has a bridge ID: a 2-byte priority followed by its 6-byte MAC address. The lowest ID wins elections, so the priority lets an administrator choose. The default priority is 32768 (0x8000). Modern switches use the bottom 12 bits of the priority field as a system ID extension, usually the VLAN or spanning tree instance number, so priorities are set in steps of 4096.
Each link has a path cost, roughly inversely proportional to its speed. The original values were 100 for 10 Mb/s, 19 for 100 Mb/s, 4 for 1 Gb/s and 2 for 10 Gb/s. Because that scale runs out above 10 Gb/s, 802.1t introduced 32-bit costs computed as 20,000,000 divided by the speed in Mb/s: 20,000 for 1 Gb/s, 2,000 for 10 Gb/s and 200 for 100 Gb/s. RSTP and MSTP (the rapid and multiple-tree versions described later in this article) use these "long" costs by default. Mixing the two scales in one network gives odd trees, so check what your switches use.
Building the tree
The protocol works in three steps, all decided by comparing BPDUs:
- Elect the root bridge. Every bridge starts by claiming to be root. When it hears a BPDU with a lower root ID than its own, it accepts that one as root and relays it. After a few exchanges, all bridges agree on the bridge with the lowest ID.
- Choose a root port on every other bridge. That is the port with the lowest total cost to the root. Ties are broken by the lowest sending bridge ID, then the lowest port ID (a 1-byte port priority, default 128 or
0x80, plus the port number). - Choose a designated port on every link. On each link, the bridge port offering the lowest cost to the root becomes the designated port and forwards frames for that link. The root's own ports are always designated.
Every other port is blocked. The lab's triangle used priorities 4096 for b1, 8192 for b2 and the default 32768 for b3. Linux prints a bridge ID as the priority in hex, a dot and the MAC address, so 1000.02000000b001 is priority 0x1000 = 4096 with MAC 02:00:00:00:b0:01. A root port of 0 means "none": b1 is the root itself:
b1: bridge id 1000.02000000b001 root id 1000.02000000b001 root port 0 root path cost 0
b2: bridge id 2000.02000000b002 root id 1000.02000000b001 root port 1 root path cost 2
b3: bridge id 8000.02000000b003 root id 1000.02000000b001 root port 1 root path cost 2
b1 to-b2 cost 2 forwarding
b1 to-b3 cost 2 forwarding
b2 to-b1 cost 2 forwarding
b2 to-b3 cost 2 forwarding
b2 to-h2 cost 2 forwarding
b3 to-b1 cost 2 forwarding
b3 to-b2 cost 2 blocking
b3 to-h3 cost 2 forwarding

b2 and b3 both reach the root at cost 2. On the link between them, both offer the same cost, so the tie goes to the lower bridge ID: b2's port is designated, and b3's port blocks. Linux gave each veth port a cost of 2 because the interface reports a 10 Gb/s speed. On real hardware the cost follows the negotiated speed, and you can set it per port.
What's in a BPDU
Here is a BPDU that b2 sent on its designated port toward b3:
Spanning Tree Protocol
Protocol Identifier: Spanning Tree Protocol (0x0000)
Protocol Version Identifier: Spanning Tree (0)
BPDU Type: Configuration (0x00)
BPDU flags: 0x01, Topology Change
0... .... = Topology Change Acknowledgment: No
.... ...1 = Topology Change: Yes
Root Identifier: 4096 / 0 / 02:00:00:00:b0:01
Root Bridge Priority: 4096
Root Bridge System ID Extension: 0
Root Bridge System ID: 02:00:00:00:b0:01 (02:00:00:00:b0:01)
Root Path Cost: 2
Bridge Identifier: 8192 / 0 / 02:00:00:00:b0:02
Bridge Priority: 8192
Bridge System ID Extension: 0
Bridge System ID: 02:00:00:00:b0:02 (02:00:00:00:b0:02)
Port identifier: 0x8002
Message Age: 0.00390625
Max Age: 6
Hello Time: 1
Forward Delay: 4
IEEE 802.3 Ethernet
Destination: Spanning-tree-(for-bridges)_00
Length: 38
Logical-Link Control
DSAP: Spanning Tree BPDU (0x42)
SSAP: Spanning Tree BPDU (0x42)
Control field: U, func=UI (0x03)
The fields:
- Protocol identifier 0, version 0 (STP), 2 (RSTP) or 3 (MSTP), and type:
0x00for a configuration BPDU,0x80for a topology change notification, and0x02for RSTP and MSTP BPDUs. - Flags: topology change (TC) and topology change acknowledgment (TCA). RSTP also uses the other six bits for the proposal and agreement handshake, the port's role and whether it is learning or forwarding.
- Root identifier and root path cost: who the sender thinks the root is, and its cost to reach it. Here the root is
b1, one link of cost 2 away fromb2. - Bridge identifier and port identifier: the sender (
b2) and the port it sent on (0x8002: priority 128, port 2). - Timers, carried in units of 1/256 second (that's why the message age above shows 0.0039, which is 1/256 s, the smallest nonzero value): message age, a rough hop count from the root that each bridge increases; max age, how long stored information is kept; hello time, how often the root sends BPDUs; and forward delay. The lab shortened the defaults of 20, 2 and 15 seconds to 6, 1 and 4 seconds.
A BPDU isn't carried with an EtherType. It uses the older 802.3 length field (here 38) followed by an LLC (Logical Link Control, article 14) header. Its destination and source service access points (DSAP and SSAP, which say which protocol the payload belongs to) are both 0x42, spanning tree, and control 0x03 means a plain unnumbered information frame. The topology change flag is set in this capture because the tree was still settling after start-up.
Port states and reconvergence
A classic STP port moves through these states:
| State | Forwards data | Learns addresses | Sends BPDUs |
|---|---|---|---|
| Disabled | No | No | No |
| Blocking | No | No | No (receives only) |
| Listening | No | No | Yes |
| Learning | No | Yes | Yes |
| Forwarding | Yes | Yes | Yes |
A port waits one forward delay in listening and another in learning before forwarding. The pause gives the new tree time to settle everywhere, so no temporary loop forms, and lets the bridge fill its table before it starts forwarding, which reduces flooding.
The price is slow recovery. The lab cut the link between b1 and b3 while h2 and h3 pinged each other, and watched b3's blocked port:
0.5 s after the failure: b3 port to-b2 is listening
4.1 s after the failure: b3 port to-b2 is learning
8.3 s after the failure: b3 port to-b2 is forwarding
61 of 140 pings answered

With a 4-second forward delay, the outage was about 8.5 seconds: two forward delays. With the default 15 seconds, it's about 30 seconds. If the failure is indirect, for example a broken link between two other switches, a bridge first has to notice that BPDUs have stopped arriving, which takes up to max age (20 seconds), for close to 50 seconds in total. In the early 2000s, that was acceptable for many LANs. Today it rarely is.
Topology changes
When the tree changes, the switches' MAC tables point at ports that no longer lead to the right place. A bridge that sees a port change state sends a topology change notification (TCN) BPDU toward the root on its root port. Each upstream bridge acknowledges it and passes it on. The root then sets the TC flag in its BPDUs for a while (max age plus forward delay), and while they see it, all bridges age out their tables after only one forward delay instead of 300 seconds. Stale entries disappear quickly, and frames are flooded until the addresses are learned again.
A port connected to a single computer that is switched on and off would trigger constant topology changes. That's why switches let you mark such edge ports (Cisco calls the feature PortFast): they go to forwarding at once and don't cause topology changes.
Rapid and multiple spanning trees
RSTP (802.1w, now in 802.1Q) keeps the same BPDU format and tree but recovers in under a second in most cases:
- States and roles are separate. There are only three states (discarding, learning, forwarding) and four roles: root, designated, alternate (a blocked port with another path to the root, like
b3's port in the lab) and backup (a blocked port on a link where the same bridge already has the designated port). - Precomputed backups. When a root port fails, the alternate port becomes the root port and forwards immediately, without waiting through listening and learning.
- Proposal and agreement. On point-to-point full-duplex links, a designated port asks its neighbor for permission to forward and gets an answer in a round trip, instead of waiting for timers.
- Faster failure detection. Every bridge sends BPDUs each hello time, and information is considered lost after three missed hellos (6 seconds with defaults).
- Edge ports are part of the standard.
RSTP is backward compatible: on a port where it hears old STP BPDUs, it falls back to the slow behavior.
MSTP (Multiple Spanning Tree Protocol, 802.1s, now in 802.1Q) runs several spanning tree instances and maps groups of VLANs to each. Different VLANs can then block different links, so redundant links carry traffic instead of sitting idle. Cisco's per-VLAN variants (PVST+ and Rapid PVST+, per-VLAN spanning tree) take the same idea further with one tree per VLAN, which doesn't scale as well.
On Linux, the kernel bridge implements only the classic protocol, as in the lab (ip link set br0 type bridge stp_state 1). RSTP and MSTP need a user-space daemon such as mstpd.
Registration protocols: MRP, MVRP and MMRP
Bridges also need to know which VLANs and which multicast groups are wanted on each port, so they don't send them where nobody is listening. The Multiple Registration Protocol (MRP, 802.1ak, in 802.1Q) is a generic way for stations and bridges to declare attributes and spread them through the tree. It replaced the older GARP (Generic Attribute Registration Protocol). Two applications use it:
- MVRP (the Multiple VLAN Registration Protocol, replacing GVRP) registers VLAN membership. When a trunk port learns that VLAN 20 is needed beyond it, VLAN 20 traffic is sent there. Otherwise it's pruned.
- MMRP (the Multiple MAC Registration Protocol, replacing GMRP) registers multicast MAC addresses. In practice it saw little use. IP multicast is mostly pruned by IGMP and MLD snooping instead: switches listen to the messages hosts send to join multicast groups (article 44).
Many operators configure trunk VLAN lists by hand or with automation instead of MVRP, but it's supported by many switches and by Linux (ip link add ... type vlan ... mvrp on).
Beyond the spanning tree
A spanning tree always leaves links unused and sends traffic along the tree even when a shorter path exists. Several approaches remove that limit:
- Link aggregation (article 15) makes parallel links one logical link, so STP doesn't block any of them. Multi-chassis versions (MLAG, or EVPN multihoming, where EVPN is Ethernet VPN) spread one bundle across two switches.
- Shortest Path Bridging (SPB, 802.1aq) and TRILL (Transparent Interconnection of Lots of Links, RFC 6325) use a link-state routing protocol (IS-IS, Intermediate System to Intermediate System) between bridges so that every link can carry traffic on shortest paths. Both had niche deployments and are now uncommon.
- Leaf-spine fabrics are the current data center design: every leaf switch connects to every spine switch, and the links between switches are routed (IP with the routing protocols BGP or OSPF, and equal-cost multipath, which spreads traffic over several equally good paths, article 27), so there are no Layer 2 loops between switches at all. Where Layer 2 is still needed across the fabric, it's carried in tunnels such as VXLAN (article 20) with BGP EVPN (RFC 7432, RFC 8365) to distribute MAC addresses instead of flooding and learning.
Spanning tree remains the default safety net in campus and office networks, often as RSTP or MSTP, on every edge switch, to stop the loop someone creates by plugging both ends of a cable into the same switch.
Common questions
Is a switch different from a bridge?
Not in principle. "Bridge" is the standards term and historically meant a device with a few ports and software forwarding. "Switch" is the marketing term for many-port bridges forwarding in hardware. A Linux bridge device behaves exactly like a small switch.
Why does my switch port take 30 seconds to come up when I plug in a laptop?
The port is running classic STP and goes through listening and learning (two forward delays of 15 seconds) before forwarding. Configure ports that connect to end devices as edge ports (PortFast), or use RSTP, where edge ports forward at once.
Which switch becomes root if I change nothing?
The one with the lowest bridge ID. With all priorities at the default 32768, that's the switch with the lowest MAC address, often the oldest switch in the network, which may be a poor choice. Set the priority on your core switches (for example 4096 on the primary, 8192 on the backup) so the root is where you want it.
Does STP stop all loops?
It stops loops between devices that run it. A loop through a device that doesn't forward BPDUs but does forward data, such as some cheap unmanaged switches or misconfigured virtual switches, can still cause a storm. Storm control and BPDU guard limit the damage.
Can a switch forward a frame before receiving all of it?
Yes. Cut-through switches start sending once they've read the destination address, which lowers latency. Store-and-forward switches receive the whole frame and check its CRC first, so they never pass on corrupted frames. Cut-through can't drop a bad frame it has already started forwarding, but it marks it so the next device drops it.
Do I need spanning tree if I use link aggregation?
Keep it running. Aggregation removes the loops of parallel links between two switches, but a wrong cable elsewhere can still create a loop, and STP is what catches it.
Summary
- A bridge (switch) learns source MAC addresses per port, forwards known unicast frames to one port, floods unknown, broadcast and multicast frames, and ages out entries after 300 seconds by default.
- Loops of bridges cause broadcast storms because frames have no TTL and flooding multiplies them. In the lab, one frame became hundreds of thousands of frames per second.
- STP (802.1D, now in 802.1Q) elects the root with the lowest bridge ID (priority plus MAC), chooses the least-cost root port on each bridge and a designated port on each link, and blocks the rest.
- BPDUs go to
01:80:c2:00:00:00in LLC frames (SAP0x42) and carry root ID, root path cost, bridge and port IDs, flags and timers in 1/256 s. - Classic STP recovers in two forward delays (30 s with defaults), up to about 50 s for indirect failures. Topology change notifications shorten table ageing during changes.
- RSTP recovers in under a second with alternate ports and the proposal and agreement handshake. MSTP maps VLAN groups to multiple trees. MRP with MVRP registers VLAN membership.
- Data centers avoid Layer 2 loops with routed leaf-spine fabrics, using VXLAN with EVPN where Layer 2 is still needed. Campus networks keep RSTP or MSTP as a safety net, with BPDU guard and root guard.
Practice questions
1. A bridge has learned that 02:00:00:00:00:05 is on port 3. A frame with that destination arrives on port 3. What does the bridge do, and why?
Answer
It drops (filters) the frame. The destination is on the same segment the frame came from, so the station has already received it, and forwarding it anywhere else would only waste bandwidth.
2. Four switches have priorities 32768, 32768, 4096 and 32768, and MAC addresses ending in :01, :02, :03 and :04. Which one becomes root?
Answer
The third one (priority 4096, MAC ending :03). The priority is compared first, and 4096 is lower than 32768. The MAC address matters only between switches with equal priority.
3. With classic STP and default timers, how long after a directly detected link failure does a blocked port start forwarding? How would RSTP compare?
Answer
Two forward delays, 2 × 15 s = 30 s (listening then learning). With RSTP, an alternate port that becomes the root port forwards immediately, and a new designated port uses the proposal and agreement handshake, so recovery usually takes well under a second.
4. Why does a bridge shorten its ageing time to the forward delay during a topology change?
Answer
After the tree changes, some learned entries point to ports that no longer lead to those stations. Frames sent there would be lost until the entries aged out after 300 seconds. Aging entries out in one forward delay makes the bridge flood briefly and relearn the correct ports quickly.
5. In a BPDU, the message age is 2 and max age is 20 (in seconds). How long will the receiving bridge keep this information if no newer BPDU arrives?
Answer
About max age minus message age, 20 − 2 = 18 seconds. Message age grows as the BPDU travels away from the root, so bridges far from the root time out information somewhat sooner.
6. Using the 802.1t long path costs, what is the cost of a path through one 1 Gb/s link and two 10 Gb/s links, and what would it be with the original short costs?
Answer
Long costs: 20,000 + 2,000 + 2,000 = 24,000. Short costs: 4 + 2 + 2 = 8. Mixing switches that use different scales would compare these numbers against each other, which is why one network should use one scale.
Previous: Duplex, Autonegotiation, Wake-on-LAN, Energy Saving and Link Flow Control
Next: Wi-Fi: 802.11 Frames, Media Access, Channels, Wi-Fi 6 and 7, and WPA3

More in TCP/IP Course
Quiz: TCP/IP Delicate Details
Twenty-five questions on the details that catch out experienced engineers: sequence numbers for SYN and FIN, TIME_WAIT versus tcp_fin_timeout, Karn's algorithm, socket buffer doubling, even and odd ephemeral ports, IPv6 checksums and fragmentation, negative DNS caching, and capture filter traps.
Quiz: TCP/IP Interview Questions
Thirty-three networking questions of the kind asked in developer, SRE and network engineering interviews: what happens when you load a URL, TCP versus UDP, handshakes, closing and TIME_WAIT, subnetting, NAT, DNS, MTU problems, congestion, TLS and QUIC, and debugging slow or failing connections.
Quiz: TCP/IP Course Final Exam (All Parts)
The final exam for the TCP/IP course: thirty questions across all fourteen parts, from packets and addressing through Ethernet, ARP, IP, DHCP, NAT, ICMP, UDP, DNS, TCP connections, reliability, congestion control, security, QUIC and troubleshooting.
Quiz: Modern Transports and Troubleshooting (TCP/IP Part 14)
Review modern transports and troubleshooting: QUIC packets, handshakes, 0-RTT, streams and connection migration, HTTP/3, Multipath TCP, SCTP multihoming, and a layered method for diagnosing real network faults.
Discussion
Sign in to ask a question or share what you learned.