Sub topic 6
Looking up the destination and forwarding
This chapter assumes you know the Ethernet frame fields (destination MAC, source MAC, EtherType) and that the switch already keeps a MAC address table, filled by learning from source addresses. Here the focus is the second half of the job: using that table to decide where a frame goes.
The decision in order
For every frame that arrives, the switch learns the source first, then looks up the destination. The lookup produces one of three outcomes.
- If the destination is a broadcast or multicast address, the switch does not search the table. It floods the frame.
- If the destination is a unicast address with no table entry, the switch floods it. This is called unknown unicast.
- If the destination is a unicast address with an entry, the switch compares the stored port with the ingress port. A different port means forward to that one port. The same port means filter, so the frame is dropped.
The lookup in code
The handle method of the LearningSwitch below returns the list of ports the frame leaves on. The group-address check uses the least significant bit of the first octet, which marks multicast and broadcast.
import time
AGING_SECONDS = 300
def is_group(mac):
# The least significant bit of the first octet marks multicast/broadcast.
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 = {} # mac -> (port, last_seen)
def handle(self, in_port, src, dst, now=None):
"""Return the list of ports the frame is sent out of."""
now = time.monotonic() if now is None else now
# 1. Learn or refresh the source address (this also handles host moves).
if not is_group(src):
self.table[src] = (in_port, now)
# 2. Look up the destination.
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] # expired entry
entry = None
if entry is None:
# Unknown unicast, broadcast or multicast: flood.
return sorted(self.ports - {in_port})
out_port = entry[0]
# Destination is behind the ingress port: filter the frame.
return [] if out_port == in_port else [out_port]Step 1 runs before step 2. A frame from a host to itself, or a reply to a host that just spoke, therefore finds the source already in the table.
Tracing four frames
sw = LearningSwitch(ports=[1, 2, 3])
A = '00:00:00:00:00:0a'
B = '00:00:00:00:00:0b'
print(sw.handle(1, A, B, now=0)) # B unknown: flood -> [2, 3]
print(sw.handle(2, B, A, now=1)) # A was learned on port 1 -> [1]
print(sw.handle(1, A, B, now=2)) # B was learned on port 2 -> [2]
print(sw.handle(1, A, B, now=400)) # B aged out (400 - 1 > 300): flood -> [2, 3]The first frame floods because B is unknown, but it teaches the switch that A is on port 1. B's reply is then forwarded to port 1 only, and it teaches the switch that B is on port 2. From then on, traffic between A and B uses a single port in each direction. The last frame floods again because B's entry was last refreshed at time 1 and has been idle for 399 seconds.
Common errors
- Looking up the source instead of the destination. The source address is only used for learning. Forwarding uses the destination.
- Learning group addresses. Broadcast and multicast addresses are never valid sources, so a switch does not record them. The code skips them with
is_group. - Expecting a table entry for broadcast. Broadcasts are flooded by rule. No entry is consulted.
- Treating filtering as an error. A frame whose destination sits behind the ingress port is dropped on purpose. This happens when a hub or another switch connects several hosts to one port. The destination already received the frame on that shared segment.
Exercise
A switch with ports 1, 2 and 3 has learned host X on port 2 and host Y on port 3. A frame from X to Y arrives on port 2, and then a frame from Y to an unknown host Z arrives on port 3. Which ports does each frame leave on?
Show answer
The X-to-Y frame leaves on port 3 only, because Y's entry points to port 3 and differs from the ingress port 2. The Y-to-Z frame floods, because Z is unknown, so it leaves on ports 1 and 2 (every port except ingress port 3).
Exercise
A frame arrives on port 4 with a destination address of ff:ff:ff:ff:ff:ff. Does the switch consult the MAC table to decide the output ports? Explain.
Show answer
No. The first octet 0xff has its least significant bit set, so the address is a group address. The switch floods it out every port except port 4 without a table lookup.