Sub topic 6
Looking up the destination and forwarding
This chapter assumes you know the Ethernet frame fields (destination MAC, source MAC, EtherType). It also assumes you know what a MAC address is and how a switch differs from a hub and a router. Earlier chapters covered what the MAC address table stores and how the switch learns source addresses. Here you follow the other half of the work: what a switch does with the destination address once the source has been recorded.
Where the lookup fits
For every frame that arrives, a switch does two independent things:
- It reads the source address and updates the table (learning).
- It reads the destination address and decides where the frame goes (forwarding).
The two steps use the same table but ask different questions. Learning asks "which port is this sender on?" Forwarding asks "which port is this receiver on?" The ingress port is the port the frame arrived on. The egress port is the port the switch sends it out of.
The destination address is the first field of an Ethernet frame, so the switch can start the lookup after reading only the first six bytes after the preamble. That is why some switches can begin forwarding before the whole frame has arrived (cut-through switching). Others receive the whole frame and check its checksum first (store-and-forward). The forwarding decision is the same in both cases.
The lookup itself
The switch uses the destination MAC address, plus the VLAN the frame belongs to, as a key. It searches the table for an exact match. If a matching entry exists, it returns a port. If none exists, the lookup returns nothing.
Two properties of this lookup matter in practice:
- Exact match, not longest prefix. A router compares an IP address against prefixes and picks the most specific one. A switch compares all 48 bits of the MAC address, and an entry either matches or it does not. There is no summarization, so a switch with 20,000 known hosts needs 20,000 entries.
- Constant time. Hardware switches keep the table in content-addressable memory (CAM) or a hash-indexed table, so the lookup takes about the same time whether the table holds ten entries or ten thousand. Linux bridges use a hash table in software with the same effect.
In this tutorial's model, the per-VLAN part of the key stays at a single VLAN to keep the examples short. A later chapter covers VLANs and per-VLAN learning.
The three outcomes
After the lookup, the switch has one of three results.
| Lookup result | Condition | Action |
|---|---|---|
| Forward | Destination is in the table, on a port other than the ingress port | Send the frame out of that one port only |
| Filter | Destination is in the table, on the ingress port itself | Drop the frame, because the receiver already saw it |
| Flood | Destination is not in the table, or is a broadcast or multicast address | Send the frame out of every port in the VLAN except the ingress port |
A forwarded frame is called known unicast. This chapter focuses on that case and on the decision order. Flooding and filtering each have their own chapter, so here they appear only as the outcomes the decision can produce.
Why group addresses skip the table
A destination is a group address when the least significant bit of its first octet is 1. This covers broadcast (ff:ff:ff:ff:ff:ff) and all multicast addresses. A group address can never be the source of a frame, so the switch never learns one. The table has no entry for it, and a lookup would always fail.
Most implementations check for group addresses first, as the diagram shows. Some multicast features (such as IGMP snooping) do create group entries, but they are built by a different mechanism and not by source learning.
A runnable decision function
The following function isolates the forwarding half of a switch. It takes a table that maps each MAC address to a port, and it returns the action and the egress ports. It does not learn or age entries.
def is_group(mac):
# The least significant bit of the first octet marks multicast/broadcast.
return int(mac.split(':')[0], 16) & 1 == 1
def decide(table, ports, in_port, dst):
"""Return (action, egress_ports) for one frame."""
if is_group(dst):
return "flood", sorted(ports - {in_port})
out_port = table.get(dst)
if out_port is None:
return "flood", sorted(ports - {in_port})
if out_port == in_port:
return "filter", []
return "forward", [out_port]
A = "00:00:00:00:00:0a"
B = "00:00:00:00:00:0b"
C = "00:00:00:00:00:0c"
table = {A: 1, B: 2}
ports = {1, 2, 3}
print(decide(table, ports, 1, B)) # ('forward', [2])
print(decide(table, ports, 1, C)) # ('flood', [2, 3])
print(decide(table, ports, 3, "ff:ff:ff:ff:ff:ff")) # ('flood', [1, 2])
print(decide(table, ports, 1, A)) # ('filter', [])The last call shows a frame whose destination A is known to be on port 1, and the frame also arrived on port 1. Sending it back would only duplicate a frame the receiver has already seen, so the switch filters it. You will meet this case when two hosts share one port through a hub or a downstream switch.
Learning first, then lookup
The order inside the switch matters. The full model from the earlier chapter learns the source before it looks up the destination:
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 = {} # 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]
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]Read the output line by line:
- The first frame from A to B floods, because B has never sent anything. The switch still learns A on port 1.
- B replies. The switch learns B on port 2, and A is already known, so the reply goes only to port 1.
- From now on, A to B is forwarded to port 2 alone.
- After a long silence, the entry for B has expired, so the switch floods again until B transmits.
Because learning comes first, a reply to a flooded frame always finds its sender in the table. That is why a conversation usually floods only once or twice before both sides are known.
Common errors and edge cases
- A host is silent, so traffic to it floods. The table only knows hosts that have sent frames. A host that only receives (some printers, monitoring taps, a server after an idle period) stays unknown, and every frame sent to it floods. This is correct behavior, not a fault.
- Unidirectional traffic keeps flooding. Suppose A sends a continuous stream to B, but B never replies, as with some UDP streams. B is never learned, and each frame floods.
- Frames to the switch itself. If the destination is the switch's own management MAC address, the table points to an internal port (the CPU), not to a front-panel port. The frame is delivered to the switch's control plane and not forwarded out.
- Blocked or down ports. The entry can name a port that spanning tree has put into a blocking state. The switch does not send the frame out of a blocking port. This is one reason topology changes cause brief outages, and a later chapter covers it.
- Several hosts behind one port. One egress port can map to many MAC addresses. With an uplink to another switch, the table holds all the remote hosts' addresses pointing at the uplink.
Checking the result on a real device
You can confirm a forwarding decision by looking at the table. On a Linux bridge, the following command lists which port each MAC address was learned on:
bridge fdb show br br0If a destination MAC appears there with dev eth2, a frame to that address is forwarded out of eth2 (unless it arrived on eth2, in which case it is filtered). If the address is missing, frames to it flood.
Exercise
A switch has ports 1 to 4. Its table holds H1 on port 1, H2 on port 2, and H3 on port 2 (H2 and H3 share a downstream switch). For each frame, give the action and egress ports: (a) H1 sends to H3 on ingress port 1; (b) H2 sends to H3 on ingress port 2; (c) H3 sends to a never-seen host H9 on ingress port 2; (d) H1 sends to ff:ff:ff:ff:ff:ff on ingress port 1.
Show answer
(a) Forward out port 2 only. (b) Filter, because H3 is on the ingress port 2, so the downstream switch already delivered it. (c) Flood to ports 1, 3 and 4, because H9 is unknown. (d) Flood to ports 2, 3 and 4, because broadcast is never in the table and always floods.
Exercise
Why does the switch learn the source address before it looks up the destination, and what would be different if it did the lookup first for a frame that is the very first one from a new host to a known host?
Show answer
Learning first guarantees the table is current before the decision. For a first frame from a new host to a known host, the destination lookup succeeds either way, so the frame is forwarded identically. The order matters for the reply and for hosts moving ports: the table already reflects the sender's current port. In the "frame to self" case where the destination is on the ingress port, a stale entry could also cause an incorrect forward instead of a filter.