OSPF is a link-state routing protocol, which means each router builds a complete map of the network topology, unlike distance-vector protocols that only know about their neighbors’ routes.
Let’s see OSPF in action on a small network. Imagine we have three routers: R1, R2, and R3, connected as follows:
R1 <–> R2 <–> R3
And let’s say we have the following IP subnets:
- R1’s interface to R2: 192.168.1.0/24 (R1 has 192.168.1.1, R2 has 192.168.1.2)
- R2’s interface to R3: 192.168.2.0/24 (R2 has 192.168.2.1, R3 has 192.168.2.2)
- R1 has a loopback interface: 10.1.1.1/32
- R3 has a loopback interface: 10.3.3.3/32
First, we need to enable OSPF on each router. On Cisco IOS, this looks like:
On R1:
router ospf 1
network 192.168.1.0 0.0.0.255 area 0
network 10.1.1.1 0.0.0.0 area 0
On R2:
router ospf 1
network 192.168.1.0 0.0.0.255 area 0
network 192.168.2.0 0.0.0.255 area 0
On R3:
router ospf 1
network 192.168.2.0 0.0.0.255 area 0
network 10.3.3.3 0.0.0.0 area 0
The router ospf 1 command starts the OSPF process with a process ID of 1. The network commands tell OSPF which interfaces to participate on and which subnets to advertise. The wildcard mask (e.g., 0.0.0.255) is crucial; it specifies which bits in the IP address must match. For a /24 subnet, the wildcard mask is 0.0.0.255. For a /32 host route (like a loopback), it’s 0.0.0.0. area 0 signifies the backbone area, common in small OSPF deployments.
After configuring this, R1 and R2 will form an OSPF adjacency over the 192.168.1.0/24 link. R2 and R3 will form an adjacency over the 192.168.2.0/24 link.
Once adjacencies are formed, routers exchange Link-State Advertisements (LSAs). Each LSA describes a router’s local links and their states. R1 will generate an LSA for its interfaces (192.168.1.1 and 10.1.1.1) and send it to R2. R2 will generate LSAs for its interfaces (192.168.1.2 and 192.168.2.1) and send them to R1 and R3. R3 will generate LSAs for its interfaces (192.168.2.2 and 10.3.3.3) and send them to R2.
All these LSAs are flooded throughout the OSPF domain (area 0 in this case). Each router collects all the LSAs and uses them to build an identical Link-State Database (LSDB).
Let’s look at the LSDB on R2:
show ip ospf database
You’d see entries for R1, R2, and R3, detailing their connected links. For example, R1’s loopback (10.1.1.1/32) would be listed as a link originating from R1.
With this complete map (the LSDB), each router independently runs Dijkstra’s Shortest Path First (SPF) algorithm. This algorithm calculates the shortest path to every other destination in the network based on link costs. The cost is typically inversely proportional to bandwidth, so a faster link has a lower cost. By default, Cisco IOS uses a reference bandwidth of 100 Mbps. The formula is Cost = Reference Bandwidth / Interface Bandwidth. For example:
- 10 Mbps Ethernet: Cost = 100 Mbps / 10 Mbps = 10
- 100 Mbps Ethernet: Cost = 100 Mbps / 100 Mbps = 1
- Gigabit Ethernet (1000 Mbps): Cost = 100 Mbps / 1000 Mbps = 0.125 (often rounded up to 1 or configured manually)
The SPF algorithm populates the router’s IP routing table with the best paths.
On R1, after SPF runs, the routing table would look something like this:
show ip route ospf
O 10.3.3.0/32 [110/2] via 192.168.1.2, 00:00:40, GigabitEthernet0/1
O 10.3.3.3/32 [110/2] via 192.168.1.2, 00:00:40, GigabitEthernet0/1
O 192.168.2.0/24 [110/1] via 192.168.1.2, 00:00:40, GigabitEthernet0/1
Here, O indicates OSPF. 110 is the administrative distance for OSPF. 2 and 1 are the total costs to reach the respective destinations. via 192.168.1.2 is the next-hop IP address.
The core problem OSPF solves is efficient and scalable routing in large, complex IP networks. Its link-state nature means that when a link changes state (goes up or down), only the routers directly connected to that link are immediately affected. They generate a new LSA, which is flooded. This triggers a recalculation of the SPF tree only for affected routers, rather than a full network-wide routing update like in some older protocols.
A key detail that often trips people up is the concept of Designated Router (DR) and Backup Designated Router (BDR) on multi-access network segments (like Ethernet). To reduce the number of adjacencies and LSA flooding, OSPF elects a DR and BDR on each segment. Only the DR and BDR form full adjacencies with all other routers on that segment. Other routers only form a "2-way" state adjacency with each other and exchange LSAs with the DR/BDR. This significantly cuts down on redundant traffic. The election is based on OSPF priority (higher is better, default is 1) and then router priority. If you have a segment where you want to force a router to be the DR, you’d configure ip ospf priority 255 on its interface.
The next concept to explore is OSPF areas, which allow for the segmentation of large OSPF networks to reduce the size of LSDBs and SPF calculations on individual routers.