Sub topic 9
How a Learning Switch Fills and Uses Its Table
This chapter assumes you know that a switch works at the data link layer and that frames carry source and destination MAC addresses. It also assumes you know what a hub does, since the contrast with a hub explains why learning switches exist. By the end you should be able to trace a learning switch's table frame by frame, which is a common GATE question.
Why a switch needs a table
A hub repeats every incoming bit on all other ports. All hosts on a hub share one collision domain, so only one frame can be in transit at a time. A switch (or bridge) instead looks at the destination MAC address of each frame and forwards it only toward the port where that host lives.
To do this the switch needs a forwarding table that maps a MAC address to a port. Nobody configures this table by hand. The switch builds it by watching the traffic that passes through it. This is called backward learning, or transparent learning, because the hosts do not know the switch exists.
Each table entry holds three things:
- a MAC address,
- the port on which a frame from that address was last seen,
- an age timer, so stale entries can be removed.
The learning algorithm
For every frame that arrives on port P, the switch performs two independent steps.
- Learn. Read the source MAC address S. Record "S is reachable via port P". If S is already in the table, refresh the port and reset the timer.
- Forward. Read the destination MAC address D and look it up.
- If D is the broadcast address, or D is not in the table (an unknown unicast), flood: send the frame out of every port except P.
- If D is in the table with port Q, and Q differs from P, forward the frame out of port Q only.
- If D is in the table with port Q, and Q equals P, filter: discard the frame. The destination is on the same segment as the sender, so it has already received the frame.
Learning always uses the source address. Forwarding always uses the destination address. Mixing these up is the most common error in exam answers.
A worked trace
Consider a switch with four ports. Host A is on port 1, B on port 2, C on port 3, and D on port 4. The table starts empty. These frames arrive in order.
| Step | Frame (source to destination) | Arrives on port | Table after learning | Action |
|---|---|---|---|---|
| 1 | A to B | 1 | A:1 | B unknown, flood to ports 2, 3, 4 |
| 2 | B to A | 2 | A:1, B:2 | A known on port 1, forward to port 1 only |
| 3 | C to B | 3 | A:1, B:2, C:3 | B known on port 2, forward to port 2 only |
| 4 | D to A | 4 | A:1, B:2, C:3, D:4 | A known, forward to port 1 only |
| 5 | A to D | 1 | unchanged | D known, forward to port 4 only |
After step 1, the switch knows only where A is. After step 2, the reply from B teaches it where B is, and from then on traffic between A and B is no longer flooded. Hosts that send nothing are never learned. A host that only receives, such as a passive listener, stays unknown, and frames sent to it are flooded every time.
A typical GATE question asks how many frames are flooded, or how many entries the table holds after a sequence of frames. Work through the table row by row as above and the answer follows.
A simulation you can run
The following program implements the algorithm. It returns the ports on which each frame is sent, which makes the flood, forward and filter cases visible.
class LearningSwitch:
def __init__(self, num_ports):
self.ports = list(range(1, num_ports + 1))
self.table = {} # MAC address -> port
def receive(self, src, dst, in_port):
"""Process one frame. Return the list of output ports."""
self.table[src] = in_port # learn from the source
if dst == "broadcast" or dst not in self.table:
return [p for p in self.ports if p != in_port] # flood
out_port = self.table[dst]
if out_port == in_port:
return [] # filter
return [out_port] # forward
sw = LearningSwitch(4)
frames = [("A", "B", 1), ("B", "A", 2), ("C", "B", 3), ("D", "A", 4), ("A", "D", 1)]
for src, dst, port in frames:
print(f"{src}->{dst} on port {port}: out {sw.receive(src, dst, port)}")
print(sw.table)The output is:
A->B on port 1: out [2, 3, 4]
B->A on port 2: out [1]
C->B on port 3: out [2]
D->A on port 4: out [1]
A->D on port 1: out [4]
{'A': 1, 'B': 2, 'C': 3, 'D': 4}Aging and host movement
Table space is finite, and hosts move or get switched off. Each entry carries a timer, typically a few minutes (300 seconds is a common default). Every frame from that source resets the timer. When the timer expires, the entry is deleted. The next frame to that address is then flooded until the host speaks again.
Aging also handles mobility. If host A is unplugged from port 1 and plugged into port 3, its next frame arrives on port 3. The learning step overwrites A:1 with A:3, so the table corrects itself after one frame. Until A sends something, frames addressed to A still go to port 1 and are lost.
Switches, collision domains and broadcast domains
A learning switch changes the domain structure of a network.
| Device | Collision domains | Broadcast domains |
|---|---|---|
| Hub with n ports | 1 for the whole device | 1 |
| Switch with n ports | n, one per port | 1 (without VLANs) |
| Router with n ports | n | n, one per port |
Each switch port is its own collision domain, and a full-duplex link has no collisions at all. Broadcasts and floods still reach every port, so the switch does not divide the broadcast domain. Only a router, or a VLAN configuration, does that. Counting domains is a standard GATE question, so memorize this table.
Edge cases and common errors
- Learning from broadcast sources. A broadcast address is never a valid source, so it is never learned. A frame sent to broadcast still teaches the switch about its sender.
- Flooding excludes the arrival port. A flooded frame goes out of all ports except the one it came in on. A four-port switch floods to three ports.
- Several hosts behind one port. If port 2 connects to a hub with hosts B and E, both map to port 2. A frame from B to E arrives on port 2 and is filtered, because the hub already delivered it.
- Loops. If two switches are joined by two links, a flooded frame circulates forever, and the table entries flip between ports. This is a broadcast storm. Switched networks with redundant paths run the Spanning Tree Protocol, which disables some links so that the topology becomes a tree.
- Table overflow. A switch has a finite table. If an attacker sends frames with many fake source addresses (MAC flooding), legitimate entries are pushed out and the switch floods more traffic, which exposes it to eavesdropping.
- No effect on addressing. A switch does not change the MAC addresses in a frame. A router, in contrast, rewrites them at every hop.
Switch versus bridge
In GATE questions the words bridge and switch are often used for the same behavior. A traditional bridge has few ports and forwards in software. A modern switch has many ports and forwards in hardware, and many can use cut-through forwarding, which starts sending a frame after reading only the destination address. A store-and-forward switch waits for the whole frame so it can check the frame check sequence first. Both use the same learning algorithm described here.
Exercise
A learning switch has 5 ports and an empty table. Host P is on port 1, Q on port 2, R on port 3. These frames arrive in order: (1) P to Q on port 1, (2) Q to P on port 2, (3) R to P on port 3, (4) P to R on port 1, (5) Q to R on port 2. For each frame, give the output ports, and then give the final table.
Show answer
(1) Q is unknown, so the frame is flooded to ports 2, 3, 4, 5. Table: P:1. (2) P is known on port 1, so the frame goes to port 1 only. Table: P:1, Q:2. (3) P is known on port 1, so the frame goes to port 1 only. Table: P:1, Q:2, R:3. (4) R is known on port 3, so the frame goes to port 3 only. (5) R is known on port 3, so the frame goes to port 3 only. The final table is P:1, Q:2, R:3. Only frame 1 was flooded.
Exercise
Two switches are connected by a single link. Switch S1 has hosts A and B, and switch S2 has hosts C and D. How many collision domains and how many broadcast domains does the whole network have, if each host connects to its switch by its own port?
Show answer
S1 has 3 ports (A, B, and the link to S2) and S2 has 3 ports (C, D, and the link). Each port is a separate collision domain, but the inter-switch link is shared by both switches, so it counts once. That gives 2 + 2 host links plus 1 inter-switch link, which is 5 collision domains. Switches do not separate broadcast domains, so there is 1 broadcast domain.