How an Ethernet switch learns where to forward a frame
See a switch learn each machine's port from the frames it sends, forward to one port, flood when it must, and cope with a machine that moves. Switching and routing · Computer networks.
You plug a new four-port switch into a laptop, a printer, a phone and a tablet. The switch has never seen any of them, so when the laptop sends its first frame to the printer, the switch cannot know which socket the printer is on. It could copy every frame to every port and everything would still arrive, but every machine would receive every conversation. Once the printer sends a frame and the switch learns its port, the next unicast to it can use one port. How does it find out where each machine is when nobody tells it?
Learn from the sender
An Ethernet frame carries two MAC addresses (the 48-bit link labels from MAC, IP and ARP): the source, who sent it, and the destination, who it is for. The switch keeps a forwarding table that maps a MAC address and VLAN to a port. Our model uses dynamic learning for valid ordinary frames, with all listed ports forwarding in one VLAN and no filtering policies. It follows two conceptual steps in order.
- Learn. Record the source MAC against the ingress port, the port the frame arrived on. A row that already says this port is refreshed; a row that says another port is moved.
- Look up the destination. A known unicast on a different eligible port gets one copy out of that egress port. A known address on the ingress port is filtered: the frame is dropped. An unknown unicast address, or the broadcast address ff:ff:ff:ff:ff:ff, is flooded: a copy goes out of every other eligible forwarding port in the same VLAN. A VLAN is a logical Layer 2 broadcast domain; port state and policy can restrict forwarding.
The order matters because the arrival port tells the switch where the sender is, and that port may lead toward a machine on another switch. The destination says only where the sender wants the frame to go.
A worked trace
Write each machine by the last byte of its MAC address: laptop 0a on p2, printer 0b on p4, phone 0c on p1 and tablet 0d on p3. The table starts empty.
- The laptop sends to the printer. The switch learns 0a on p2. Address 0b is unknown, so the frame is flooded out of p1, p3 and p4. With ordinary destination filtering, the phone and tablet ignore their copies and the printer accepts its copy.
- The printer replies. The switch learns 0b on p4. Address 0a is known on p2, so one copy goes out of p2 and none anywhere else.
- The laptop sends to the printer again. Row 0a is refreshed on p2, and 0b is known on p4, so one copy goes out of p4. Here, one frame from each side was enough to learn both rows.
- The phone sends a broadcast. The switch learns 0c on p1 and floods out of p2, p3 and p4. Broadcast is never a source address, so it never gets a row, and it floods however full the table is.
- The tablet sends to 0e, a machine that has not sent a frame yet. The switch learns 0d on p3 and, with no row for 0e, floods out of p1, p2 and p4.
The animations name each machine by a device (tv, nas, cam) instead of an address byte. In the first one the second frame comes from a machine nobody has written to, so frames go to machines that have already spoken and not only to whoever is replying.
A new switch learns from the first exchange
Read the steps as text
- A switch with 5 ports connects 5 hosts. The forwarding table starts empty.
- On p1, host tv sends a frame to nas. The switch learns tv on p1. Destination nas is not in the table, so the frame goes out of every port but p1.
- On p3, host cam sends a frame to tv. The switch learns cam on p3. Destination tv is on p1, so only p1 gets a copy.
- On p2, host nas sends a frame to cam. The switch learns nas on p2. Destination cam is on p3, so only p3 gets a copy. Altogether the switch sent 6 copies for 3 frames.
The second animation starts with a table that already lists five of six machines, then sends a frame to a known machine, a broadcast and a frame to the sixth machine, which has not sent anything yet.
Some frames flood whatever the table holds
Read the steps as text
- A switch with 6 ports connects 6 hosts. The forwarding table starts with 5 addresses listed.
- On p1, host register sends a frame to scale. The switch refreshes register on p1. Destination scale is on p2, so only p2 gets a copy.
- On p1, host register sends a broadcast frame. The switch refreshes register on p1. A broadcast goes out of every port but p1.
- On p4, host door sends a frame to sensor. The switch refreshes door on p4. Destination sensor is not in the table, so the frame goes out of every port but p4. Altogether the switch sent 11 copies for 3 frames.
When a machine moves or goes quiet
A dynamic row records the last port where the switch learned that source. Moving or aging can change it.
Suppose the laptop is unplugged from p2 and joins a small hub behind p4, where the printer already sits. A hub repeats signals it receives to its other attached ports, so the laptop and the printer now share one segment. The laptop's next frame to the printer arrives on p4. The switch moves row 0a from p2 to p4, then looks up 0b and finds p4, the ingress port. The hub has already repeated the frame toward the printer, so sending it back would repeat it; the switch filters it.
Until the laptop speaks again its row is wrong. A frame for 0a from the phone is still copied out of p2, where nothing listens now, and is lost. The laptop's first frame from the new place repairs the row.
The third animation puts a hub on p1 beside a server and starts with every row correct. The laptop is then plugged into that hub; a frame sent to it still goes out of its old port, and the laptop's first frame repairs the row and is filtered.
A device moves to another port
Read the steps as text
- A switch with 4 ports connects laptop on p3, server on p1, scanner on p2 and camera on p4. The forwarding table starts with 3 addresses listed.
- On p2, host scanner sends a frame to laptop. The switch refreshes scanner on p2. Destination laptop is on p3, so only p3 gets a copy.
- Host laptop moves from p3 to p1. On p2, host scanner sends a frame to laptop. The switch refreshes scanner on p2. Destination laptop is on p3, so only p3 gets a copy, but laptop is no longer there.
- On p1, host laptop sends a frame to server. The switch moves laptop from p3 to p1. Destination server is on p1 as well, so the switch filters the frame.
- On p2, host scanner sends a frame to laptop. The switch refreshes scanner on p2. Destination laptop is on p1, so only p1 gets a copy. Altogether the switch sent 3 copies for 4 frames.
A switch also forgets. A dynamic row that no source frame has refreshed for the aging time is removed; 300 seconds is a common default, and it is configurable. In this model, without a static entry or filtering rule, a later frame for that address is an unknown unicast and floods until the source is relearned. Aging keeps the table from filling with machines that have left.
Recap
- A switch finds machines from the frames they send: it records the source MAC against the ingress port, then looks up the destination.
- A known unicast on another eligible port gets one copy. In this model unknown unicasts and broadcasts flood the other eligible VLAN ports.
- A dynamic row moves when its source appears on another port and ages out after inactivity; a known destination on the ingress port is filtered.
Beyond one flat switch
The examples keep each VLAN's learning and flooding separate. In an ordinary configuration, traffic between VLANs needs a Layer 3 device. Hubs, switches and VLANs separates collision domains from broadcast domains and shows the tagging that carries several VLANs on one link.
A Layer 2 loop can circulate flooded frames repeatedly, so networks use loop-avoidance mechanisms such as spanning tree.
ARP is a separate question: it is how a host learns the MAC address of a neighbour before it can send the frame this lesson starts from.
Continue with Longest-prefix routing to compare this local-link decision with an IPv4 route lookup.