Sub topic 5
Learning: Recording the Source Address on the Ingress Port
This chapter assumes you know the Ethernet frame fields (destination MAC, source MAC, EtherType) and that a switch keeps a MAC address table. Here you will see how the entries get into that table.
The rule
A frame enters a switch through its ingress port, the port it arrived on. The switch reads the frame's source MAC address and records that the address is reachable through that port. This happens for every frame, before the destination lookup, and it needs no configuration.
The source address is the one that tells the switch where a host is. A frame from A that arrives on port 1 shows that A is reachable through port 1. The destination address says nothing about where that host is.
What the switch does with each frame
- Read the source MAC and the VLAN of the frame.
- Skip learning if the source is a group address. A group address has the least significant bit of its first octet set, and it is never a valid source.
- Create an entry if none exists.
- Refresh the timestamp if the entry already exists on the same port.
- Overwrite the port if the entry exists on a different port. The switch treats this as a host move.
A runnable version
import time
def is_group(mac):
return int(mac.split(':')[0], 16) & 1 == 1
def learn(table, in_port, src, now=None):
now = time.monotonic() if now is None else now
if is_group(src):
return "ignored"
old = table.get(src)
table[src] = (in_port, now)
if old is None:
return "new"
return "refreshed" if old[0] == in_port else "moved"
table = {}
A = '00:00:00:00:00:0a'
print(learn(table, 1, A, now=0)) # new
print(learn(table, 1, A, now=5)) # refreshed
print(learn(table, 3, A, now=9)) # moved
print(learn(table, 2, 'ff:ff:ff:ff:ff:ff', now=10)) # ignored
print(table) # {'00:00:00:00:00:0a': (3, 9)}The table stores the port and a last-seen time for each address. The time is what aging uses later.
Common mistakes
- Learning from the destination. The switch never does this. A host that never sends a frame is never learned, so frames addressed to it are flooded.
- Expecting entries to appear before traffic. The table starts empty after a boot or a flush. It fills only as frames arrive.
- Forgetting the port state. A port that spanning tree holds in a blocking state does not learn addresses.
- Assuming one table per switch. Entries are keyed by VLAN and MAC together, so the same MAC can appear in two VLANs. A later chapter covers this.
Exercise
A switch has an empty table. Host A (port 1) sends a frame to host B (port 2). B then replies to A. Which addresses has the switch learned after the reply, and on which ports?
Show answer
It learned A on port 1 from the first frame and B on port 2 from the reply. Learning uses only the source address of each frame. Before the reply, B was unknown, so the first frame was flooded.