SPF calculation
Calculate a path from each router’s own position and predict a cost change.
Understand
SPF builds a shortest-path tree rooted at the local router from the area topology. It repeatedly settles the nearest reachable vertex and relaxes outgoing links. OSPF costs are directional: changing R1’s outgoing cost does not change R2’s outgoing cost. Prefix cost includes the path to the advertising router plus the attached prefix cost.
In the triangle baseline all transit costs are 10. R1 reaches R3’s loopback directly with 10 + 1 = 11; via R2 costs 10 + 10 + 1 = 21. The direct path therefore wins. Equal paths may supply multiple next hops. SPF is topology calculation; route-type selection, policy and competition with other protocols still affect the installed routing table.
Lab setup
Use a fresh lab for this lesson; changes from other lessons are not cumulative. All outputs are illustrative excerpts, not recorded router tests. Transit cost is explicitly 10 and Loopback0 cost is 1. Interface names may vary by image.
| Router | Interface | IPv4 address | Area |
|---|---|---|---|
| R1 | Gi1 | 192.0.2.1/30 | 0 |
| R2 | Gi1 | 192.0.2.2/30 | 0 |
| R1 | Gi2 | 192.0.2.5/30 | 0 |
| R3 | Gi1 | 192.0.2.6/30 | 0 |
| R2 | Gi2 | 192.0.2.9/30 | 0 |
| R3 | Gi2 | 192.0.2.10/30 | 0 |
Loopback0 is 10.255.0.<router number>/32; area assignments: R1: area 0, R2: area 0, R3: area 0.
Complete baseline configurations
R1
configure terminal
hostname R1
router ospf 1
router-id 10.255.0.1
passive-interface Loopback0
exit
interface Loopback0
ip address 10.255.0.1 255.255.255.255
ip ospf 1 area 0
ip ospf cost 1
exit
interface GigabitEthernet1
ip address 192.0.2.1 255.255.255.252
ip ospf network point-to-point
ip ospf cost 10
ip ospf 1 area 0
no shutdown
exit
interface GigabitEthernet2
ip address 192.0.2.5 255.255.255.252
ip ospf network point-to-point
ip ospf cost 10
ip ospf 1 area 0
no shutdown
exit
endR2
configure terminal
hostname R2
router ospf 1
router-id 10.255.0.2
passive-interface Loopback0
exit
interface Loopback0
ip address 10.255.0.2 255.255.255.255
ip ospf 1 area 0
ip ospf cost 1
exit
interface GigabitEthernet1
ip address 192.0.2.2 255.255.255.252
ip ospf network point-to-point
ip ospf cost 10
ip ospf 1 area 0
no shutdown
exit
interface GigabitEthernet2
ip address 192.0.2.9 255.255.255.252
ip ospf network point-to-point
ip ospf cost 10
ip ospf 1 area 0
no shutdown
exit
endR3
configure terminal
hostname R3
router ospf 1
router-id 10.255.0.3
passive-interface Loopback0
exit
interface Loopback0
ip address 10.255.0.3 255.255.255.255
ip ospf 1 area 0
ip ospf cost 1
exit
interface GigabitEthernet1
ip address 192.0.2.6 255.255.255.252
ip ospf network point-to-point
ip ospf cost 10
ip ospf 1 area 0
no shutdown
exit
interface GigabitEthernet2
ip address 192.0.2.10 255.255.255.252
ip ospf network point-to-point
ip ospf cost 10
ip ospf 1 area 0
no shutdown
exit
endApply
Raise only R1’s direct R3 interface cost. R1 Gi2 is 192.0.2.5/30; do not change the R2–R3 interface by mistake.
R1
configure terminal
interface GigabitEthernet2
ip ospf cost 30
end| Candidate at R1 | Cost to 10.255.0.3/32 |
|---|---|
| Direct R1→R3 | 30 + 1 = 31 |
| R1→R2→R3 | 10 + 10 + 1 = 21 |
Verify and interpret
R1# show ip route ospf | include 10.255.0.3
O 10.255.0.3/32 [110/21] via 192.0.2.2, GigabitEthernet1
R3# show ip route ospf | include 10.255.0.1
O 10.255.0.1/32 [110/11] via 192.0.2.5, GigabitEthernet1Forward and return paths differ because the single change is directional. This is valid routing, not proof of a fault.
R1 · restore
configure terminal
interface GigabitEthernet2
ip ospf cost 10
endR1# show ip route ospf | include 10.255.0.3
O 10.255.0.3/32 [110/11] via 192.0.2.6, GigabitEthernet2Restoring the cost returns the direct next hop. Use a sourced ping to test forwarding separately.
Technical references
Key Takeaways
- SPF is rooted at the local router.
- Outgoing costs are directional.
- Add all outgoing transit costs and the destination prefix cost.
- Different roots can produce asymmetric forwarding.
- Route calculation and route installation are distinct.