Sub topic 13
Several switches: many MAC addresses behind one port
This chapter assumes you already know how one switch learns source addresses, looks up destinations, and floods unknown unicast frames. It also assumes you know what a VLAN is. The question here is what changes when switches connect to each other.
One port, many addresses
On a port connected to a single host, the MAC table has one address for that port. A port connected to another switch is different. Every host behind that switch is reachable through the port, so the table holds many addresses for it.
The switch does not know it is connected to another switch. It uses the same two rules as before:
- Learning: a frame arrives on a port, so its source MAC address is recorded against that port.
- Forwarding: a frame's destination MAC address is looked up, and the frame goes out of the port recorded for it.
The uplink is just a port where many different source addresses arrive. Nothing needs configuring for this. The table maps each MAC address to the direction in which it is reachable, not to the physical host.
After the hosts have all sent traffic, the tables look like this:
| Switch | MAC address | Learned on port | Reached through |
|---|---|---|---|
| SW1 | A | 1 | Direct link |
| SW1 | B | 2 | Direct link |
| SW1 | C | 3 | Uplink to SW2 |
| SW1 | D | 3 | Uplink to SW2 |
| SW2 | C | 1 | Direct link |
| SW2 | D | 2 | Direct link |
| SW2 | A | 3 | Uplink to SW1 |
| SW2 | B | 3 | Uplink to SW1 |
Each switch has only a partial view. SW1 knows nothing about the ports of SW2. It knows only that C and D are somewhere out of port 3.
Simulating two switches
This script wires two switches together and sends frames between hosts. It reports which hosts receive each frame, so you can see flooding cross the uplink. Aging is left out to keep it short.
from collections import deque
def is_group(mac):
# The least significant bit of the first octet marks multicast/broadcast.
return int(mac.split(':')[0], 16) & 1 == 1
class Switch:
def __init__(self, name, ports):
self.name = name
self.ports = set(ports)
self.table = {} # mac -> port
def handle(self, in_port, src, dst):
if not is_group(src):
self.table[src] = in_port
out = None if is_group(dst) else self.table.get(dst)
if out is None:
return sorted(self.ports - {in_port}) # flood
return [] if out == in_port else [out] # filter or forward
MAC = {h: f'00:00:00:00:00:0{h.lower()}' for h in 'ABCD'}
NAME = {m: h for h, m in MAC.items()}
switches = {'SW1': Switch('SW1', [1, 2, 3]), 'SW2': Switch('SW2', [1, 2, 3])}
# (switch, port) -> host name, or the (switch, port) at the other end of a cable
links = {
('SW1', 1): 'A', ('SW1', 2): 'B', ('SW1', 3): ('SW2', 3),
('SW2', 1): 'C', ('SW2', 2): 'D', ('SW2', 3): ('SW1', 3),
}
def send(src_host, dst_host):
"""Return the hosts that receive the frame."""
start = next(k for k, v in links.items() if v == src_host)
queue = deque([(start, MAC[src_host], MAC[dst_host])])
received = []
while queue:
(name, port), src, dst = queue.popleft()
for out in switches[name].handle(port, src, dst):
peer = links[(name, out)]
if isinstance(peer, tuple):
queue.append((peer, src, dst)) # frame enters the next switch
else:
received.append(peer)
return received
print(send('A', 'C')) # ['B', 'C', 'D'] C unknown everywhere: flood
print(send('C', 'A')) # ['A'] A was learned on SW2 port 3
print(send('A', 'C')) # ['C'] C is now known on both switches
print(send('B', 'D')) # ['A', 'C', 'D'] D has never sent: flood again
for sw in switches.values():
print(sw.name, {NAME[m]: p for m, p in sorted(sw.table.items())})
# SW1 {'A': 1, 'B': 2, 'C': 3}
# SW2 {'A': 3, 'B': 3, 'C': 1}The first frame floods through both switches, so host B and host D receive a frame that was not meant for them. The second frame goes straight back, because SW2 learned A on its uplink from the first frame. By the third frame, both switches forward without flooding.
The last frame shows that flooding depends on the destination. D has never sent anything, so neither switch has learned it, and the frame floods again. The table also shows that SW2 has learned B on port 3, even though that frame never reached D's reply path.
Filtering on an uplink
The filtering rule still applies. A switch drops a frame whose destination is on the port it arrived on.
Suppose SW1 has learned C and D on port 3, and C sends a frame to D. The frame never reaches SW1, because SW2 delivers it locally. But if a hub or an unmanaged switch sits on the uplink path, the frame can arrive at SW1's port 3 with a destination that SW1 also associates with port 3. SW1 filters it. Without this rule, the frame would return to the segment it came from.
Trunks and VLANs
Uplinks between switches often carry several VLANs on one trunk port. The table is then keyed by VLAN and MAC address together, not by MAC address alone:
- The same MAC address can appear in two VLANs, possibly on different ports.
- A flooded frame goes out only on ports that carry its VLAN.
- On a trunk, each frame is tagged with its VLAN ID, so the next switch learns the source address in the right VLAN.
A trunk port can therefore hold entries from many VLANs. On Cisco-style output, look at the Vlan column to tell them apart.
Aging across switches
Entries behind an uplink are refreshed only when the remote host sends a frame. A host that only receives traffic, such as a printer or a UDP listener, never refreshes its entry on any switch. After the aging time (300 seconds by default on many switches), the entry expires and the next frame to that host floods.
This affects every switch on the path. The far-side switch ages the entry for a host at its own pace, and the aging timers on different switches are independent.
Common errors
- Assuming a switch knows the topology. A switch knows only the direction to each address. It cannot tell you how many switches lie behind a port.
- Expecting one MAC address per port. Uplinks, hypervisor hosts, Wi-Fi access points, and IP phones with a PC attached all put several addresses behind one port.
- Loops. If two paths connect the same pair of switches and no loop prevention runs, a flooded frame returns on a different port. Each switch then relearns the source on the new port, and the entry flips back and forth. This is MAC flapping, covered in the host-move chapter. Spanning tree prevents it by blocking one of the paths.
- Port security on an uplink. A rule such as "maximum 1 MAC address per port" works on an access port and breaks an uplink. The port sees many sources and shuts down.
- Reading the wrong switch. If a host's MAC address shows up on a trunk port, the host is behind that trunk. Follow the entry to the next switch and repeat the lookup until you find an access port.
Finding a host in a switched network
To locate a host by its MAC address, follow the table from switch to switch:
- Look up the address on the switch you can reach.
- If the port is an access port, the host is connected to it.
- If the port is an uplink, log in to the switch on the other end and look up the same address.
- Repeat until the address is found on an access port.
On a Linux bridge, bridge fdb show br br0 does the same job. The dev field names the port, and entries behind a bridge uplink show the uplink interface.
Exercise
SW1 has learned that host C is on port 3, which is its uplink to SW2. SW2 is then rebooted and its table is cleared. Host A sends a frame to host C. Describe what SW1 and SW2 do with it, and what each switch learns.
Show answer
SW1 looks up C and finds port 3, so it forwards the frame out of port 3 only. SW2 receives it on its uplink and learns A on that port. SW2 has no entry for C, so it floods the frame out of every other port, and C receives it. Once C replies, SW2 learns C on its access port and the next frames are forwarded without flooding. SW1 changes nothing.
Exercise
A printer on SW2 only receives traffic and never sends. A user on SW1 prints every hour. Why does every print job begin with a flooded frame, and how would you avoid it?
Show answer
The printer never sends a frame, so neither switch learns its address, or the entry ages out after the aging time of 300 seconds, which is shorter than an hour. The first frame of each job is therefore unknown unicast and floods. A static MAC table entry for the printer on each switch, or a longer aging time, stops the flooding.