Wiki DE Start in the browser

Wiki › Basics

Route selection: prefix, distance and metric

Which route wins? Longest prefix, administrative distance and metric in the right order, with the metrics of RIP, OSPF and BGP.

Layer
3 (Vermittlung)
Standard
RFC 1812, RFC 2453, RFC 2328 …
See an example
4 demos

Three questions, always in the same order

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:

  1. 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).
  2. Administrative distance (AD): only for prefixes of equal length. It says how much the router trusts a source. Smaller is better.
  3. Metric: only for the same source. It says how good the path is. Smaller is better.

If several equally good routes remain, the router spreads the traffic over all of them (ECMP (equal-cost paths), RFC 2992).

Administrative distance

The distance is not a standard but a vendor convention. leyoNET uses the common values:

SourceDistanceCode in show ip route
directly connected0C
static1S
eBGP20B
OSPF110O
RIP120R
iBGP200B
unusable255(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.

Metric per protocol

ProtocolMetricMeaningStandard
static0no rating, the path is givenRFC 1812
RIP, RIPnghopsnumber of routers to the destination, 16 = unreachableRFC 2453, Section 3.4
OSPFcostsum of the costs of all outgoing interfaces along the pathRFC 2328, Section 16
BGPpath attributeslocal preference, AS path length, MED and more, in orderRFC 4271, Section 9.1

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)
  • static routes, entered by hand
  • dynamic protocols: OSPF, RIP, BGP

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

  • Demo “Static routes” in leyoNETDemo “Static routes” in leyoNET
    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.

    Watch live Project file

  • Demo “OSPF” in leyoNETDemo “OSPF” in leyoNET
    Slow motion This is how it looks in the simulation: every dot is a frame on the cable.
    OSPF

    Three routers in a triangle learn their networks via OSPF.

    Hello, DBD, LSU, LSAck in the packet list; the routing table fills with O routes. Then remove a cable: OSPF recalculates.

    Watch live Project file

  • ECMP: two equal paths

    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.

    Watch live Project file

  • Demo “RIP” in leyoNETDemo “RIP” in leyoNET
    RIP

    The same triangle, this time with RIP v2 (distance vector).

    Regular updates via UDP 520; the metric counts hops.

    Watch live Project file

“Watch live” opens the network in leyoNET, the simulation runs in a loop. In the app you find it under File → Demo networks, with a step-by-step tour.

Practise

  • ★★☆☆☆ Two routers, two static routes · 3 goals · Worksheet (German)
  • ★★★★☆ Routing with OSPF · 3 goals · Worksheet (German)
  • ★★★★☆ Use two paths (ECMP) · 3 goals · Worksheet (German)
  • ★★★☆☆ RIP in a chain · 2 goals · Worksheet (German)

In leyoNET under Tools → Exercise catalogue. Every exercise checks itself.

Standards

  • RFC 1812 – Requirements for IP Version 4 Routers
  • RFC 2453 – RIP Version 2
  • RFC 2328 – OSPF Version 2
  • RFC 4271 – A Border Gateway Protocol 4 (BGP-4)
  • RFC 2992 – Analysis of an Equal-Cost Multi-Path Algorithm

Read on

Found a mistake or have an idea? Source of this page on GitHub.