Sub topic 12
VLANs and per-VLAN learning
This chapter assumes you know how a switch learns source addresses, floods unknown destinations, and ages entries. It adds one idea: the table is keyed by VLAN as well as by MAC address.
Why one table is really many
A VLAN (virtual LAN) is a separate broadcast domain inside one physical switch. Each access port belongs to one VLAN. A frame that enters a port in VLAN 10 can only be delivered to ports in VLAN 10.
The switch keeps this separation in its MAC table. An entry is not just "MAC to port". It is "VLAN and MAC to port". The same MAC address can appear in two VLANs with different ports, and the two entries do not affect each other.
| VLAN | MAC address | Port |
|---|---|---|
| 10 | 0011.2233.4455 | Gi0/1 |
| 10 | 0011.2233.4466 | Gi0/2 |
| 20 | 0011.2233.4455 | Gi0/7 |
What changes in each step
Every step you already know now happens inside the frame's VLAN.
- Learning. The switch records the source MAC against the ingress port and the VLAN of that port.
- Lookup. The switch searches for the destination MAC only among entries for the same VLAN.
- Flooding. An unknown unicast, broadcast, or multicast frame goes out only the other ports in that VLAN.
- Filtering. A frame is filtered if the destination is learned on the ingress port in that VLAN.
Modelling it in code
The learning switch from earlier needs one change. The table key becomes a pair, and flooding uses the ports of one VLAN.
class VlanSwitch:
def __init__(self, port_vlans):
self.port_vlans = port_vlans # port -> VLAN id
self.table = {} # (vlan, mac) -> port
def handle(self, in_port, src, dst):
vlan = self.port_vlans[in_port]
self.table[(vlan, src)] = in_port
out = self.table.get((vlan, dst))
if out is None:
return sorted(p for p, v in self.port_vlans.items()
if v == vlan and p != in_port)
return [] if out == in_port else [out]
sw = VlanSwitch({1: 10, 2: 10, 3: 20, 4: 20})
A = '00:00:00:00:00:0a'
B = '00:00:00:00:00:0b'
print(sw.handle(1, A, B)) # [2]: flooded only inside VLAN 10
print(sw.handle(3, A, B)) # [4]: same MACs, VLAN 20, separate entry
print(sw.handle(2, B, A)) # [1]: A was learned in VLAN 10This version leaves out aging so the VLAN logic stays visible. A real switch ages each entry as before.
Reading it on real devices
On a Cisco IOS-style switch, the Vlan column is part of every entry. You can filter by VLAN:
Switch# show mac address-table vlan 10On a Linux bridge, VLAN filtering must be enabled. The VLAN then appears in the forwarding database:
bridge fdb show br br0
bridge vlan showCommon errors
- Expecting a MAC to be known everywhere. A host learned in VLAN 10 is unknown in VLAN 20. A frame in VLAN 20 for that MAC floods.
- Mismatched port VLANs. If two hosts sit in different VLANs by mistake, they never learn each other, and a switch cannot forward between them. Only a router can.
- Counting entries by MAC. The table's capacity is used per VLAN and MAC pair, so one device present in many VLANs uses several entries.
Exercise
Host X (MAC aa) is learned on port 1 in VLAN 10. A frame with destination aa arrives on port 5, which is in VLAN 20. What does the switch do, and why?
Show answer
It floods the frame to the other ports in VLAN 20. The lookup uses the key (VLAN 20, aa), and the only entry for aa is in VLAN 10, so the destination counts as unknown in VLAN 20.