Sub topic 16
Topology changes and flushing the table
A switch learns that a MAC address is reachable through a port, and it keeps believing that until the entry ages out. That works while the network is stable. When a link fails or a spanning tree recalculates, the port that used to lead to a host may no longer be the right one. This chapter covers how switches avoid sending traffic into a dead path, and the commands that clear the table.
This chapter assumes you know source learning, aging, flooding, and the basics of trunks and uplinks between switches. It also assumes you know what Spanning Tree Protocol (STP) is for: it blocks redundant links so that a loop-free tree remains.
Why stale entries become a problem
Aging works well when a host goes quiet. A topology change is a different case. The host is still there, but the path to it has changed.
Suppose switch S1 connects to S2 by two links, and STP blocks one of them. S1 learned host B on port 2, which faces the active link. That link fails, and STP unblocks the backup link on port 3. B is now reachable through port 3, but S1's table still says port 2.
S1 has to relearn B from a frame that B sends. If B is mostly receiving, for example a server answering rarely or a host being sent a stream, no such frame arrives. Frames for B go to the dead port until the entry ages out. With the default aging time of 300 seconds, that is up to five minutes of lost traffic after STP has already reconverged.
How STP signals a topology change
The fix is to tell every switch in the network that its table may be wrong. Spanning tree carries that signal in its Bridge Protocol Data Units (BPDUs). The mechanism differs between the original protocol and the rapid version.
Classic STP (802.1D)
A switch that detects a topology change does the following:
- It sends a Topology Change Notification (TCN) BPDU out its root port, toward the root bridge.
- Each upstream switch acknowledges the TCN and forwards it toward the root.
- The root bridge sets the Topology Change (TC) flag in the configuration BPDUs it sends. By default it keeps the flag set for max age plus forward delay, which is 20 + 15 = 35 seconds.
- Every switch that receives a BPDU with the TC flag set shortens its aging time to the forward delay, 15 seconds by default, for as long as the flag is set.
So classic STP does not delete the whole table at once. It fast-ages it. Entries for hosts that keep sending are refreshed and survive. Entries for silent hosts expire within 15 seconds and are relearned through flooding.
Rapid STP (802.1w)
Rapid STP (RSTP) handles this more directly:
- A topology change is detected only when a non-edge port moves to the forwarding state. A port going down does not count. Edge ports, which Cisco calls PortFast ports and which face end hosts, never generate topology changes. Without this rule, every laptop that was switched on would flush the network.
- The detecting switch immediately flushes the MAC addresses learned on all its non-edge ports, except the port where the change was received.
- It sends BPDUs with the TC flag out its root port and designated ports for twice the hello time (4 seconds by default). Each switch that receives one repeats the flush and flooding of the flag.
The flush is immediate, and the effect is local to the port set involved. Convergence after a failure takes seconds instead of the minutes aging would need.
| Property | 802.1D STP | 802.1w RSTP |
|---|---|---|
| What triggers it | A port moves to forwarding, or from forwarding to blocking | A non-edge port moves to forwarding |
| Signal | TCN BPDU up to the root, then the TC flag from the root | TC flag set on BPDUs and propagated by every switch |
| Effect on the table | Aging time drops to the forward delay (15 s) | Entries on the affected ports are flushed immediately |
| Duration of the signal | Max age + forward delay (35 s) | Twice the hello time (4 s) |
Modeling it in code
The following example is self-contained. It models both behaviors on top of the learn, lookup, flood and filter logic from earlier chapters.
def is_group(mac):
return int(mac.split(':')[0], 16) & 1 == 1
class FlushingSwitch:
def __init__(self, ports, aging=300, tc_aging=15):
self.ports = set(ports)
self.aging = aging
self.tc_aging = tc_aging
self.tc_until = 0
self.table = {} # mac -> (port, last_seen)
def topology_change(self, now, mode, from_port=None, duration=35):
if mode == "flush":
# RSTP style: drop every entry except those on the receiving port.
self.table = {m: (p, t) for m, (p, t) in self.table.items()
if p == from_port}
elif mode == "fast_age":
# 802.1D style: shorten the aging time while the TC flag is set.
self.tc_until = now + duration
def handle(self, in_port, src, dst, now):
if not is_group(src):
self.table[src] = (in_port, now)
aging = self.tc_aging if now < self.tc_until else self.aging
entry = None if is_group(dst) else self.table.get(dst)
if entry is not None and now - entry[1] > aging:
del self.table[dst]
entry = None
if entry is None:
return sorted(self.ports - {in_port})
return [] if entry[0] == in_port else [entry[0]]
A = '00:00:00:00:00:0a'
B = '00:00:00:00:00:0b'
def build():
sw = FlushingSwitch([1, 2, 3])
sw.handle(1, A, B, now=0) # flood, A learned on port 1
sw.handle(2, B, A, now=1) # B learned on port 2
return sw
sw = build()
print(sw.handle(1, A, B, now=10)) # [2]: stale, B has moved behind port 3
sw.topology_change(now=10, mode="flush")
print(sw.handle(1, A, B, now=11)) # [2, 3]: flooded, B can answer
sw = build()
sw.topology_change(now=10, mode="fast_age")
print(sw.handle(1, A, B, now=12)) # [2]: 12 - 1 = 11 s, under 15 s
print(sw.handle(1, A, B, now=30)) # [2, 3]: 29 s > 15 s, entry expiredThe first frame at now=10 still goes to port 2, which is the stale-entry problem. After the flush, the frame floods out ports 2 and 3, reaches B on its new path, and B's reply teaches the switch the right port. In the fast-aging case, a recently refreshed entry survives and an old one does not.
Controlling and observing the behavior
You can see whether topology changes are happening, and you can flush the table by hand.
On a Cisco IOS-style switch, the spanning tree detail output shows the count and the origin of the last change:
Switch# show spanning-tree detail
VLAN0001 is executing the rstp compatible Spanning Tree protocol
Number of topology changes 3 last change occurred 00:02:11 ago
from GigabitEthernet0/2A steadily increasing counter with a recent timestamp means something keeps triggering flushes. The usual cause is a flapping link or an end-host port without PortFast. The "from" interface tells you where to look.
To clear learned entries manually:
Switch# clear mac address-table dynamic
Switch# clear mac address-table dynamic interface GigabitEthernet0/2Older releases spell it clear mac-address-table dynamic. Only dynamic entries are removed. Static entries and port security sticky entries stay.
On a Linux bridge, STP is off by default. When it is on, the kernel applies the same fast-aging rule. The ageing time is in centiseconds and the default is 30000, which is 300 seconds.
ip link set br0 type bridge stp_state 1 # enable STP
ip -d link show br0 # look for ageing_time and stp_state
bridge fdb show br br0 # the learned entries
bridge fdb flush dev eth1 # drop entries learned on one portThe last command needs a recent iproute2. A static (permanent) entry is not flushed, as on Cisco.
Side effects of flushing
A flush is a controlled loss of knowledge, and it has costs.
- Flooding bursts. Right after a flush, most unicast traffic is flooded until hosts transmit and the table refills. On a busy network with a big table, this can briefly saturate links and slow down hosts that receive frames not meant for them.
- Repeated flushes. A flapping uplink or a misconfigured access port generates a topology change each time. The table never stabilizes and the network stays in a permanent flooding state. This is a common cause of "the network is slow but nothing is down."
- Security. A device that can send BPDUs can trigger flushes on purpose. That is why access ports should have BPDU guard, which shuts down a port that receives BPDUs, and why PortFast should be set only on ports that face end hosts.
- Per-VLAN scope. With per-VLAN spanning tree, a topology change in one VLAN flushes only that VLAN's entries, which follows from the per-VLAN tables covered earlier.
Exercise
A switch running classic 802.1D STP learns host B on port 2. The link behind port 2 fails and STP opens a backup path through port 3. Host B is silent and only receives traffic. With default timers, roughly how long can frames for B keep going to port 2 if the switch gets no topology change signal, and how does the signal change this? Then explain why an RSTP edge port that comes up does not cause a flush.
Show answer
Without a signal, entries age out only after the default 300 seconds, so frames for B can be lost for up to about five minutes. With the TC flag set (for 35 seconds by default), switches shorten the aging time to the 15-second forward delay, so B's stale entry expires within about 15 seconds. The switch then floods frames for B, and B's reply teaches the correct port. In RSTP, only non-edge ports that move to forwarding count as a topology change. An edge port faces an end host, which cannot create a loop or change the path to other hosts, so no flush is needed. Without this rule, every host that was switched on would flush the network.