How does a switch learn and use MAC addresses to forward frames?
Interview preparation resource from Gate Smashers.
A switch learns MAC addresses by examining the source MAC address of each received Ethernet frame and recording the source MAC, incoming port, and VLAN in its MAC address table. When forwarding the frame, it looks up the destination MAC address in the table. A known unicast frame is sent only to the associated port; an unknown unicast frame is flooded to other ports in the same VLAN; and a broadcast frame is flooded within the VLAN. Entries age out after inactivity, allowing the switch to adapt when devices move.

MAC address learning
When a frame arrives, the switch reads its source MAC address and associates that address with the port on which the frame was received. The association is maintained in the switch's MAC address table, also called a forwarding table or CAM table.
MAC learning is VLAN-specific: the same MAC address is associated with a port within a particular VLAN. If the switch later receives a frame with the same source MAC on a different port, it updates the table to reflect the new location.
Forwarding decisions
After learning from the source address, the switch examines the destination MAC address and consults its MAC address table to determine where to send the frame.
- Known unicast: If the destination MAC is known in the same VLAN, the switch forwards the frame only through the port mapped to that MAC address.
- Unknown unicast: If no table entry exists for the destination MAC, the switch floods the frame out eligible ports in the same VLAN, except the port on which it arrived.
- Broadcast: A frame addressed to the Ethernet broadcast address is flooded out eligible ports in the same VLAN, except the incoming port.
- Same-port destination: If the destination MAC maps to the same port on which the frame arrived, the switch filters the frame rather than sending it back out that port.
Table aging and adaptation
Dynamically learned MAC address entries are normally removed after an inactivity period. Aging prevents stale entries from remaining indefinitely and helps the switch adapt when a device is disconnected or moved to another port.
This learning, forwarding, flooding, and aging process lets a switch build its forwarding information automatically without requiring a manually configured entry for every attached device.
