PacketLab
OSPF · Database and path selection

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.

Lab topologyIllustrative topology
192.0.2.0/30area 0192.0.2.4/30area 0192.0.2.8/30area 0R1Lo0 · 10.255.0.1/32R2Lo0 · 10.255.0.2/32R3Lo0 · 10.255.0.3/32
Solid lines are physical links, configured as point-to-point. Interfaces and areas are listed below. Router symbol: GNS3, GPL-2.0. Original layout.
RouterInterfaceIPv4 addressArea
R1Gi1192.0.2.1/300
R2Gi1192.0.2.2/300
R1Gi2192.0.2.5/300
R3Gi1192.0.2.6/300
R2Gi2192.0.2.9/300
R3Gi2192.0.2.10/300

Loopback0 is 10.255.0.<router number>/32; area assignments: R1: area 0, R2: area 0, R3: area 0.

Complete baseline configurations

R1

Configuration
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
end

R2

Configuration
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
end

R3

Configuration
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
end

Apply

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

Configuration
configure terminal
interface GigabitEthernet2
 ip ospf cost 30
end
Candidate at R1Cost to 10.255.0.3/32
Direct R1→R330 + 1 = 31
R1→R2→R310 + 10 + 1 = 21

Verify and interpret

Illustrative output · excerpt
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, GigabitEthernet1

Forward and return paths differ because the single change is directional. This is valid routing, not proof of a fault.

R1 · restore

Configuration
configure terminal
interface GigabitEthernet2
 ip ospf cost 10
end
Illustrative output · excerpt
R1# show ip route ospf | include 10.255.0.3
O 10.255.0.3/32 [110/11] via 192.0.2.6, GigabitEthernet2

Restoring the cost returns the direct next hop. Use a sourced ping to test forwarding separately.

Technical references

OSPFv2 standard · Router configuration guide

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.

SPF calculation Quiz

Six questions · four options each · select one answer per question. Submit all answers to see your score and explanations.

1. Where is the SPF tree rooted?
2. What is the original direct cost to R3 Lo0?
3. What is the original path via R2?
4. After R1 Gi2 becomes cost 30, which path wins?
5. Does this change R3’s outgoing cost toward R1?
6. What restores the original next hop?