A router may know several routes to one destination: a static one, one from OSPF, a default route. It chooses for every packet like this:
Longest prefix (longest match): the most specific matching route wins, no matter where it came from. A /24 route beats a /16 route and the default route /0 (RFC 1812, Section 5.2.4.3).
Administrative distance (AD): only for prefixes of equal length. It says how much the router trusts a source. Smaller is better.
Metric: only for the same source. It says how good the path is. Smaller is better.
The distance is not a standard but a vendor convention. leyoNET uses the common values:
Source
Distance
Code in show ip route
directly connected
0
C
static
1
S
eBGP
20
B
OSPF
110
O
RIP
120
R
iBGP
200
B
unusable
255
(not installed)
A static route with a higher distance (e.g. 200) becomes a backup route (floating static route): it only takes effect when the better route disappears. See Static routing.
RIP only counts routers. A path over two fast fibre links is worse for RIP than one over a single slow link. That is why RIP is considered simple but imprecise.
OSPF calculates with costs per interface. Many vendors derive them from the bandwidth (reference bandwidth divided by bandwidth, e.g. 100 Mbit/s ÷ 10 Mbit/s = 10). In leyoNET every interface has a cost of 10; ip ospf cost <1–65535> on the interface lets you prefer or avoid a path.
BGP does not choose by speed but by rules (policy) between organisations. With equal rules the shorter AS path wins.
Example: reading show ip route
Router# show ip route
S>* 0.0.0.0/0 [1/0] via 203.0.113.1, eth2
O>* 10.1.0.0/24 [110/20] via 10.0.12.2, eth1
C>* 10.0.12.0/30 is directly connected, eth1
R>* 172.16.5.0/24 [120/2] via 10.0.13.3, eth0
The square brackets contain [distance/metric]. [110/20] means: learned from OSPF, cost 20 (two interfaces of 10 each). >* marks the selected, active route.
A packet to 10.1.0.7 matches 10.1.0.0/24 and 0.0.0.0/0. The /24 is more specific, so the OSPF route wins, even though the static default route has the smaller distance.
Convergence
When a link fails, all routers must find a new best route. The time this takes is called convergence. OSPF detects the failure through missing hellos (dead interval) and recalculates at once. RIP waits for updates and in the worst case may count up to 16 (count to infinity, see RIP and RIPng). Static routes do not converge at all: only a backup route steps in, and only when the router's own interface fails.
Try it in leyoNET
Routing table: select a router, Routing on the right, or show ip route on the command line (IPv6: show ipv6 route).
Build two paths with OSPF (exercise “Routing with OSPF”), then set ip ospf cost 50 on one interface and watch the traffic take the other path (tracert).
Backup route: enter the default route twice, the second with distance 200, then unplug the cable of the first.
How to do it in leyoNET
The same text as in the app’s help (F1 key).
Routing
The routing table says via which neighbour a network is reachable. Entries come from:
connected networks
automatic routing for beginners: the router calculates its routes by itself (Automatic routing)
The choice follows RFC 1812: the most specific route (longest prefix); on a tie the lower administrative distance (connected 0, static 1, eBGP 20, OSPF 110, RIP 120, iBGP 200), then the lower metric (RIP: hops, OSPF: cost). The table shows both as [distance/metric].
Backup route: A static route with a larger distance (e.g. 200) is only used when the better one disappears, for instance because its interface has no link (floating static route). Distance 255 = never use.
Equal-cost paths (ECMP)
If two or more paths with equal cost lead to the same destination, routers use all of them (equal-cost multi-path, up to 4 paths). This spreads the load and adds a backup.
Static: two routes to the same network with the same distance but a different gateway (ip route 10.4.0.0/24 10.0.12.2 and ip route 10.4.0.0/24 10.0.13.3). The same route twice is not allowed; with another distance it becomes a backup route.
OSPF: paths with equal cost are all installed (RFC 2328).
Traffic is spread per flow, not per packet: a hash over source, destination, protocol and ports picks the path (hash threshold, RFC 2992). This keeps the packets of a connection in order; pings from different computers may take different paths.
show ip route marks every active path with >*. If a path fails, the flows use the remaining ones.
See an example
Static routes
Two routers, each knows the remote network through a static route.
«tracert» shows both routers as hops. Delete a route and try again: destination network unreachable.
Router 1 reaches the server network via Router 2 or Router 3, both paths cost the same. OSPF installs both, and the router spreads the flows by a hash (RFC 2992).
Routing table of Router 1: two O routes to 10.4.0.0/24. The traceroutes from PC 1 and PC 2 may take different paths. Remove a cable to Router 2: everything uses Router 3.
RFC 2992 – Analysis of an Equal-Cost Multi-Path Algorithm
Read on
How routing worksDefault gateway, routing table, longest prefix and administrative distance – and when to route statically, automatically or dynamically.
Static routingEntering routes by hand: destination network, mask and next hop, default route, outbound and return path and the most common mistakes.
OSPFLink-state routing: neighbours via hello, a database of all links, shortest paths by Dijkstra – for IPv4 (OSPFv2) and IPv6 (OSPFv3).
RIP and RIPngDistance vector routing: routers tell their neighbours which networks they reach in how many hops – simple, but with limits.
ECMP (equal-cost paths)If several paths with the same cost lead to the destination, the router uses all of them. It does not spread each packet but each flow.
BGPRouting between providers and organisations: autonomous systems, neighbours over TCP 179, announced networks and the AS path.