Sub topic 8
Filtering: when the frame is not forwarded at all
This chapter assumes you know the Ethernet frame fields (destination MAC, source MAC, EtherType), the difference between unicast, broadcast and multicast addresses, and how a switch learns source addresses and looks up destinations.
What filtering means
A switch has three possible outcomes for a frame: forward it to one port, flood it to every other port, or drop it. Filtering is the third outcome. The switch discards the frame because sending it anywhere would be pointless.
The most common case is simple. The switch looks up the destination MAC address and finds that the table entry points to the same port the frame arrived on. The destination host is already reachable on that port, so it has already received the frame.
When it happens
Filtering happens when two or more hosts share one port. Typical cases:
- A hub or an unmanaged switch is plugged into a switch port, with several hosts behind it.
- Another switch is connected by an uplink, and the hosts on that far side talk to each other.
- A virtualization host exposes several virtual machines through one physical port.
Hosts that share a port can already hear each other. The shared segment delivers the frame to them directly. If the switch also sent the frame back out the ingress port, every host there would receive a duplicate copy, and bandwidth would be wasted.
A worked example
Hosts A and C sit behind a small hub on port 1. Host B is on port 2.
- A sends a frame to C. The switch learns that A is on port 1.
- C is unknown, so the switch floods the frame to ports 2 and 3. C replies to A, and the switch learns that C is on port 1.
- A sends another frame to C. The switch finds C on port 1, which is the ingress port. It filters the frame.
- B never sees that frame. The conversation between A and C stays off port 2.
Filtering in code
The learning switch from the earlier chapters already contains this rule. The last line returns an empty list when the table entry points to the ingress port.
import time
AGING_SECONDS = 300
def is_group(mac):
return int(mac.split(':')[0], 16) & 1 == 1
class LearningSwitch:
def __init__(self, ports, aging=AGING_SECONDS):
self.ports = set(ports)
self.aging = aging
self.table = {}
def handle(self, in_port, src, dst, now=None):
now = time.monotonic() if now is None else now
if not is_group(src):
self.table[src] = (in_port, now)
entry = None if is_group(dst) else self.table.get(dst)
if entry is not None and now - entry[1] > self.aging:
del self.table[dst]
entry = None
if entry is None:
return sorted(self.ports - {in_port})
out_port = entry[0]
return [] if out_port == in_port else [out_port]
sw = LearningSwitch(ports=[1, 2, 3])
A = '00:00:00:00:00:0a'
C = '00:00:00:00:00:0c'
print(sw.handle(1, A, C, now=0)) # C unknown: flood -> [2, 3]
print(sw.handle(1, C, A, now=1)) # A is on port 1, the ingress port -> []
print(sw.handle(1, A, C, now=2)) # C is on port 1 -> []The second and third calls print an empty list. No port receives the frame.
Common mistakes
- Believing a filtered frame is lost. The destination already received it on the shared segment.
- Expecting to see filtering when each host has its own port. With one host per port, a known destination is never on the ingress port, so this case does not arise.
- Forgetting that the source is learned first. Even a filtered frame refreshes the sender's table entry.
Exercise
A switch has learned that host X is on port 4. A frame from host Y arrives on port 4 with destination X. Which source MAC does the switch learn, and what does it do with the frame?
Show answer
It learns (or refreshes) Y on port 4. X is also on port 4, which is the ingress port, so the switch filters the frame and sends it nowhere.