Sub topic 3
Link Costs and Path Metrics
This chapter assumes you know basic IP addressing and subnetting, what a routing table is, and how longest-prefix match picks a route. It also assumes you know the graph terms node, edge, weight, and shortest path. The previous chapter modeled a network as a weighted graph. Here we look at where the weights come from and what "shortest" means in practice.
What a metric is
A metric is a number a routing protocol attaches to a route so it can compare candidate paths to the same destination. The protocol picks the path with the lowest total metric. Two ideas need separating:
- A link cost is the weight on one edge, usually one router interface.
- A path metric is the value for a whole path. For most protocols in this tutorial it is the sum of link costs along the path.
The rule is additive: if the path A→B costs 10 and B→C costs 5, the path A→B→C costs 15. This additive property is what makes Bellman-Ford and Dijkstra work. Both assume that extending a path never makes it cheaper, so link costs must be non-negative. Dijkstra's algorithm is incorrect with negative weights, and a negative cycle would make Bellman-Ford's answer meaningless.
Metrics used by common protocols
Each protocol chooses its own definition of cost. Costs from different protocols cannot be compared directly.
| Protocol | Link cost | Path metric | Typical range |
|---|---|---|---|
| RIP | 1 per router hop | Sum, so the hop count | 1 to 15; 16 means unreachable |
| OSPF | Based on interface bandwidth (default reference 100 Mb/s) | Sum of outgoing interface costs | 1 to 65535 per link |
| IS-IS | Default 10 on every interface, configurable | Sum | 1 to 63 (narrow), up to 16 million (wide) |
Hop count in RIP
RIP counts every link as 1. A path through three routers costs 3 whatever the link speeds. This is simple and cheap to compute, but it ignores capacity. A 10 Gb/s path with four hops loses to a 56 kb/s serial line with one hop. The small maximum of 15 also limits the network diameter, which later chapters use for loop control.
Bandwidth-based cost in OSPF
OSPF on many implementations computes:
cost = reference_bandwidth / interface_bandwidthThe result is rounded down to an integer, with a minimum of 1. With the common default reference of 100 Mb/s:
| Interface | Bandwidth | Cost |
|---|---|---|
| Ethernet | 10 Mb/s | 10 |
| Fast Ethernet | 100 Mb/s | 1 |
| Gigabit Ethernet | 1000 Mb/s | 1 (0.1 rounded up to the minimum) |
| 10 Gigabit Ethernet | 10000 Mb/s | 1 |
This is a well-known gotcha. With the default reference, Fast Ethernet, Gigabit, and 10 Gigabit links all cost 1, so OSPF sees them as equal. The fix is to raise the reference bandwidth, and to set the same value on every router in the OSPF domain. If routers disagree, they compute inconsistent costs, and traffic can take paths that the other routers do not expect.
Cost direction matters
Link cost belongs to an interface, so it is directional. The cost from A to B is configured on A's interface, and the cost from B to A is configured on B's interface. They may differ. In graph terms, the network is a directed graph, and each router's cost counts only for traffic leaving through that interface.
This means paths can be asymmetric. Traffic from A to D may go one way and the reply may return another way. Asymmetry is legal and common, but it complicates troubleshooting, because a traceroute shows only the forward path.
Equal-cost paths
When two paths to a destination have the same total metric, a router can install both and split traffic between them. This is equal-cost multipath (ECMP). Most implementations balance per flow, hashing fields such as source and destination address and ports, so packets of one flow stay in order.
The Dijkstra function in this tutorial keeps only one first hop per destination. A production implementation would store every first hop that ties for the best distance.
Worked example
Take four routers with these directed costs, the same in both directions:
- R1–R2: 10
- R2–R4: 10
- R1–R3: 1
- R3–R4: 50
Sum the paths from R1 to R4:
- R1→R2→R4: 10 + 10 = 20
- R1→R3→R4: 1 + 50 = 51
The path through R2 wins, even though the first hop through R3 is cheaper. A router must compare totals, not first links. Under RIP, both paths have 2 hops, so they tie and RIP would use both. The metric definition changes the result.
You can check totals with a small program:
costs = {
("R1", "R2"): 10,
("R2", "R4"): 10,
("R1", "R3"): 1,
("R3", "R4"): 50,
}
def path_cost(path, costs):
total = 0
for a, b in zip(path, path[1:]):
total += costs.get((a, b)) or costs[(b, a)]
return total
paths = [["R1", "R2", "R4"], ["R1", "R3", "R4"]]
for p in paths:
print(" -> ".join(p), path_cost(p, costs))
print("hop counts:", [len(p) - 1 for p in paths])Running it prints 20 and 51 for the two paths, and hop counts of 2 and 2.
Choosing and changing costs
Operators use costs as a policy tool. Raising the cost on a backup link keeps traffic off it until the primary fails. Lowering the cost on a preferred link attracts traffic. Good practice:
- Keep costs proportional to something meaningful, such as bandwidth or latency.
- Use the same scheme across the domain.
- Change costs gradually and one link at a time, because each change triggers recomputation in every router.
Metrics that track live load, such as current utilization, are tempting but risky. Traffic shifts to the "cheap" link, making it busy, so its cost rises, and traffic shifts back. The routing system oscillates. Protocols therefore prefer static or slowly changing metrics.
Common errors
- Comparing metrics across protocols. An OSPF cost of 20 and a RIP metric of 3 say nothing about each other. Routers use a separate ranking, called administrative distance on many platforms, to choose between protocols.
- Leaving the OSPF reference bandwidth at its default on a network with gigabit or faster links.
- Using different reference bandwidths on different routers.
- Assuming the return path equals the forward path.
- Assuming the fewest hops means the fastest path.
Exercise
A network runs OSPF with the default 100 Mb/s reference bandwidth. Path X crosses two Gigabit Ethernet links. Path Y crosses one Fast Ethernet link (100 Mb/s). What are the total costs, which path does OSPF prefer, and what should the operator change?
Show answer
Each Gigabit link costs 1 (100/1000 rounds up to the minimum of 1), so path X costs 2. The Fast Ethernet link costs 100/100 = 1, so path Y costs 1. OSPF prefers path Y, the slower one. The operator should raise the reference bandwidth, for example to 10000 Mb/s, on every router in the domain. Then a Gigabit link costs 10 and a Fast Ethernet link costs 100, so X costs 20 and Y costs 100, and OSPF prefers X.
Exercise
In the worked example, suppose the R3–R4 cost drops from 50 to 5. What are the new path totals from R1 to R4, and which path is chosen?
Show answer
R1→R2→R4 still costs 20. R1→R3→R4 now costs 1 + 5 = 6. The path through R3 is chosen.