Sub topic 5
Learning: Recording the Source Address on the Ingress Port
This chapter assumes you know what a MAC address is and how it differs from an IP address. It also assumes you know the Ethernet frame fields (destination MAC, source MAC, EtherType) and the difference between a hub and a switch. Earlier chapters described what the MAC address table stores. This chapter covers how entries get into it.
The core rule
A switch is not configured with a list of which host sits on which port. It builds that list itself, using one rule:
When a frame arrives on a port, the switch records that the frame's source MAC address is reachable through that ingress port (the port the frame came in on).
The rule works because of how Ethernet forwarding behaves. A frame can only arrive on a port if the sender is reachable in that direction. If a frame with source 00:00:00:00:00:0a arrives on port 1, then sending a frame back out of port 1 will reach that host, or a device on the way to it.
The switch never learns from the destination address. The destination only says where the sender wants the frame to go. It says nothing about where that device is attached.
What learning does to a table entry
For each incoming frame, the switch looks up the source address in its table. Three outcomes are possible:
| Situation | What the switch does | Name |
|---|---|---|
| Source not in the table | Adds an entry: source, ingress port, current time | New entry |
| Source in the table, same port | Resets the entry's age timer | Refresh |
| Source in the table, different port | Overwrites the port and resets the timer | Move |
The refresh case matters for aging. An entry is only removed after it has gone unrefreshed for the aging time, 300 seconds by default on many switches. A host that sends traffic regularly stays in the table. A host that goes quiet is eventually forgotten. The next chapter covers aging in detail.
The move case lets the table follow a laptop that is unplugged from one port and plugged into another. The switch does not wait for the old entry to age out. The first frame from the new port corrects it.
Where learning sits in the pipeline
Learning happens before the forwarding decision, and it does not depend on the decision's outcome.
This ordering has two consequences:
- A frame that is flooded still teaches the switch about its sender. The very first frame in a conversation, which the switch must flood because the destination is unknown, already records the sender's location.
- A frame that is filtered (dropped because the destination is on the same port it arrived on) also teaches the switch. Learning is independent of what happens to the frame afterward.
Which source addresses are not learned
A source address should always be a unicast address. The least significant bit of the first octet is the individual/group bit. If it is 1, the address is a group address (broadcast or multicast). A valid frame should never carry one as its source, and a switch does not learn it. The only reason to learn is to forward later frames to one port, and group addresses are never forwarded that way.
For example, ff:ff:ff:ff:ff:ff has a first octet of 0xff. The lowest bit is 1, so it is a group address. 00:00:00:00:00:0a starts with 0x00, so it is unicast.
Other frames are also not used for learning:
- Frames with errors. A frame that fails the frame check sequence (FCS) is discarded, so its source field cannot be trusted.
- Frames on ports that are not in a learning state. Under spanning tree, a port in the blocking or listening state does not learn. A port in the learning state records sources but does not yet forward. Only a forwarding port both learns and forwards.
- Frames that a security feature rejects. Port security, covered in a later chapter, can drop a frame from an unauthorized source before the address is added.
A small implementation
The learning switch in this tutorial's earlier examples handles learning in a single step. Here is the learning step on its own, returning which of the three outcomes occurred so you can watch the table change. It keys the table on a VLAN and a MAC address, which previews a later chapter. Switches keep a separate address space per VLAN.
def is_group(mac):
# The least significant bit of the first octet marks multicast/broadcast.
return int(mac.split(':')[0], 16) & 1 == 1
def learn(table, src, in_port, now, vlan=1):
"""Record src on in_port. Return 'ignored', 'new', 'refresh' or 'move'."""
if is_group(src):
return 'ignored'
key = (vlan, src)
old = table.get(key)
table[key] = (in_port, now) # always store the latest port and time
if old is None:
return 'new'
return 'refresh' if old[0] == in_port else 'move'
table = {}
A = '00:00:00:00:00:0a'
print(learn(table, A, 1, now=0)) # new
print(learn(table, A, 1, now=10)) # refresh
print(learn(table, A, 3, now=20)) # move
print(learn(table, 'ff:ff:ff:ff:ff:ff', 2, now=30)) # ignored
print(table) # {(1, '00:00:00:00:00:0a'): (3, 20)}The table[key] = (in_port, now) line is the whole mechanism. New entries, refreshes and moves are the same write. They differ only in whether a previous value existed and whether its port matched.
Real switches do this in hardware. The lookup and the write go to a content-addressable memory, so learning does not slow forwarding. Some low-end devices limit how fast they can add new entries, which matters in the capacity chapter.
Seeing learning happen
Consider two hosts, A on port 1 and B on port 2, with an empty table:
- A sends a frame to B. The switch learns "A is on port 1". B is unknown, so the frame is flooded to every other port.
- B replies to A. The switch learns "B is on port 2". A is known, so the reply goes out port 1 only.
- From here on, frames between A and B each use a single port, and every frame refreshes the sender's age timer.
After one round trip, both addresses are learned. This is why the first exchange between two hosts reaches every port, and later ones do not.
You can check this on real equipment. On a Cisco IOS-style switch, show mac address-table lists entries with type DYNAMIC for learned addresses. On a Linux bridge, bridge fdb show br br0 lists them, and learned entries have no permanent flag. Compare the table before and after you send a ping to see new entries appear.
Common errors and misconceptions
- "The switch learns the destination." It does not. If a host never sends a frame, the switch cannot learn it. A silent host that only receives (a printer that never initiates, for example) stays unknown and gets flooded to until it transmits something.
- "Learning needs a reply." It needs any frame from the host. An ARP request, a DHCP discover or a gratuitous ARP all teach the switch the sender's port.
- "A learned address is always one device." On an uplink to another switch, many source addresses are learned on the same port. A later chapter covers this.
- "A move means something is wrong." One move is normal when a host is re-cabled or a virtual machine migrates. Repeated moves in quick succession between two ports are a problem. That pattern is MAC flapping, and it usually points to a loop or a duplicated address.
- Forged sources. The switch trusts the source field. Any host can send a frame with someone else's source address, and the switch will move that address to the attacker's port. Port security and static entries are the defenses.
Exercise
Host C is on port 3 and has never sent a frame. Host A (port 1) sends a frame to C, and the switch has no entry for C. List the table entries after this frame, which ports the frame goes out of, and when the switch will first learn C.
Show answer
The table gains one entry: A on port 1. The frame is flooded out all ports except port 1, so it leaves ports 2, 3 and so on. C receives it, but the switch learns nothing about C from a frame it receives. The switch learns C only when C sends a frame of its own, such as a reply, and that frame arrives on port 3.
Exercise
Using the learn function above with an empty table, what does each call return, and what does the table contain at the end?
learn(table, '00:00:00:00:00:0b', 2, now=0)
learn(table, '01:00:5e:00:00:05', 2, now=5)
learn(table, '00:00:00:00:00:0b', 4, now=9)
learn(table, '00:00:00:00:00:0b', 4, now=12)
Show answer
The calls return 'new', 'ignored', 'move' and 'refresh'. The second address has its group bit set (first octet 0x01), so it is ignored. The final table is {(1, '00:00:00:00:00:0b'): (4, 12)}.