What the OSPF cost is
OSPF picks the path whose total cost to the destination is lowest. That cost is the OSPF metric.
RFC 2328 Section 2.1 defines the cost as a value attached to the output side of each router interface. The administrator configures it, and the lower it is the more likely that interface is used to forward traffic. Section 9 states that an interface output cost must be greater than zero.
Two things follow from that.
- A cost is one-directional. There is no single “link cost” shared by the two routers on a link. R1 has its own cost on its outgoing side and R2 has its own. Change one side and the forward and return paths can differ
- The minimum is 1. Zero is not allowed
The configurable maximum is 65535, because the field that carries a per-link cost in the Router-LSA is 16 bits wide (RFC 2328 Appendix A.4.2).
The RFC does not say how to arrive at the cost. Section 1.2 calls it a single dimensionless metric and says nothing about bandwidth. Deriving it from bandwidth is a vendor implementation.
How a cost is decided
In the Cisco implementation the cost of one interface is decided in this order.
If the interface has an explicit cost, that value is used as-is. Otherwise the cost is the reference bandwidth divided by the interface bandwidth. Both are in Mbps, and anything below 1 is rounded up to 1.
| Setting | Where it goes | Unit |
|---|---|---|
auto-cost reference-bandwidth <value> | under router ospf <process> | Mbps |
cost <value> | under area <ID> → interface <IF> | the cost itself (1 to 65535) |
bandwidth <value> | under the interface, outside OSPF | kbps |
The default reference bandwidth is 100 Mbps, which is far too low for current link speeds. 100 divided by 1000 is 0.1 and 100 divided by 10000 is 0.01; both round up to cost 1. A 1 Gbps link, a 10 Gbps link and a 100 Gbps link are indistinguishable. To tell them apart, raise the reference bandwidth.
Give every router in the area the same reference bandwidth. Miss one router and its links become comparatively cheap, and traffic ends up on paths nobody intended.
Test setup
A diamond: two paths from R1 to R3, one through R2 and one through R4. Every link is 1 Gbps and every link is in area 0. The observation target is fixed as 3.3.3.3/32 as seen from R1.
| Item | Value |
|---|---|
| Path A | R1 - R2 - R3 |
| Path B | R1 - R4 - R3 |
| Router IDs | 1.1.1.1 on R1, 2.2.2.2 on R2, 3.3.3.3 on R3, 4.4.4.4 on R4 |
| Observation target | 3.3.3.3/32 as seen from R1 |
| Software | XRd 26.1.1 |
The test runs in seven STEPs.
| STEP | Change | What to look for |
|---|---|---|
| 0 | Defaults | 1 Gbps links and Loopbacks all have cost 1. The two paths are equal cost |
| 1 | Lower bandwidth on one interface | The cost follows the interface bandwidth. The longer way round can be cheaper |
| 2 | Set the reference bandwidth to 100 Gbps on all four | Every cost changes at once. Loopbacks do not |
| 3 | Return R4 alone to the default | Its links become cheap and traffic drifts onto R4 |
| 4 | Bring R4 back in line | Equal cost returns and two next hops land in the FIB |
| 5 | Set cost on one interface | It wins over the reference bandwidth. Only the forward path changes |
| 6 | Restore all defaults | Back to the STEP 0 state (final state) |
STEP 0: the default state
The Cost column of show ospf interface brief is what this article looks at most. The 1 Gbps physical interfaces and the Loopback all read 1.
RP/0/RP0/CPU0:R1#show ospf interface brief
Fri Sep 11 07:28:19.669 UTC
* Indicates MADJ interface, (P) Indicates fast detect hold down state
Interfaces for OSPF 1
Interface PID Area IP Address/Mask Cost State Nbrs F/C
Lo0 1 0 1.1.1.1/32 1 LOOP 0/0
Gi0/0/0/0 1 0 10.1.2.1/24 1 DR 1/1
Gi0/0/0/1 1 0 10.1.4.1/24 1 DR 1/1R1 installs two paths to 3.3.3.3/32, both with metric 3: R1’s outgoing side 1, plus R2’s (or R4’s) outgoing side 1, plus R3’s Loopback 1.
RP/0/RP0/CPU0:R1#show route ospf
Fri Sep 11 07:28:18.205 UTC
O 2.2.2.2/32 [110/2] via 10.1.2.2, 00:08:37, GigabitEthernet0/0/0/0
O 3.3.3.3/32 [110/3] via 10.1.2.2, 00:04:18, GigabitEthernet0/0/0/0
[110/3] via 10.1.4.4, 00:04:18, GigabitEthernet0/0/0/1
O 4.4.4.4/32 [110/2] via 10.1.4.4, 00:04:18, GigabitEthernet0/0/0/1
O 10.2.3.0/24 [110/2] via 10.1.2.2, 00:08:37, GigabitEthernet0/0/0/0
O 10.3.4.0/24 [110/2] via 10.1.4.4, 00:04:18, GigabitEthernet0/0/0/1The default reference bandwidth appears in no show output. show ospf, show ospf 1, show protocols ospf, show ospf interface and show running-config router ospf 1 were all checked and none of them prints it. While the value is the default it does not appear in the configuration either.
STEP 1: lower the bandwidth and watch the cost move
To confirm the cost follows the interface bandwidth, lower the bandwidth on R1’s interface towards R4 to the equivalent of 10 Mbps.
(R1)
interface GigabitEthernet0/0/0/1
bandwidth 10000That interface now costs 10: the default reference bandwidth of 100 Mbps divided by 10 Mbps.
RP/0/RP0/CPU0:R1#show ospf interface brief
Fri Sep 11 07:30:57.443 UTC
* Indicates MADJ interface, (P) Indicates fast detect hold down state
Interfaces for OSPF 1
Interface PID Area IP Address/Mask Cost State Nbrs F/C
Lo0 1 0 1.1.1.1/32 1 LOOP 0/0
Gi0/0/0/0 1 0 10.1.2.1/24 1 DR 1/1
Gi0/0/0/1 1 0 10.1.4.1/24 10 DR 1/1The routes change too. 3.3.3.3/32 drops to a single path through R2. The interesting line is 4.4.4.4/32: R4 is directly attached, yet R1 now reaches it the long way round through R2 and R3.
RP/0/RP0/CPU0:R1#show route ospf
Fri Sep 11 07:30:54.975 UTC
O 2.2.2.2/32 [110/2] via 10.1.2.2, 00:11:14, GigabitEthernet0/0/0/0
O 3.3.3.3/32 [110/3] via 10.1.2.2, 00:00:36, GigabitEthernet0/0/0/0
O 4.4.4.4/32 [110/4] via 10.1.2.2, 00:00:36, GigabitEthernet0/0/0/0
O 10.2.3.0/24 [110/2] via 10.1.2.2, 00:11:14, GigabitEthernet0/0/0/0
O 10.3.4.0/24 [110/3] via 10.1.2.2, 00:00:36, GigabitEthernet0/0/0/0Going straight to R4 costs 10 plus 1 for the Loopback, so 11. Going round through R2 and R3 costs 1 plus 1 plus 1 plus 1, so 4. Cost is neither distance nor hop count, so a path with more hops can be the cheaper one.
STEP 2: raise the reference bandwidth
This is the fix for 1 Gbps and 10 Gbps collapsing to the same cost. With a reference bandwidth of 100 Gbps, a 1 Gbps link costs 100, a 10 Gbps link costs 10 and a 100 Gbps link costs 1.
Remove the bandwidth from STEP 1 and apply the same value to all four routers.
(apply to R1, R2, R3 and R4)
router ospf 1
auto-cost reference-bandwidth 100000The 1 Gbps physical interfaces move from 1 to 100. The Loopback stays at 1.
RP/0/RP0/CPU0:R1#show ospf interface brief
Fri Sep 11 07:33:59.373 UTC
* Indicates MADJ interface, (P) Indicates fast detect hold down state
Interfaces for OSPF 1
Interface PID Area IP Address/Mask Cost State Nbrs F/C
Lo0 1 0 1.1.1.1/32 1 LOOP 0/0
Gi0/0/0/0 1 0 10.1.2.1/24 100 DR 1/1
Gi0/0/0/1 1 0 10.1.4.1/24 100 DR 1/1The metric now breaks down cleanly. The 201 for 3.3.3.3/32 is R1’s outgoing side 100, plus R2’s outgoing side 100, plus R3’s Loopback 1. That last 1 is why routes to a loopback always carry an extra 1.
RP/0/RP0/CPU0:R1#show route ospf
Fri Sep 11 07:33:56.694 UTC
O 2.2.2.2/32 [110/101] via 10.1.2.2, 00:01:00, GigabitEthernet0/0/0/0
O 3.3.3.3/32 [110/201] via 10.1.2.2, 00:00:42, GigabitEthernet0/0/0/0
[110/201] via 10.1.4.4, 00:00:42, GigabitEthernet0/0/0/1
O 4.4.4.4/32 [110/101] via 10.1.4.4, 00:01:00, GigabitEthernet0/0/0/1
O 10.2.3.0/24 [110/200] via 10.1.2.2, 00:00:48, GigabitEthernet0/0/0/0
O 10.3.4.0/24 [110/200] via 10.1.4.4, 00:00:42, GigabitEthernet0/0/0/1The cost travels through the area in the Router-LSA. In the LSA R1 originates, the per-link TOS 0 Metrics matches what the interface now shows: 1 for the Loopback stub and 100 for each of the two physical links.
Number of Links: 3
Link connected to: a Stub Network
(Link ID) Network/subnet number: 1.1.1.1
(Link Data) Network Mask: 255.255.255.255
Number of TOS metrics: 0
TOS 0 Metrics: 1
Link connected to: a Transit Network
(Link ID) Designated Router address: 10.1.2.1
(Link Data) Router Interface address: 10.1.2.1
Number of TOS metrics: 0
TOS 0 Metrics: 100
Link connected to: a Transit Network
(Link ID) Designated Router address: 10.1.4.1
(Link Data) Router Interface address: 10.1.4.1
Number of TOS metrics: 0
TOS 0 Metrics: 100STEP 3: what one forgotten router does
Return R4 alone to the default reference bandwidth, reproducing a configuration that was missed during a rollout.
(R4 only)
router ospf 1
no auto-cost reference-bandwidth 100000Leaving the value off no auto-cost reference-bandwidth does not remove it. The commit succeeds and show configuration commit changes reports no change, but the line stays in show running-config and the costs do not move. The value has to be typed out.
R4’s interfaces are back to 1 while the other three routers stay at 100.
RP/0/RP0/CPU0:R4#show ospf interface brief
Fri Sep 11 07:39:11.310 UTC
* Indicates MADJ interface, (P) Indicates fast detect hold down state
Interfaces for OSPF 1
Interface PID Area IP Address/Mask Cost State Nbrs F/C
Lo0 1 0 4.4.4.4/32 1 LOOP 0/0
Gi0/0/0/0 1 0 10.1.4.4/24 1 BDR 1/1
Gi0/0/0/1 1 0 10.3.4.4/24 1 BDR 1/1Both of R4’s links are now a hundred times cheaper than the rest, so the path from R1 to R3 drifts onto R4. The two equal-cost paths collapse to one and the metric falls from 201 to 102.
RP/0/RP0/CPU0:R1#show route ospf
Fri Sep 11 07:38:04.079 UTC
O 2.2.2.2/32 [110/101] via 10.1.2.2, 00:05:07, GigabitEthernet0/0/0/0
O 3.3.3.3/32 [110/102] via 10.1.4.4, 00:01:09, GigabitEthernet0/0/0/1
O 4.4.4.4/32 [110/101] via 10.1.4.4, 00:05:07, GigabitEthernet0/0/0/1
O 10.2.3.0/24 [110/200] via 10.1.2.2, 00:04:55, GigabitEthernet0/0/0/0
O 10.3.4.0/24 [110/101] via 10.1.4.4, 00:01:09, GigabitEthernet0/0/0/1The reverse direction does the same. R3 also reaches R1 through R4.
RP/0/RP0/CPU0:R3#show route ospf
Fri Sep 11 07:38:46.951 UTC
O 1.1.1.1/32 [110/102] via 10.3.4.4, 00:01:52, GigabitEthernet0/0/0/1
O 2.2.2.2/32 [110/101] via 10.2.3.2, 00:05:38, GigabitEthernet0/0/0/0
O 4.4.4.4/32 [110/101] via 10.3.4.4, 00:05:38, GigabitEthernet0/0/0/1
O 10.1.2.0/24 [110/200] via 10.2.3.2, 00:05:38, GigabitEthernet0/0/0/0
O 10.1.4.0/24 [110/101] via 10.3.4.4, 00:01:52, GigabitEthernet0/0/0/1Traffic collects on the one router that was missed. Raising the reference bandwidth has to be done on every router in the area, with none left out.
STEP 4: bring it back in line
Applying the same value to R4 restores the two equal-cost paths.
RP/0/RP0/CPU0:R1#show route ospf
Fri Sep 11 07:40:15.979 UTC
O 2.2.2.2/32 [110/101] via 10.1.2.2, 00:07:19, GigabitEthernet0/0/0/0
O 3.3.3.3/32 [110/201] via 10.1.2.2, 00:00:38, GigabitEthernet0/0/0/0
[110/201] via 10.1.4.4, 00:00:38, GigabitEthernet0/0/0/1
O 4.4.4.4/32 [110/101] via 10.1.4.4, 00:07:19, GigabitEthernet0/0/0/1
O 10.2.3.0/24 [110/200] via 10.1.2.2, 00:07:07, GigabitEthernet0/0/0/0
O 10.3.4.0/24 [110/200] via 10.1.4.4, 00:00:38, GigabitEthernet0/0/0/1Equal-cost paths land in the FIB as well as the RIB. The tail of show cef lists both next hops together with the hash buckets that spread flows across them.
via 10.1.2.2/32, GigabitEthernet0/0/0/0, 7 dependencies, weight 0, class 0 [flags 0x0]
path-idx 0 NHID 0x2 [0x8a75b3c0 0x0]
next hop 10.1.2.2/32
local adjacency
via 10.1.4.4/32, GigabitEthernet0/0/0/1, 7 dependencies, weight 0, class 0 [flags 0x0]
path-idx 1 NHID 0x4 [0x8a75b500 0x0]
next hop 10.1.4.4/32
local adjacency
Load distribution: 0 1 0 1 (refcount 2)
Hash OK Interface Address
0 Y GigabitEthernet0/0/0/0 10.1.2.2
1 Y GigabitEthernet0/0/0/1 10.1.4.4
2 Y GigabitEthernet0/0/0/0 10.1.2.2
3 Y GigabitEthernet0/0/0/1 10.1.4.4 STEP 5: set a cost on one interface
The reference bandwidth applies one formula to every interface, so it is the wrong tool for adjusting a single link. That is what the per-interface cost is for.
Set the cost on R1’s interface towards R2 to 50.
(R1)
router ospf 1
area 0
interface GigabitEthernet0/0/0/0
cost 50The display shows the configured 50, not the 100 the reference bandwidth would produce. The per-interface cost wins.
RP/0/RP0/CPU0:R1#show ospf interface brief
Fri Sep 11 07:42:43.518 UTC
* Indicates MADJ interface, (P) Indicates fast detect hold down state
Interfaces for OSPF 1
Interface PID Area IP Address/Mask Cost State Nbrs F/C
Lo0 1 0 1.1.1.1/32 1 LOOP 0/0
Gi0/0/0/0 1 0 10.1.2.1/24 50 DR 1/1
Gi0/0/0/1 1 0 10.1.4.1/24 100 DR 1/1R1 now reaches R3 by a single path through R2: 50 plus 100 plus 1 is 151, against 100 plus 100 plus 1 for the R4 path.
RP/0/RP0/CPU0:R1#show route ospf
Fri Sep 11 07:42:41.932 UTC
O 2.2.2.2/32 [110/51] via 10.1.2.2, 00:00:46, GigabitEthernet0/0/0/0
O 3.3.3.3/32 [110/151] via 10.1.2.2, 00:00:46, GigabitEthernet0/0/0/0
O 4.4.4.4/32 [110/101] via 10.1.4.4, 00:09:45, GigabitEthernet0/0/0/1
O 10.2.3.0/24 [110/150] via 10.1.2.2, 00:00:46, GigabitEthernet0/0/0/0
O 10.3.4.0/24 [110/200] via 10.1.4.4, 00:03:04, GigabitEthernet0/0/0/1Now look at the other end. R3 still has two equal-cost paths back to R1. Nothing changed.
RP/0/RP0/CPU0:R3#show route ospf
Fri Sep 11 07:43:29.020 UTC
O 1.1.1.1/32 [110/201] via 10.2.3.2, 00:03:51, GigabitEthernet0/0/0/0
[110/201] via 10.3.4.4, 00:03:51, GigabitEthernet0/0/0/1
O 2.2.2.2/32 [110/101] via 10.2.3.2, 00:10:20, GigabitEthernet0/0/0/0
O 4.4.4.4/32 [110/101] via 10.3.4.4, 00:10:20, GigabitEthernet0/0/0/1
O 10.1.2.0/24 [110/200] via 10.2.3.2, 00:10:20, GigabitEthernet0/0/0/0
O 10.1.4.0/24 [110/200] via 10.3.4.4, 00:03:51, GigabitEthernet0/0/0/1Only R1’s outgoing cost was changed; the outgoing sides on R3 and R2 were left alone. A one-sided cost change is all it takes to make the forward and return paths differ. Apply the same change at both ends if you want the routing to stay symmetric.
STEP 6: restore the defaults
Removing the cost and the reference bandwidth returns everything to the STEP 0 state.
RP/0/RP0/CPU0:R1#show ospf interface brief
Fri Sep 11 07:45:21.407 UTC
* Indicates MADJ interface, (P) Indicates fast detect hold down state
Interfaces for OSPF 1
Interface PID Area IP Address/Mask Cost State Nbrs F/C
Lo0 1 0 1.1.1.1/32 1 LOOP 0/0
Gi0/0/0/0 1 0 10.1.2.1/24 1 DR 1/1
Gi0/0/0/1 1 0 10.1.4.1/24 1 DR 1/1Design notes
- If you raise the reference bandwidth, raise it on every router in the area. One router left out collects traffic around itself (STEP 3)
- Pick a value with room to spare. Even with no 100 Gbps links today, leave headroom so a faster link can still be told apart later. Changing it means revisiting every router, so choose a value you will not have to revise
- The reference bandwidth does not affect Loopback costs. A route to a loopback always carries an extra
1 - A
costapplies only to the outgoing direction. Set it on one side and the forward and return paths diverge (STEP 5) - Type the value out in
no auto-cost reference-bandwidth. Leave it off and the commit succeeds while the configuration stays
Verification configuration and show output
Three kinds of file were collected from all four routers at every STEP, one file per router. The verification configuration is the ..._run.txt file (the final state is the STEP 6 one).
| File | Contents |
|---|---|
..._show.txt | show version / show interface description / show route / show route ospf / show ospf / show ospf interface / show ospf interface brief / show ospf neighbor / show ospf neighbor detail / show ospf database / show ospf database router / show ospf database network / show ospf statistics interface / show cef 3.3.3.3/32 / show ospf database router self-originate |
..._log.txt | show logging narrowed to that STEP. A marker was written with logmsg at the start of each STEP and its timestamp passed to show logging start |
..._run.txt | show running-config at that STEP (the verification configuration for that STEP) |
STEP 0: Defaults - 1 Gbps links and Loopbacks all have cost 1; the two paths are equal cost
| Router | show output | syslog | running-config |
|---|---|---|---|
| R1 | show | log | run |
| R2 | show | log | run |
| R3 | show | log | run |
| R4 | show | log | run |
STEP 1: Lower bandwidth on one interface - the cost follows the bandwidth, and the longer way round is cheaper
| Router | show output | syslog | running-config |
|---|---|---|---|
| R1 | show | log | run |
| R2 | show | log | run |
| R3 | show | log | run |
| R4 | show | log | run |
STEP 2: Set the reference bandwidth to 100 Gbps on all four - every cost changes at once, Loopbacks do not
| Router | show output | syslog | running-config |
|---|---|---|---|
| R1 | show | log | run |
| R2 | show | log | run |
| R3 | show | log | run |
| R4 | show | log | run |
STEP 3: Return R4 alone to the default - its links become cheap and traffic drifts onto R4
| Router | show output | syslog | running-config |
|---|---|---|---|
| R1 | show | log | run |
| R2 | show | log | run |
| R3 | show | log | run |
| R4 | show | log | run |
STEP 4: Bring R4 back in line - equal cost returns and two next hops land in the FIB
| Router | show output | syslog | running-config |
|---|---|---|---|
| R1 | show | log | run |
| R2 | show | log | run |
| R3 | show | log | run |
| R4 | show | log | run |
STEP 5: Set cost on one interface - it wins over the reference bandwidth; only the forward path changes
| Router | show output | syslog | running-config |
|---|---|---|---|
| R1 | show | log | run |
| R2 | show | log | run |
| R3 | show | log | run |
| R4 | show | log | run |
STEP 6: Restore all defaults (final state) - back to the STEP 0 state
| Router | show output | syslog | running-config |
|---|---|---|---|
| R1 | show | log | run |
| R2 | show | log | run |
| R3 | show | log | run |
| R4 | show | log | run |
References
| RFC | Title | Summary |
|---|---|---|
| RFC 2328 | OSPF Version 2 | Defines the cost: attached to the output side of each router interface and configured by the administrator (Section 2.1), greater than zero (Section 9), and carried in a 16-bit metric field in the Router-LSA (Appendix A.4.2). It does not specify how to derive a cost from bandwidth. |
Related articles
- What Is OSPF
- OSPF Router ID
- OSPF Packet Types and Header Format
- OSPF Authentication
- OSPF Neighbor States
- OSPF Options Field
- OSPF DR and BDR
- OSPF Network Types
- OSPF Cost (Metric)
- OSPF External Routes (Redistributing Static Routes)
- OSPF Multiple Areas and the ABR
- OSPF Virtual Links
- OSPF Stub and Totally Stubby Areas
- OSPF NSSA and Totally NSSA
- OSPF Route Summarization
- OSPF LSAs and the LSA Header
- OSPF router-LSA (type 1)
- OSPF network-LSA (type 2)
- OSPF summary-LSA (type 3)
- OSPF ASBR summary-LSA (type 4)
- OSPF AS external-LSA (type 5)
- OSPF NSSA External-LSA (Type 7)