What routing is
Routing is moving a packet across several networks toward its destination. No single router knows the whole path. Each one makes one decision: given this destination IP, what is the best next hop? Then it forgets about the packet.
The path we follow
The front desk PC in the Kadé office loads an order from kade-api. The office uses an ISP called LankaNet (made up for this course). LankaNet connects straight to Google in Singapore, and also buys general internet access from a transit provider, a bigger network that carries traffic to everywhere else.
Each link between two routers is its own tiny network (a /30 has room for exactly 2 devices). Router interfaces are named like ge0, ge1 (gigabit Ethernet). Addresses are documentation or private ranges; the AS numbers are explained in section 10.
Wide by design - scroll it sideways.
The routing table
Every router keeps a routing table: a list of destination networks mapped to a next hop and an outgoing interface. You met small ones in chapters 5 and 6. Here is R1's:
| Destination | Next hop | Interface | Source of the route |
|---|---|---|---|
| 172.16.1.0/24 | direct | eth1 | connected (eth1 is on this network) |
| 172.16.2.0/24 | direct | eth2 | connected |
| 198.51.100.0/24 | direct | eth0 | connected |
| 0.0.0.0/0 | 198.51.100.1 | eth0 | static (typed in by whoever set up the router) |
- Destination: a network in CIDR form (chapter 3).
- Next hop: the IP of the next router, or "direct" when the destination is on a network this router is connected to.
- Interface: which port to send it out of.
For every packet, the router: (1) reads the destination IP, (2) finds every row it matches, (3) picks the most specific one (section 7), (4) runs ARP for the next hop (or for the target itself if "direct"), (5) builds a new frame and sends it out that interface.
Hop by hop
Step through the trip. At each stop you see that router's table (the row it chose is marked), its decision, and the packet as it leaves:
1 · Front desk PC · looks up 34.87.120.15
| Destination | Next hop | Iface | Note |
|---|---|---|---|
| 172.16.1.0/24 | direct | en0 | connected |
| 0.0.0.0/0 | 172.16.1.1 | en0 | default gateway ← chosen |
34.87.120.15 is not in 172.16.1.0/24. Only the default route matches, so send to 172.16.1.1. ARP gives R1 eth1's MAC.
- frame · source MAC
- 3c:7c:3f:10:22:01 (PC)
- frame · destination MAC
- e4:8d:8c:00:01:01 (R1 eth1)
- IP · source (with port)
- 172.16.1.10:52814
- IP · destination (with port)
- 34.87.120.15:443
- IP · TTL
- 64
What changes, and what does not
| # | Field | PC → R1 | R1 → R2 | R2 → R3 | R3 → R4 | R4 → kade-api |
|---|---|---|---|---|---|---|
| 1 | Destination IP | 34.87.120.15 | same | same | same | 10.10.1.10 GCP's 1:1 NAT |
| 2 | Source IP | 172.16.1.10 | 198.51.100.20 R1's NAT | same | same | same |
| 3 | TTL | 64 | 63 | 62 | 61 | 60 |
| 4 | IP header checksum | - | recalculated | recalculated | recalculated | recalculated |
| 5 | MAC addresses | PC → R1 | R1 → R2 | R2 → R3 | R3 → R4 | R4 side → kade-api |
| 6 | TCP ports, HTTP data | 52814 → 443 | 40001 → 443 R1's NAT | same | same | same |
TTL: the loop safety valve
Routers make mistakes. Say LankaNet mistypes a route: R2 sends 203.0.113.99 to R3, and R3 sends it back to R2. Without a limit, a packet for that address would bounce forever, and millions of them would fill the links.
Wide by design - scroll it sideways.
So the IP header carries a TTL (Time To Live), a number that each router lowers by one. At 0 the router drops the packet and tells the sender with an ICMP message. Common starting values: 64 (Linux, macOS), 128 (Windows), 255 (many routers). Chapter 13 turns this safety valve into a tool: traceroute.
Longest prefix match
A destination can match several rows. R3, LankaNet's border router, has these routes (among others):
| Destination | Next hop | Interface | Learned from |
|---|---|---|---|
| 0.0.0.0/0 | 192.0.2.6 | ge2 | transit provider: "send me anything" |
| 34.0.0.0/8 | 192.0.2.6 | ge2 | transit provider: "I can reach 34.x" |
| 34.87.0.0/16 | 192.0.2.2 | ge1 | Google directly: "34.87.x is mine" |
34.87.120.15 matches all three. The router always picks the most specific match: the one with the longest prefix, meaning the most network bits that agree. Bit by bit:
34.87.120.1534.87.120.15
Only the first /N bits of each route are compared (the rest are shown as dots). Green = same as the destination, highlighted = different. /0 compares nothing, so it always matches. Among the matches, the one comparing the most bits wins.
The /16 wins, so the packet goes straight to Google over the direct link, not the long way through the transit provider. This is good for LankaNet too: direct links with Google are usually cheaper and faster than paid transit.
Try other destinations against R3's full table:
| R3’s table | Next hop | Iface | Note |
|---|---|---|---|
| 10.200.0.0/30 | direct | ge0 | connected |
| 198.51.100.0/24 | 10.200.0.1 | ge0 | OSPF: customers behind R2 |
| 192.0.2.0/30 | direct | ge1 | connected: link to Google |
| 192.0.2.4/30 | direct | ge2 | connected: link to transit |
| 34.87.0.0/16 | 192.0.2.2 | ge1 | BGP from Google ← chosen |
| 34.0.0.0/8 | 192.0.2.6 | ge2 | BGP from transit · matches |
| 0.0.0.0/0 | 192.0.2.6 | ge2 | BGP from transit · matches |
34.87.120.15 → 34.87.0.0/16 (BGP from Google) → next hop 192.0.2.2 out ge1
When two routes have the same length
Longest prefix match comes first. Only if two matching routes have the same prefix length does the router need a tie-breaker:
| 1 · Trust in the source | Routes learned in different ways get a trust score (Cisco calls it administrative distance; lower wins). Typical values: connected 0, static 1, BGP from another network 20, OSPF 110, RIP 120. |
| 2 · Metric | Among routes from the same protocol, the one with the lower cost wins (fewer hops for RIP, faster links for OSPF). |
| 3 · Still equal: ECMP | Equal-Cost Multi-Path: the router uses all of them and spreads traffic. It keeps each connection on one path, so packets are not reordered. |
The default route
What if nothing matches? Routers use a default route, 0.0.0.0/0: "when in doubt, send it here". It matches every address with 0 bits, so it always loses to anything more specific. It is the last resort, not the first choice.
| Front desk PC | 0.0.0.0/0 → 172.16.1.1 its default gateway (chapter 5) |
| R1 office | 0.0.0.0/0 → 198.51.100.1 LankaNet |
| R2 ISP edge | 0.0.0.0/0 → 10.200.0.2 R3, deeper in LankaNet |
| R3 ISP border | 0.0.0.0/0 → 192.0.2.6 the transit provider |
| Big backbone routers | no default at all they know a route to every network: the "default-free zone" |
Static and dynamic routing
Routing tables are filled in two ways:
Static routes
Typed in by hand. Simple and predictable. Fine for small setups like R1's one default route.
Problem: if a link fails, nothing updates itself.
Dynamic routes
Routers tell each other which networks they can reach, using a routing protocol, and update their tables when links fail or return.
| # | Protocol | Used | How it chooses | In our path |
|---|---|---|---|---|
| 1 | RIP | Small, older networks | Fewest hops (max 15) | Not used |
| 2 | OSPF | Inside one organisation | Every router learns the full map of links and computes the fastest path | R2 and R3 inside LankaNet |
| 3 | BGP | Between organisations: the whole internet | Policies and paths between networks, not just speed | R3 with Google and the transit provider |
The internet is a network of networks. Each organisation that runs its own routing is an autonomous system (AS) with a number: Google is AS 15169. BGP is how they tell each other "these address ranges are reachable through me":
Green: announcements that build R3's table from section 7. Amber: LankaNet's own announcement, which is how Google knows where to send kade-api's replies to 198.51.100.20. Traffic flows the opposite way to announcements. AS 64500 and 64510 are numbers reserved for examples.
Wide by design - scroll it sideways.
Routing in GCP
A VPC has a routing table too, managed by GCP. For kade-vpc today, plus the office VPN that is coming later:
| # | Destination | Next hop | Type | Priority |
|---|---|---|---|---|
| 1 | 10.10.1.0/24 | the VPC network | subnet route (automatic) | - |
| 2 | 10.10.2.0/24 | the VPC network | subnet route (automatic) | - |
| 3 | 0.0.0.0/0 | default-internet-gateway | static (created with the VPC) | 1000 |
| 4 | 172.16.0.0/16 later | VPN tunnel to the office | dynamic: learned by Cloud Router over BGP | set by BGP |
Other routing ideas you will hear
| Asymmetric routing | The reply may take a different path from the request. Each router decides on its own, and kade-api's reply is routed by Google's and LankaNet's tables, not by the original path. Keep this in mind when reading traceroute (chapter 13). |
| Route summarization | Announcing one bigger range instead of many small ones. The office could announce 172.16.0.0/16 instead of 172.16.1.0/24 and 172.16.2.0/24. Smaller tables, fewer updates. |
| Blackhole (null) route | A route whose next hop is "drop it". Used to throw away attack traffic, or to stop a summary route from looping. |
| Routing table vs forwarding table | Protocols build the routing table (all known routes). The router copies only the winners into a fast forwarding table used for each packet. |
The full IPv4 header
This chapter used many IP header fields one at a time: TTL, protocol, the checksum. Here is the whole header, laid out like UDP's (chapter 15) and TCP's (chapter 18). It is 20 bytes without options, which is what almost every packet uses:
Five rows = 20 bytes, like TCP’s fixed part. Everything a router needs is here; the data behind it is someone else’s business (chapter 11).
And the real bytes: the IP header around the SYN from chapter 18, as it leaves the laptop toward kade-api:
Byte 0 = 45: version 4 in the top half, IHL 5 in the bottom half. Bytes 6-7 = 40 00: the DF bit is on, offset 0. The checksum a2 7c was computed for real; re-adding all ten 16-bit words gives 0xFFFF.
| # | Field | Size | In this packet | Job, and where you met it |
|---|---|---|---|---|
| 1 | Version | 4 bits | 4 | IPv4. (IPv6 packets start with 6.) |
| 2 | IHL (header length) | 4 bits | 5 → 5 × 4 = 20 bytes | Where the header ends, like TCP's data offset |
| 3 | DSCP | 6 bits | 0 | Priority class. Networks can use it to put voice calls ahead of backups (QoS). |
| 4 | ECN | 2 bits | 0 | Congestion marks (chapter 20). 0 = not ECN-capable. |
| 5 | Total length | 16 bits | 00 3c = 60 | Header + everything inside: 20 IP + 40 TCP |
| 6 | Identification | 16 bits | 3c 1a | Matches fragments of one packet (chapter 18) |
| 7 | Flags | 3 bits | 010 = DF | DF and MF (chapter 18). TCP always sets DF. |
| 8 | Fragment offset | 13 bits | 0 | Where a fragment's data starts, in 8-byte units |
| 9 | TTL | 8 bits | 40 = 64 | Lowered at every router (section 6) |
| 10 | Protocol | 8 bits | 06 = TCP | What is inside: 1 ICMP, 6 TCP, 17 UDP (chapter 13) |
| 11 | Header checksum | 16 bits | a2 7c | Covers the IP header only. After R1 lowers TTL to 63 and NATs the source, it becomes 3a f4. |
| 12 | Source address | 32 bits | c0 a8 01 17 | 192.168.1.23 (chapter 2) |
| 13 | Destination address | 32 bits | 22 57 78 0f | 34.87.120.15, the only field routers look up |
| 14 | Options | 0-40 bytes | none | Rarely used; many routers treat packets with options as suspicious |
Where latency comes from
Three words get mixed up all the time:
| Word | Meaning | Home → kade-api |
|---|---|---|
| Latency | How long one piece of data takes to get there. Usually quoted as round-trip time (RTT). | 41 ms RTT |
| Bandwidth | The most a link can carry per second. A property of the link. | 100 Mbit/s (your plan) |
| Throughput | What you actually get per second, after windows, loss and sharing (chapters 19-20). | anything up to 100 Mbit/s |
Every packet's delay is the sum of four parts, at every hop:
| # | Delay | Caused by | Size on our path | Can you reduce it? |
|---|---|---|---|---|
| 1 | Propagation | Distance. Light in fibre covers about 200 km per millisecond. | ~35 ms of the 41 (Colombo ⇄ Singapore) | Only by moving closer: a nearer region, a CDN, anycast (section 15) |
| 2 | Transmission | Pushing the bits onto the link: packet size ÷ link speed | 1,500 bytes at 100 Mbit/s = 0.12 ms | Faster links; matters on slow links |
| 3 | Queuing | Waiting behind other packets in router queues | 0 to hundreds of ms (bufferbloat, chapter 20) | Better queues, less congestion |
| 4 | Processing | Routers reading headers; the server doing its work | microseconds per router; kade-api's own time on top | Faster code (for the server part) |
the 41 ms ping from home to kade-api, roughly split
Try the physics yourself:
- One-way, light in fibre
- 3300 ÷ 200 km/ms = 16.5 ms
- Best possible RTT
- 33.0 ms before any queues or processing
- Transmission of one packet
- 1,500 × 8 bits ÷ 100 Mbit/s = 0.12 ms per link
Cable routes are longer than straight lines: Colombo to Singapore is about 2,700 km direct but more by cable, which fits the ~35 ms part of the 41 ms ping.
Anycast: one IP, many places
Normally one IP address belongs to one machine in one place: unicast. With anycast, the same address range is announced over BGP (section 10) from many locations at once. Every router simply follows its best route, so each user reaches the nearest location without knowing others exist.
| # | Kind | One sender reaches | Example |
|---|---|---|---|
| 1 | Unicast | one specific host | kade-api today at 34.87.120.15 |
| 2 | Broadcast | every host on the local network | ARP requests, DHCP Discover |
| 3 | Multicast | every host that joined a group | Live video inside a company network |
| 4 | Anycast | the nearest of many hosts sharing one IP | 1.1.1.1, 8.8.8.8, Cloudflare, Google's global load balancers |
Later in the course, api.kade.lk moves to a GCP global external load balancer, which gets one anycast IP (34.120.7.9 here is an example). TCP and TLS then end at the nearest Google edge, so the handshakes (chapter 16) happen over a short distance, and the long part of the trip runs over Google's own network.
Wide by design - scroll it sideways.
- Failover for free: if one location stops announcing the route, BGP sends users to the next-nearest one.
- Spreads attacks: a DDoS aimed at one IP lands on many locations at once.
- Works for TCP because internet routes rarely change in the middle of a connection. It was first used for DNS (short UDP exchanges), and still is.
Summary
| # | Idea | In one line | New terms |
|---|---|---|---|
| 1 | Routing | One decision per router: best next hop for this destination IP | routing, next hop |
| 3 | Table | Destination (CIDR) → next hop + interface | connected, static route |
| 5 | Changes | IP stays (except NAT); MAC, TTL, checksum change every hop | |
| 6 | TTL | Lowered by 1 per router; at 0 dropped + ICMP message | TTL, routing loop |
| 7 | Longest prefix | Most specific match wins | longest prefix match |
| 8 | Ties | Trust (admin distance), then metric, then ECMP | administrative distance, metric, ECMP |
| 9 | Default | 0.0.0.0/0, the last resort | default-free zone |
| 10 | Filling tables | Static by hand; dynamic via RIP, OSPF, BGP | OSPF, BGP, AS, transit, peering |
| 11 | GCP | Subnet, static and dynamic routes; priority; Cloud Router for BGP | Cloud Router |
| 13 | IPv4 header | 20 bytes: version, length, ID/flags/offset, TTL, protocol, checksum, addresses | IHL, DSCP |
| 14 | Latency | Propagation + transmission + queuing + processing; distance dominates | latency, bandwidth, throughput |
| 15 | Anycast | One IP announced from many places; BGP picks the nearest | unicast, anycast |
Try it yourself
Your routing table
- What to look for: Connected route for your LAN, and the default route.
- Runs on: Linux · macOS: netstat -rn -f inet · Windows: route print -4
- From section: 3. The routing table
ip routeWhich row wins for one destination
- What to look for: The chosen next hop and interface.
- Runs on: Linux · macOS: route -n get 34.87.120.15
- From section: 7. Longest prefix match
ip route get 34.87.120.15The reply's TTL
- What to look for:
ttl=57means the reply started at 64 and passed about 7 routers. - From section: 6. TTL: the loop safety valve
ping -c 1 1.1.1.1Who announces an IP range
- What to look for: The
origin:line shows the AS number (AS15169 for Google). - From section: 10. Static and dynamic routing
whois -h whois.radb.net 8.8.8.8A VPC's routes
- What to look for: Destination range, next hop, priority.
- From section: 11. Routing in GCP
gcloud compute routes list --filter="network:kade-vpc"One packet's IP header decoded
- What to look for:
tos 0x0, ttl 64, id …, flags [DF], proto TCP (6), length … - From section: 13. The full IPv4 header
sudo tcpdump -n -v -c 1 -i en0 tcpAnycast in action
- Steps:
traceroute 1.1.1.1, and ask a friend abroad to run it too - What to look for: Same destination, different last hops and much shorter RTTs than the distance to any one "home" of 1.1.1.1.
- From section: 15. Anycast: one IP, many places
traceroute 1.1.1.1Routes a Cloud Router learned over BGP
- What to look for: The
bestRouteslist, once the VPN exists. - From section: 11. Routing in GCP
gcloud compute routers get-status ROUTER --region=REGION