OSPF Fundamental - How SPF works


In this lesson, you will learn how OSPF uses the Shortest Path First (SPF) algorithm to calculate the best loop-free path through a network.

The example focuses on point-to-point links and explains how OSPF builds its topology, evaluates cumulative path costs, and selects the shortest path from the local router to each destination.

Table of Contents

What Is SPF in OSPF?

Shortest Path First is the algorithm OSPF uses to calculate the best paths through an area. Each router runs the SPF calculation from its own perspective and treats itself as the root of the shortest-path tree.

The calculation uses the topology information stored in the OSPF link-state database. Because routers in the same area should have a synchronized view of the topology, they can independently calculate consistent loop-free paths.

How the SPF Calculation Works

Before running the SPF algorithm, an OSPF router forms neighbor relationships and exchanges Link-State Advertisements. These LSAs describe routers, links, networks, and their associated OSPF costs.

  1. The router receives LSAs from other OSPF routers in the area.
  2. It stores the topology information in its link-state database.
  3. The local router places itself at the root of the SPF tree.
  4. SPF evaluates the available links and their accumulated costs.
  5. The lowest-cost loop-free path to each reachable destination is selected.
  6. The resulting best routes are considered for installation in the routing table.

How OSPF Compares Path Costs

OSPF selects paths according to their cumulative cost. The total cost to a destination is calculated by adding the outbound interface costs along the complete path.

A path with fewer router hops is not automatically preferred. A path containing more hops can still become the best path when its total OSPF cost is lower.

When multiple paths to the same destination have an equal total cost, OSPF can install equal-cost paths and use Equal-Cost Multipath routing, subject to the platform configuration and supported path limit.

Point-to-Point SPF Example

The topology in this lesson uses point-to-point links between the routers. On a point-to-point network, OSPF represents the direct relationship between the two connected routers and uses the configured interface costs when calculating the shortest-path tree.

The video demonstrates how the local router evaluates the available links, compares cumulative costs, and progressively builds a loop-free tree toward all reachable routers and networks.

Verifying the OSPF Best Path

After OSPF completes the SPF calculation, use the Cisco IOS verification commands below to examine the topology and confirm the selected routes.

Check OSPF neighbors

show ip ospf neighbor

This command confirms that the expected OSPF neighbor relationships are established.

Check the link-state database

show ip ospf database

This command displays the LSAs used by OSPF to build its view of the area topology.

Check the OSPF routing table

show ip route ospf

This command displays the OSPF routes installed in the routing table, including their administrative distance and calculated metric.

Check a specific destination

show ip route <destination-network>

Use this command to verify the selected next hop, outgoing interface, and OSPF metric for a particular destination.

Check OSPF interface costs

show ip ospf interface brief
show ip ospf interface <interface-name>

These commands help confirm the interface costs that contribute to the cumulative path calculation.

Conclusion

OSPF uses the SPF algorithm to create a shortest-path tree from the perspective of each router. The calculation is based on the synchronized link-state database and the cumulative OSPF cost of every available path.

Understanding the SPF process makes it easier to predict OSPF path selection, troubleshoot unexpected routes, and design more efficient and resilient OSPF networks.

Related Articles