lesson 2.1 · chapter 2 of 5: connecting hosts · 25 min
Hubs, switches and routers
objective
Watch the same messages go through a hub, a switch and a router, and say what each one reads (nothing, MAC addresses, IP addresses) and where it sends things.
Three ways to pass messages on
The apartment building’s mail room
Two computers can share one cable. But plug in three, or thirty, and you need a box in the middle that everyone’s cable plugs into, to pass messages between them.
Think of a big apartment building, and three different ways the post could get from flat to flat:
- The loudspeaker. Whoever has a letter reads it out over a loudspeaker in every flat. Everyone hears everything, and only one person can use the loudspeaker at a time.
- The mail room clerk. A clerk who knows which flat everyone lives in takes each letter straight to the right door.
- The post office. For a letter to another town, the clerk hands it to the post office, which knows the way to every town.
Those are the three boxes in this lesson: the hub, the switch and the router.
in one sentence A hub shouts everything to everyone, a switch delivers to the right door, and a router sends things to other networks.
Ports
A port on one of these boxes is a socket where a cable plugs in, like the numbered mail slots in a mail room. Ports are numbered: port 1, port 2, and so on. (This is a different thing from the port numbers in lesson 1.2: same word, different meaning. Networking does that sometimes, sorry.)
in one sentence A port is a numbered socket on a box, where a cable plugs in.
The hub: the loudspeaker
A hub doesn’t read anything. Whatever signal comes in on one port, it repeats straight out of every other port. That makes it a layer 1 box: it only deals with the signal itself.
Because everyone shares, only one host can talk at a time. If two talk at once, their signals crash into each other and both are ruined. That’s called a collision, and both have to try again.
Hubs are very old: you’d only find one in a museum now. But they’re the easiest way to see why switches were invented.
in one sentence A hub repeats every message to everyone, so only one host can talk at a time.
The switch: the mail room clerk
A switch reads the MAC address on every frame (remember from lesson 1.2: a frame is a message in its layer 2 envelope, with a to and from MAC address on it). That makes it a layer 2 box.
It keeps a list called the MAC address table: which MAC address lives on which port. Nobody types it in; the switch fills it in by itself. Every frame says who it’s from, so when one arrives, the switch notes that MAC address is on this port.
Then it sends each frame only out of the port where the to address lives. Sending a message on like this is called forwarding it. If the switch doesn’t know where the to address is yet, it sends a copy out of every port, just this once. That’s called flooding.
in one sentence A switch learns which MAC address is on which port, and sends each frame only where it needs to go.
The router: the post office
A switch joins hosts on one network. To reach a host on a different network, like your home reaching a website, you need a router.
A router reads a different address: the IP address. You’ll learn all about IP addresses in lesson 3.1; for now, think of it as a postal address with a town in it. The first part of an IP address says which network (which town) a host is in. That makes the router a layer 3 box.
A router has a foot in each network it joins, and keeps a routing table: a list of networks and which way to send messages for each one, like the post office knowing which van goes to which town.
in one sentence A router joins different networks, and reads the IP address to pick the way to the right one.
To keep the pictures readable, the MAC addresses below are shortened to one letter: PC A’s MAC is just A.
(A PC, personal computer, is any laptop or desktop computer.)
The hub: everyone hears everything
Four PCs on a hub. Watch what happens when A sends a frame to C, and then when two PCs talk at once.
in one sentence A hub sends every frame to everyone, and when two hosts talk at once their frames collide.
The switch: it learns
Same four PCs, but now with a switch. Watch the MAC address table under the picture fill in.
What to notice:
- The switch learns from the source MAC and decides from the destination MAC.
- An unknown destination gets flooded once. After the host answers, never again.
- Lots of conversations can happen at once, and they never collide.
The router: joining two networks
Switches are for hosts on the same network. To reach a host on a different network you need a router. Here are two networks, each with its own switch, and a router joining them. The router has an IP address on each side.
One new word: every host knows the router as its default gateway. A gateway is a way out, like the front door of a building: anything that isn’t for someone on my network, send it to the gateway.
The big idea: MAC addresses get you to the next box; IP addresses get you all the way there. A switch reads the MAC. A router reads the IP, and writes a new frame for the next network.
in one sentence A switch learns where everyone is and delivers on the same network; a router reads the IP address and passes messages to a different network.
Be the switch
A switch has five ports. Its MAC address table looks like this:
| port | MAC |
|---|---|
| 1 | A |
| 2 | B |
PC C is on port 3, PC D on port 4 and PC E on port 5, but they haven’t sent anything yet. These frames arrive, in this order. For each one, which ports does the switch send it out of, and what does it add to its table?
- From C to A
- From A to D
- From D to A
- From E to everyone (
FF:FF:FF:FF:FF:FF)
hint
Do two things for every frame, in this order: learn (write down the source MAC and the port it came in on), then forward (look up the destination).
hint
If the destination is in the table, send it to that one port. If it isn’t, or it’s a broadcast, flood it out of every port except the one it came in on.
show the solution
| Frame | Learns | Sends it out of | Why |
|---|---|---|---|
| C → A | C is on port 3 | port 1 | A is in the table |
| A → D | (already knows A) | ports 2, 3, 4, 5 | D isn’t in the table yet: flood |
| D → A | D is on port 4 | port 1 | A is in the table |
| E → everyone | E is on port 5 | ports 1, 2, 3, 4 | broadcasts always flood |
The table at the end:
| port | MAC |
|---|---|
| 1 | A |
| 2 | B |
| 3 | C |
| 4 | D |
| 5 | E |
5 check: test yourself
Pick an answer for each question. You'll see straight away whether it's right, and why. Questions marked what happens next? are about the animations.
A frame arrives at a hub. Where does it go?
A hub is the loudspeaker: it doesn’t read anything, it repeats every signal to everyone.
what happens next?A switch has just been switched on, so its MAC address table is empty. A frame arrives from A, going to C. What does the switch do?
It always learns from the from address first. It doesn’t know where C is yet, so this once it sends a copy everywhere.
what happens next?C answers A. The switch already knows A is on port 1. What happens next?
Now it knows where A lives, so the answer goes only to A’s port. And it learns C’s port from the from address.
What does a router read to decide where to send a message?
Routers are the post office: they read the IP address (the postal address) to pick which network to send it to.
A message crosses a router. What changes?
The IP addresses stay the same the whole way. Each network gets a brand-new frame with the MAC addresses for that stop.