Sub topic 9
How a Learning Switch Fills and Uses Its Table
Prerequisites and working model
Assume you know that a local network uses Ethernet frames and that each network interface has a MAC address. A MAC address is a link-layer address used to deliver a frame on one local network. A switch connects several ports and forwards frames by using a table that maps MAC addresses to ports.
The table is called a forwarding table, MAC table, or CAM table. CAM means content-addressable memory, a fast memory used by many switches. Each entry usually contains:
| MAC address | Port | Age |
|---|---|---|
| A1:A1:A1:A1:A1:A1 | 1 | Recent |
The switch does not need a manually entered entry for every device. It learns entries from the source address of incoming frames.
Learning and forwarding steps
When a frame arrives on port 1, the switch performs these steps:
- It reads the frame's source MAC address.
- It records that the source address is reachable through port 1.
- It reads the destination MAC address.
- It looks up the destination in its table.
- It forwards, filters, or floods the frame.
The switch handles the destination in one of three ways:
- If the destination is known on a different port, it sends the frame only through that port.
- If the destination is known on the same incoming port, it filters the frame instead of sending it back.
- If the destination is unknown, it floods the frame through every other port in the same broadcast domain.
Flooding is also used for broadcast frames. A broadcast destination is intended for all devices in the local broadcast domain.
A switch learns from every incoming frame, including a frame whose destination is unknown. It does not need to understand the application data. It uses the Ethernet header, especially the source and destination MAC addresses.
Worked trace
Suppose a switch starts with an empty table. Host A sends a frame to Host B through port 1. The switch learns A -> 1. If B is not yet in the table, the switch floods the frame through ports 2, 3, and 4. Host B receives it and may reply through port 3.
When the reply arrives, the switch learns B -> 3. It already knows A -> 1, so it sends the reply only to port 1. Later frames between A and B are unicast through the correct ports.
If a device moves to another port, a new frame from that device updates the entry. Switches also remove entries after an aging time. Aging prevents stale mappings from remaining forever.
Common errors and GATE-style reasoning
Do not confuse learning with forwarding. The source address determines which table entry is updated. The destination address determines where the current frame goes.
A switch does not normally forward a known unicast frame back through its incoming port. It filters that frame. An unknown unicast is flooded, but the switch still learns its source address.
A basic switch does not route between different IP networks. A router performs that network-layer task. A switch's MAC table is local to its broadcast domain and can contain separate logical tables when VLANs are configured.
def process_frame(table, source, destination, incoming_port, ports):
table[source] = incoming_port
if destination in table:
output = [] if table[destination] == incoming_port else [table[destination]]
else:
output = [port for port in ports if port != incoming_port]
return output
ports = [1, 2, 3, 4]
table = {}
print(process_frame(table, "A", "B", 1, ports), table) # [2, 3, 4]
print(process_frame(table, "B", "A", 3, ports), table) # [1]
print(process_frame(table, "A", "B", 1, ports), table) # [3]Exercise
A frame with source C and destination A enters port 4. The table contains A -> 2 but no entry for C. Which entry is learned, and where is the frame sent?
Show answer
The switch learns C -> 4 and sends the frame only through port 2, because the destination A is known on a different port.