Sub topic 10
When a Host Moves: Updates and MAC Flapping
This chapter assumes you know how a switch learns source addresses, looks up destinations, and ages entries. Here we look at what happens when a MAC address that is already in the table shows up on a different port.
Moves are just learning again
A switch has no separate "move" operation. Step 1 of the forwarding logic is: record the source MAC address against the ingress port. If the address is already in the table, the switch overwrites the old port and resets the timer.
The handle method from the earlier chapters already does this, because self.table[src] = (in_port, now) replaces any existing entry. Consider a laptop that is unplugged from port 1 and plugged into port 3:
import time
AGING_SECONDS = 300
def is_group(mac):
return int(mac.split(':')[0], 16) & 1 == 1
class LearningSwitch:
def __init__(self, ports, aging=AGING_SECONDS):
self.ports = set(ports)
self.aging = aging
self.table = {}
self.moves = 0
def handle(self, in_port, src, dst, now=None):
now = time.monotonic() if now is None else now
if not is_group(src):
old = self.table.get(src)
if old is not None and old[0] != in_port:
self.moves += 1
self.table[src] = (in_port, now)
entry = None if is_group(dst) else self.table.get(dst)
if entry is not None and now - entry[1] > self.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]]
sw = LearningSwitch(ports=[1, 2, 3])
laptop = '00:00:00:00:00:0a'
server = '00:00:00:00:00:0b'
sw.handle(1, laptop, server, now=0)
print(sw.handle(2, server, laptop, now=1)) # [1]
sw.handle(3, laptop, server, now=2) # laptop moved to port 3
print(sw.handle(2, server, laptop, now=3)) # [3]
print(sw.moves) # 1The moves counter is an addition for this chapter. A real switch does not need one to move a host, but it is how you would detect the problem described below.
Why a move works without aging
The old entry is not waiting for the 300-second timer. The first frame the laptop sends from the new port replaces it. Until then, frames for the laptop still go to port 1, where nobody is listening. The host is unreachable until it transmits, or until the old entry ages out and traffic is flooded. Many operating systems send a frame when a link comes up, which shortens this gap.
MAC flapping
A MAC address that keeps alternating between two or more ports is called flapping. The switch is doing what it was designed to do, so flapping is a symptom, not a switch fault. Frames with the same source MAC are really arriving on more than one port.
Common causes:
- A Layer 2 loop, where the same broadcast frame returns through a second path. Spanning tree failures are the usual source.
- A host with two NICs that share one MAC address, for example in some bonding or load-balancing modes.
- A virtual machine migrating between hosts, or a virtual switch that reflects frames.
- Two devices configured with the same MAC address.
What flapping looks like
Cisco IOS-style switches log a message such as %SW_MATM-4-MACFLAP_NOTIF naming the MAC address, the VLAN, and the two ports. Linux bridges have no equivalent log by default. On Linux you can watch bridge monitor fdb for repeated updates.
Flapping costs more than log noise. Traffic for the flapping address goes to whichever port wrote last, so frames are lost or delayed. A loop also fills links with duplicated frames.
Troubleshooting steps
- Read the log message to get the MAC address and the two ports.
- Trace each port to the device behind it.
- Check whether both paths lead to the same host or to a loop.
- Fix the cause: disable the extra link, correct bonding mode, or change the duplicate address.
Exercise
A switch logs that MAC 0a is flapping between ports 1 and 3 several times a second. Is a host that was moved once the likely cause? Explain.
Show answer
No. A single move changes the entry once and then it is stable. Repeated alternation means frames with source 0a keep arriving on both ports, which points to a loop, a duplicate MAC address, or a bonding or virtualization setup that sends from two ports.