Sub topic 19
Learning switches and collision domains
This chapter assumes you know the Ethernet frame fields and MAC addresses from the earlier chapters. It also assumes you know how CSMA/CD detects collisions and why the minimum frame size exists. Here we see how a switch removes most of those collisions and how it builds its forwarding table without configuration.
From shared medium to switched LAN
On a hub, or on the original coaxial bus, every bit sent by one station reaches every other station. All stations compete for one channel, so they form one collision domain: the set of stations whose simultaneous transmissions would collide. CSMA/CD, backoff and the 64-byte minimum frame all exist because of this sharing.
A switch is a link-layer device that reads the destination MAC address of each frame and forwards it only where it needs to go. Each switch port is a separate collision domain. A frame arriving on one port is received completely, then transmitted on another port using that port's own medium access rules.
A broadcast domain is the set of stations that receive a frame sent to the broadcast address ff:ff:ff:ff:ff:ff. A switch floods broadcasts out of every port, so all ports of one switch (without VLANs) share one broadcast domain. A router does not forward link-layer broadcasts, so each router interface starts a new broadcast domain.
| Device | Layer | Collision domains | Broadcast domains |
|---|---|---|---|
| Repeater or hub | 1 | One for all ports | One |
| Switch or bridge | 2 | One per port | One (without VLANs) |
| Router | 3 | One per interface | One per interface |
The learning algorithm
A switch has a forwarding table that maps MAC addresses to ports. Nobody fills it in by hand. The switch learns it from traffic, using three rules for each arriving frame:
- Learn. Record the frame's source address against the port it arrived on, and refresh that entry's timer.
- Look up. Search the table for the destination address.
- Decide. There are three outcomes:
- Flood. If the destination is a broadcast address or is not in the table, send the frame out of every port except the arrival port.
- Filter. If the table says the destination is on the arrival port, discard the frame. The destination already saw it on that shared segment.
- Forward. Otherwise, send the frame only out of the port in the table.
Entries expire after an aging time, commonly 300 seconds. Aging removes stale entries after a host is unplugged, and it lets the switch relearn a host that moves to another port.
A runnable model
The class below implements the three rules. Aging is left out to keep the code short.
BROADCAST = "ff:ff:ff:ff:ff:ff"
class LearningSwitch:
def __init__(self, ports):
self.ports = list(ports)
self.table = {} # MAC address -> port
def receive(self, in_port, src, dst):
"""Return the list of ports the frame is sent out of."""
self.table[src] = in_port # learn
if dst == BROADCAST or dst not in self.table: # flood
return [p for p in self.ports if p != in_port]
out_port = self.table[dst]
if out_port == in_port: # filter
return []
return [out_port] # forward
sw = LearningSwitch([1, 2, 3, 4])
print(sw.receive(1, "A", "B")) # [2, 3, 4] B unknown, flood
print(sw.receive(2, "B", "A")) # [1] A was learned on port 1
print(sw.receive(3, "C", "A")) # [1]
print(sw.receive(1, "A", "B")) # [2] B was learned on port 2
print(sw.receive(1, "D", "A")) # [] D shares port 1 with A, filtered
print(sw.receive(4, "E", BROADCAST)) # [1, 2, 3]Learning uses only the source address. A switch can never learn a destination from a frame addressed to it. This is why the first frame to an unknown host is flooded, and why the reply lets the switch forward later traffic directly.
Why switches change the role of CSMA/CD
If every port connects to exactly one host, the link has only two transmitters, the host and the switch port. Modern switched Ethernet runs these links in full-duplex mode, with separate transmit and receive paths. Collisions cannot occur, so CSMA/CD is switched off on that link. Three consequences follow:
- Each host gets the full link rate. Ten hosts on a 100 Mbps switch each have a dedicated 100 Mbps link, whereas on a hub they would share 100 Mbps.
- Aggregate capacity can exceed the link rate, because frames between different pairs of ports proceed at the same time.
- The minimum frame size no longer depends on the cable length. The 64-byte minimum stays in the standard for compatibility with half-duplex links and hubs.
A port connected to a hub is still a shared, half-duplex segment. CSMA/CD runs there, and that whole segment, including the switch port, is one collision domain.
Store-and-forward and cut-through
A store-and-forward switch receives the whole frame and checks the CRC before sending it on. It never propagates corrupted frames, and it can connect links of different speeds. It adds a delay of one frame transmission time per hop. A cut-through switch begins sending once it has read the destination address, which is the first 6 bytes after the preamble. Its delay is smaller, but it may forward a corrupted frame, because the CRC at the end is not yet known.
Common errors and edge cases
- Counting broadcast domains by switches. Connecting switches to each other does not create new broadcast domains. Only routers (or VLAN boundaries) do.
- Learning from the destination. The table is built from source addresses only.
- Forgetting the filter case. A frame whose destination is on the arrival port is dropped, not sent back.
- Loops. If switches form a cycle, a flooded frame circulates forever and the table entries flip between ports. The Spanning Tree Protocol blocks some ports so that the active topology is a tree.
- Table overflow. A switch has limited table space. If an attacker fills it with fake source addresses, legitimate destinations are dropped from the table and their traffic is flooded.
- GATE counting convention. Questions usually count one collision domain per switch port, and one domain for a hub with every port attached to it. State this assumption in your working.
Exercise
A switch has 5 ports. Four ports each connect directly to one host. The fifth port connects to a hub that has 3 hosts attached. The switch is not connected to a router. How many collision domains and broadcast domains are there?
Show answer
There are 5 collision domains: one for each of the four single-host links, and one for the hub segment together with the fifth switch port. The hub and its 3 hosts share a single domain, and the CSMA/CD rule applies only there. There is 1 broadcast domain, because the switch and hub forward broadcasts everywhere and no router separates them.
Exercise
A new switch has an empty table. Host A on port 1 sends a frame to host B on port 2. Then B replies to A. Which ports does each frame leave through?
Show answer
The first frame is flooded to every port except port 1, because B is unknown, and the switch learns A on port 1. The reply from B arrives on port 2, and the switch learns B on port 2. It looks up A and sends the reply only out of port 1.