Sub topic 12
VLANs and per-VLAN learning
This chapter assumes you know the earlier material: how a switch learns source addresses, forwards known unicast frames, floods unknown ones, and ages entries out. It also assumes you know the 802.1Q tag at a basic level, which is a 4-byte field that carries a VLAN ID. Here you will see how VLANs change the MAC address table and the flooding rules. Linking several switches comes in the next chapter.
What a VLAN changes
A VLAN (virtual LAN) splits one physical switch into several logical Layer 2 networks. Each VLAN is its own broadcast domain. A broadcast sent in VLAN 10 must never reach a host in VLAN 20, even if both hosts plug into the same switch.
For a learning switch, this adds one question to every frame: which VLAN does this frame belong to? The answer changes how the switch learns, looks up, and floods.
Deciding which VLAN a frame belongs to
The switch assigns a VLAN to each frame when it arrives, based on the ingress port.
- Access port. The port belongs to exactly one VLAN. Frames arrive untagged, and the switch assigns them the port's VLAN. Hosts, printers and phones usually connect this way.
- Trunk port. The port carries several VLANs. Frames carry an 802.1Q tag with a 12-bit VLAN ID, so the switch reads the VLAN from the tag. A frame that arrives untagged is placed in the port's native VLAN (the PVID on a Linux bridge). The switch also drops tagged frames whose VLAN is not allowed on the trunk.
The VLAN assigned at ingress is used for everything that follows. The same MAC address can appear in many VLANs, so a MAC address alone no longer identifies an entry.
The table key becomes (VLAN, MAC)
Without VLANs, the table maps a MAC address to a port. With VLANs, the lookup key is the pair of VLAN ID and MAC address.
| VLAN | MAC address | Port | Age |
|---|---|---|---|
| 10 | 00:00:00:00:00:0a | 1 | 12 s |
| 10 | 00:00:00:00:00:0b | 2 | 40 s |
| 20 | 00:00:00:00:00:0a | 3 | 5 s |
The same MAC address appears twice here, on different ports, in different VLANs. This is valid. A router with sub-interfaces, or a server that sits in two VLANs through one NIC, behaves this way. Learning in each VLAN is independent, which standards call independent VLAN learning (IVL).
Each lookup is also confined to one VLAN. A frame in VLAN 10 for ...0a is forwarded to port 1. A frame in VLAN 20 for the same address goes to port 3. The two entries never interfere, and a move in one VLAN does not cause MAC flapping in the other.
Flooding stays inside the VLAN
Flooding keeps its meaning but covers fewer ports. When the destination is unknown, broadcast or multicast, the switch sends the frame out of every port that is a member of the frame's VLAN, except the ingress port. Member ports are:
- access ports assigned to that VLAN, and
- trunk ports that allow that VLAN.
Filtering works the same way. If the destination entry in that VLAN points at the ingress port, the frame is dropped.
A VLAN-aware switch in Python
This extends the learning switch from earlier in the tutorial. Access ports map to one VLAN. Trunk ports carry a set of allowed VLANs. The table key is (vlan, mac).
import time
AGING_SECONDS = 300
def is_group(mac):
return int(mac.split(':')[0], 16) & 1 == 1
class VlanSwitch:
def __init__(self, access, trunks, native=1, aging=AGING_SECONDS):
self.access = dict(access) # port -> VLAN ID
self.trunks = {p: set(v) for p, v in trunks.items()} # port -> allowed VLANs
self.native = native
self.aging = aging
self.table = {} # (vlan, mac) -> (port, last_seen)
def classify(self, in_port, tag):
"""Return the VLAN of the frame, or None if it must be dropped."""
if in_port in self.access:
# Access ports accept untagged frames only.
return None if tag is not None else self.access[in_port]
vlan = self.native if tag is None else tag
return vlan if vlan in self.trunks[in_port] else None
def members(self, vlan):
ports = {p for p, v in self.access.items() if v == vlan}
ports |= {p for p, allowed in self.trunks.items() if vlan in allowed}
return ports
def handle(self, in_port, src, dst, tag=None, now=None):
"""Return the list of ports the frame is sent out of."""
now = time.monotonic() if now is None else now
vlan = self.classify(in_port, tag)
if vlan is None:
return []
if not is_group(src):
self.table[(vlan, src)] = (in_port, now)
entry = None if is_group(dst) else self.table.get((vlan, dst))
if entry is not None and now - entry[1] > self.aging:
del self.table[(vlan, dst)]
entry = None
if entry is None:
return sorted(self.members(vlan) - {in_port})
out_port = entry[0]
return [] if out_port == in_port else [out_port]Now run some frames. Ports 1 and 2 are in VLAN 10, ports 3 and 4 are in VLAN 20, and port 5 is a trunk for both VLANs.
sw = VlanSwitch(access={1: 10, 2: 10, 3: 20, 4: 20},
trunks={5: {10, 20}}, native=10)
A = '00:00:00:00:00:0a'
B = '00:00:00:00:00:0b'
print(sw.handle(1, A, B, now=0)) # VLAN 10, B unknown -> [2, 5]
print(sw.handle(3, A, B, now=1)) # VLAN 20, same MACs -> [4, 5]
print(sw.handle(2, B, A, now=2)) # A known in VLAN 10 -> [1]
print(sw.handle(1, A, B, now=3)) # B known in VLAN 10 -> [2]
print(sw.table[(10, A)][0], sw.table[(20, A)][0]) # 1 3
print(sw.handle(5, B, A, tag=20, now=4)) # tagged frame, VLAN 20 -> [3]
print(sw.handle(1, A, B, tag=10, now=5)) # tagged frame on access port -> []The first two frames carry the same MAC pair but flood to different port sets. Address A ends up with two entries, port 1 in VLAN 10 and port 3 in VLAN 20. The frame on the trunk is learned under VLAN 20 because of its tag.
Reading per-VLAN tables on real devices
Cisco IOS-style switches show the VLAN as the first column and can filter by it.
Switch# show mac address-table vlan 10
Mac Address Table
-------------------------------------------
Vlan Mac Address Type Ports
---- ----------- -------- -----
10 0011.2233.4455 DYNAMIC Gi0/1
10 0011.2233.4466 DYNAMIC Gi0/2
Total Mac Addresses for this criterion: 2
Switch# show mac address-table address 0011.2233.4455The second command lists every VLAN in which that address is known. This is useful when you suspect a device is visible in a VLAN where it should not be.
On a Linux bridge, you must turn on VLAN filtering. Otherwise the bridge treats all ports as one VLAN-unaware domain.
ip link add br0 type bridge vlan_filtering 1
ip link set eth1 master br0
bridge vlan add dev eth1 vid 10 pvid untagged
bridge vlan show
bridge fdb show br br0With VLAN filtering on, bridge fdb show adds a vlan field to each line, for example 00:11:22:33:44:55 dev eth1 vlan 10 master br0. The bridge vlan show output lists which VLANs each port carries.
Gotchas and common errors
- Wrong VLAN on a port. A host moved to a port in another VLAN is learned in that VLAN, so its old VLAN entry simply ages out. The symptom is a host that cannot reach its usual neighbours. Check
show mac address-tablefor the host's MAC and read the VLAN column. - Native VLAN mismatch. If two ends of a trunk disagree about which VLAN is untagged, frames land in a different VLAN on each side. Learning then happens in the wrong VLAN, and the same MAC may appear to flap between VLANs.
- Disallowed VLAN on a trunk. A frame for a VLAN missing from the allowed list is dropped at ingress. The address is never learned, and it never appears in the table.
- Shared capacity. The table has one size limit for all VLANs. A device present in 50 VLANs uses 50 entries. Many VLANs with many hosts can fill the table faster than you expect.
- Aging is per entry. An entry in VLAN 10 is refreshed only by frames sent from that MAC in VLAN 10. Traffic in VLAN 20 does not keep it alive.
Exercise
A switch has access ports 1 and 2 in VLAN 10 and port 3 in VLAN 20. Host H (MAC ending 0a) sends a frame from port 1 in VLAN 10 and is learned. Later H sends a frame for an unknown destination from port 3 in VLAN 20. What does the MAC table hold for H, and where is the second frame flooded?
Show answer
The table holds two entries for H: (VLAN 10, 0a) on port 1 and (VLAN 20, 0a) on port 3. Neither entry replaces the other, because the key includes the VLAN. The second frame is flooded only to the other member ports of VLAN 20. Here there are none other than the ingress port, unless a VLAN 20 trunk or other access port exists, and ports 1 and 2 receive nothing.
Exercise
A trunk between two switches has a native VLAN of 1 on one side and 99 on the other. An untagged frame leaves the first switch. Which VLAN does the second switch learn its source MAC in, and why is that a problem?
Show answer
The second switch treats untagged frames as native VLAN 99, so it learns the source MAC under VLAN 99 even though the sender was in VLAN 1. Its table and flooding then follow the wrong VLAN, which can leak traffic between VLANs and make the same MAC appear in two VLANs or flap.