The sorting belt that never plans
A parcel sorter reads a postcode and drops the parcel on one belt. It never plans the whole journey.
A router does the same with a packet. It reads the destination address, picks one line in its table and hands the packet on.
Every router on the way does the same job again.
By the end of this lesson
Note
I can read a routing table and pick the route a packet takes.
Key words
| Word | What it means | Example |
|---|---|---|
| Routing table | a router's list of networks and where to send packets for each | |
| Next hop | the neighbouring router that gets the packet next | via 10.0.1.1 |
| Connected route | a network the router is plugged into directly | |
| Static route | a route that a person typed in by hand | |
| Default route | the line used when no other line matches | 0.0.0.0/0 |
| Longest prefix | the matching line with the most network bits |
The cells students fill in are left blank in this preview.
A host sends direct or to its gateway
A host asks one question first. Is the destination on my own network?
If yes, it sends the packet straight there. If no, it sends it to its default gateway, a router on its own network.
The host never plans the rest of the journey.
Read a host's routes
sudo ip netns exec h1 ip routeShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Ask which way h1 sends a packet
sudo ip netns exec h1 ip route get 10.0.3.2Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Reading the answers
The first Run prints two lines. One is the network h1 is on. The other starts with default via 10.0.1.1: everything else goes to r1.
The second Run starts with 10.0.3.2 via 10.0.1.1 dev to-r1. The word via names the next hop.
A router follows its table
A router has several interfaces and a routing table. Each line says: for this network, use this way.
A router starts with only its connected routes. It learns every other network because a person adds a static route, or because a protocol shares routes.
Without a line for a network, the router cannot forward packets to it.
Worked example: a router's first table
| Network | Next hop | Kind |
|---|---|---|
| 10.0.1.0/24 | direct, on to-h1 | Connected |
| 10.0.12.0/24 | direct, on to-r2 | Connected |
| 10.0.3.0/24 | none | No line, so no way |
The cells students fill in are left blank in this preview.
Read r1's table in its console
sudo vtysh -N r1 -c "show ip route"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Ask r1 about an unknown network
sudo ip netns exec r1 ip route get 10.0.3.2Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Send a real packet from h1
sudo ip netns exec h1 ping -c 2 -W 1 10.0.3.2Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
No line means no way
The console lists four lines for r1. Two start with C for connected and two with L for the router's own addresses.
The ip route get Run prints RTNETLINK answers: Network is unreachable. The table has no line for 10.0.3.2, so r1 has nowhere to send it.
The ping shows the same gap from the host's side. Each reply reads From 10.0.1.1 and ends Destination Net Unreachable.
The longest prefix wins
Several lines can match one address. The router picks the line with the longest prefix, the one that names the network most exactly.
The default route, 0.0.0.0/0, matches everything with a prefix of zero bits. So it loses to any other match and is used last.
A common mistake
Warning
Many people think the first matching line wins. A router does not read from the top. It looks at every line and picks the longest match. The order in the table does not change the answer.
Worked example: which line wins
| Destination | Lines that match | Winner |
|---|---|---|
| 10.0.3.200 | 0.0.0.0/0, 10.0.0.0/8, 10.0.3.0/24, 10.0.3.128/25 | 10.0.3.128/25 |
| 10.0.3.5 | 0.0.0.0/0, 10.0.0.0/8, 10.0.3.0/24 | 10.0.3.0/24 |
| 10.9.9.9 | 0.0.0.0/0, 10.0.0.0/8 | 10.0.0.0/8 |
| 8.8.8.8 | 0.0.0.0/0 | 0.0.0.0/0 |
The cells students fill in are left blank in this preview.
Going further: say why a line won
Copy your pick_route into a new function, explain_route. It returns the whole winning line, not only the next hop.
Use it on the TABLE from the tests for 10.0.3.129, 10.0.3.127, 10.200.0.1 and 1.1.1.1. For each one, write a sentence naming every line that matched and why the winner beat the others.