Routing Protocols
Typing static routes into every router doesn't scale, and static routes can't react when a link fails. Dynamic routing protocols let routers tell each other which networks they can reach, compute the best paths automatically, and recalculate when something changes.
Two families
| Distance vector | Link state | |
|---|---|---|
| Idea | "tell your neighbours your distances to every network" | "tell everyone the state of your links, then each router computes the map" |
| Knowledge | only what neighbours say ("routing by rumour") | the full topology of the area |
| Algorithm | Bellman-Ford | Dijkstra's shortest path first (SPF) |
| Convergence | slow; can count to infinity | fast |
| CPU/memory | low | higher |
| Examples | RIP, (EIGRP is an advanced variant) | OSPF, IS-IS |
Between organisations, the Internet uses a third kind: BGP (Border Gateway Protocol), a path-vector protocol in which routes carry the list of networks (autonomous systems) they pass through, and choices are driven by business policy rather than shortest distance.
Link state: Dijkstra's algorithm
In OSPF, every router floods a description of its links (with costs, usually based on bandwidth) to all others. Each router then runs Dijkstra's algorithm on the resulting graph to find the cheapest path to every destination. Step through it on a small network of routers A–F:
The same algorithm in Python, printing the same steps and then the resulting routing table for A (the first hop toward each destination):
import heapq
links = [("A", "B", 4), ("A", "C", 1), ("C", "B", 2), ("B", "D", 5), ("C", "D", 8),
("C", "E", 10), ("D", "E", 2), ("D", "F", 6), ("E", "F", 3)]
graph = {}
for a, b, cost in links:
graph.setdefault(a, []).append((b, cost))
graph.setdefault(b, []).append((a, cost))
def dijkstra(source):
dist = {source: 0}
prev = {}
done = set()
heap = [(0, source)]
while heap:
d, node = heapq.heappop(heap)
if node in done:
continue
done.add(node)
print(f"take {node} (distance {d})")
for neighbour, cost in sorted(graph[node]):
if neighbour in done:
continue
new = d + cost
if new < dist.get(neighbour, float("inf")):
dist[neighbour] = new
prev[neighbour] = node
heapq.heappush(heap, (new, neighbour))
print(f" relax {node}->{neighbour}: {d} + {cost} = {new}")
return dist, prev
dist, prev = dijkstra("A")
print("\nRouting table of A:")
for dest in sorted(dist):
if dest == "A":
continue
hop, path = dest, [dest]
while prev[hop] != "A":
hop = prev[hop]
path.append(hop)
print(f" {dest}: cost {dist[dest]:>2}, next hop {hop}, path A-{'-'.join(reversed(path))}")take A (distance 0)
relax A->B: 0 + 4 = 4
relax A->C: 0 + 1 = 1
take C (distance 1)
relax C->B: 1 + 2 = 3
relax C->D: 1 + 8 = 9
relax C->E: 1 + 10 = 11
take B (distance 3)
relax B->D: 3 + 5 = 8
take D (distance 8)
relax D->E: 8 + 2 = 10
relax D->F: 8 + 6 = 14
take E (distance 10)
relax E->F: 10 + 3 = 13
take F (distance 13)
Routing table of A:
B: cost 3, next hop C, path A-C-B
C: cost 1, next hop C, path A-C
D: cost 8, next hop C, path A-C-B-D
E: cost 10, next hop C, path A-C-B-D-E
F: cost 13, next hop C, path A-C-B-D-E-F
The direct link A-B costs 4, but going through C costs only 1 + 2 = 3, so A sends traffic for B via C. That's the point of a routing protocol: the best path isn't always the obvious one.
Distance vector: Bellman-Ford by rumour
In a distance vector protocol each router starts knowing only its directly connected networks. Every period (30 s in RIP) it sends its whole table to its neighbours, who add the link cost and keep any route that's better than what they have. After a few rounds, everyone converges:
neighbours = {
"R1": {"R2": 1},
"R2": {"R1": 1, "R3": 1, "R4": 4},
"R3": {"R2": 1, "R4": 1},
"R4": {"R2": 4, "R3": 1},
}
# Each router knows its own LAN at distance 0.
tables = {r: {f"net-{r}": (0, "direct")} for r in neighbours}
for round_number in range(1, 5):
changed = False
snapshot = {r: dict(t) for r, t in tables.items()} # everyone sends at once
for router, links in neighbours.items():
for neighbour, cost in links.items():
for net, (dist, _) in snapshot[neighbour].items():
new = dist + cost
if new < tables[router].get(net, (99, None))[0]:
tables[router][net] = (new, neighbour)
changed = True
known = {r: len(t) for r, t in tables.items()}
print(f"round {round_number}: routes known {known}{'' if changed else ' (converged)'}")
if not changed:
break
print("R1's table:")
for net, (dist, hop) in sorted(tables["R1"].items()):
print(f" {net}: distance {dist} via {hop}")round 1: routes known {'R1': 2, 'R2': 4, 'R3': 3, 'R4': 3}
round 2: routes known {'R1': 4, 'R2': 4, 'R3': 4, 'R4': 4}
round 3: routes known {'R1': 4, 'R2': 4, 'R3': 4, 'R4': 4}
round 4: routes known {'R1': 4, 'R2': 4, 'R3': 4, 'R4': 4} (converged)
R1's table:
net-R1: distance 0 via direct
net-R2: distance 1 via R2
net-R3: distance 2 via R2
net-R4: distance 3 via R2
R1 reaches R4's network in 3 hops via R2 (R2 → R3 → R4), not via R2's direct but expensive cost-4 link.
Count to infinity
Distance vector's weakness is bad news travelling slowly. If R3's network fails, R2 may hear from R1 "I can reach it in 2" (a stale route that went through R2 itself!) and believe it. Each round the two routers bump their distances up by one, until they reach the protocol's "infinity" (16 in RIP):
INFINITY = 16
# Line topology R1 - R2 - R3, cost 1 per link. R3's LAN just went down.
r1, r2 = 2, 1 # distances to R3's LAN before the failure
r2 = INFINITY # R2 notices its link to R3 is gone
for step in range(1, 20):
r2 = min(INFINITY, r1 + 1) # R2 believes R1's old advertisement
r1 = min(INFINITY, r2 + 1) # R1 updates from R2
print(f"round {step}: R1={r1:>2} R2={r2:>2}")
if r1 == INFINITY and r2 == INFINITY:
print("both finally agree the network is unreachable")
breakround 1: R1= 4 R2= 3
round 2: R1= 6 R2= 5
round 3: R1= 8 R2= 7
round 4: R1=10 R2= 9
round 5: R1=12 R2=11
round 6: R1=14 R2=13
round 7: R1=16 R2=15
round 8: R1=16 R2=16
both finally agree the network is unreachable
Fixes include split horizon (never advertise a route back to the neighbour you learned it from), poison reverse and hold-down timers. Link-state protocols avoid the problem entirely because every router has the full map.
Real protocols at a glance
| Protocol | Type | Metric | Where |
|---|---|---|---|
| RIPv2 | distance vector | hop count (max 15) | small, legacy networks |
| OSPFv2/v3 | link state | cost = reference bandwidth / link bandwidth | enterprise networks |
| IS-IS | link state | configurable cost | ISP backbones |
| EIGRP | advanced distance vector | bandwidth + delay | Cisco networks |
| BGP-4 | path vector | policy, AS path length | between ISPs, the Internet itself |
router# show ip route ospf
10.0.0.0/8 is variably subnetted, 6 subnets, 2 masks
O 10.1.20.0/24 [110/3] via 10.0.12.2, 00:14:07, GigabitEthernet0/1
O 10.1.30.0/24 [110/4] via 10.0.12.2, 00:14:07, GigabitEthernet0/1In [110/3], 110 is the administrative distance of OSPF and 3 is the path cost.
Test yourself
Which algorithm do link-state protocols such as OSPF use to compute routes?
- Bellman-Ford
- Dijkstra's shortest path first
- Spanning tree
- Round robin
In the lesson's network, why does A route to B via C?
- A-B is down
- A-C-B costs 3, less than the direct link's 4
- C is the default gateway
- Dijkstra always prefers two hops
What does split horizon prevent?
- ARP spoofing
- A router advertising a route back to the neighbour it learned it from
- Broadcast storms
- Fragmentation