What problem does OSPF solve?
A router needs a route before it can send a packet towards another network. A route tells it which neighbouring router to use next. You can enter routes by hand, but a growing network needs many entries and changes can make them out of date.
OSPF (Open Shortest Path First) lets routers share information about their connections. Each router uses that information to choose routes. If a connection fails and another usable path exists, OSPF can calculate a new path. It cannot create a path where there is no working connection.
In the example, a user on LAN A opens an internal website on LAN B. R1 is the router near the user. R4 connects to the server's LAN. R2 and R3 provide two possible paths between them. OSPF learns the paths; the routers then forward the user's packets.
http://192.168.4.10PC-A sends a web request to the web server. The routers must find a working path between them.
R1–R4: routers join different networks. SW1/SW2: switches connect devices within each LAN; they do not add routed hops or extra OSPF costs.
Grey lines: Ethernet connections. SW1 and SW2 are access switches. Purple arrow: the web request's path. The response travels back; it is not drawn here.
Device IP: 192.168.4.10 identifies this server. Subnet: 192.168.4.0/24 identifies its network, not a device or website. URL: http://192.168.4.10 tells the browser to request a web page at that IP.
- Destination subnet
- 192.168.4.0/24
- Next hop · router interface IP
- 10.0.12.2 · R2
- Forwarding path
- R1 → R2 → R4
- OSPF cost
- 3
- R1 · RID 1.1.1.1
- Gi0/0: 192.168.1.1/24 → LAN A; Gi0/1: 10.0.12.1/30 → R2; Gi0/2: 10.0.13.1/30 → R3.
- R2 · RID 2.2.2.2
- Gi0/0: 10.0.12.2/30 → R1; Gi0/1: 10.0.24.1/30 → R4.
- R3 · RID 3.3.3.3
- Gi0/0: 10.0.13.2/30 → R1; Gi0/1: 10.0.34.1/30 → R4.
- R4 · RID 4.4.4.4
- Gi0/0: 192.168.4.1/24 → LAN B; Gi0/1: 10.0.24.2/30 → R2; Gi0/2: 10.0.34.2/30 → R3.
- LAN gateways and switch ports
- PC-A gateway: 192.168.1.1. Server gateway: 192.168.4.1. Each access switch uses Gi0/1 to its endpoint and Gi0/2 to its router; endpoints use Ethernet ports.
- OSPF assumptions and costs
- Area 0; broadcast Ethernet; Hello 10 / Dead 40 seconds; no authentication; LAN interfaces passive. R1–R3 cost 10 in both directions; other outgoing interfaces, including the LANs, cost 1. Switches do not run OSPF here.
The OSPF process on one router
On a Cisco router OSPF is off until you start it. Starting it creates an OSPF process: a program running on the router alongside everything else, with a number you choose.
router ospf 1Global configuration mode. Starts OSPF process 1 (any number from 1 to 65535).
The process ID, 1 here, identifies this OSPF process on this router. R2 could use process 20 and still become R1's neighbour. Creating a process alone does not turn on OSPF on every interface. You must also select the interfaces that should take part.
Compare the OSPF tables and the three kinds of ID
- Local process ID
- 1
- Router ID · OSPF identity
- 1.1.1.1
- Gi0/1 · interface IPv4
- 10.0.12.1/30
- Local process ID
- 10
- Router ID · OSPF identity
- 2.2.2.2
- Gi0/0 · interface IPv4
- 10.0.12.2/30
10.0.12.0/30 is the shared subnet. The routers exchange OSPF control packets here.The router ID
The router ID (RID) identifies a router in OSPF. It is written like an IPv4 address, such as 1.1.1.1, but it does not have to be the address used to reach the router. Give each OSPF router a different ID.
On Cisco IOS, a configured router ID is used first. If none is set, IOS chooses the highest IPv4 address on an available loopback interface. A loopback is a software interface rather than a physical socket. If there is no usable loopback, IOS chooses the highest IPv4 address on an active physical interface.
See how IOS chooses a router ID
router ospf 1
router-id 1.1.1.1Setting the RID by hand is normal practice: it is predictable and doesn't change when interfaces are added.
If none of the three gives an address (for example, a router with no IPv4 addresses yet), the process can't start, and IOS logs a message like this:
%OSPF-4-NORTRID: OSPF process 1 failed to allocate unique router-id and cannot start
Set the RID before forming adjacencies. Applying a changed RID may require an OSPF process restart, depending on the IOS release. Check the active ID with show ip ospf. Restarting with clear ip ospf process disrupts this process's adjacencies.
Neighbours: who to talk to
An OSPF neighbour is another OSPF router found on a connected link. R1 sends small Hello messages to discover R2 and R3 and check that they are still there. On Cisco broadcast and point-to-point interfaces, the default Hello interval is normally 10 seconds.
Routers that need to exchange their network records build a closer connection called an adjacency. When the required information has been exchanged, the connection is Full. A passive interface advertises its network without sending Hellos or building neighbours there.
On a shared Ethernet LAN, routers also choose a designated router (DR) to help share OSPF updates and a backup designated router (BDR) to take over if it fails. These are roles given to existing routers. The DR does not choose routes for everyone else.
R1#show ip ospf neighbor Neighbor ID Pri State Dead Time Address Interface 2.2.2.2 1 FULL/DR 00:00:36 10.0.12.2 GigabitEthernet0/1 3.3.3.3 1 FULL/DR 00:00:33 10.0.13.2 GigabitEthernet0/2
Links, link state and LSAs
A link is a connection to another router or network. Its link state describes information such as which network it reaches and the cost of using it. A link-state advertisement (LSA) is an OSPF record carrying network information. It is not a user's web request.
For example, R4 reports that it connects to LAN B and to the other routers. Its router LSA describes its own links and their costs. Other LSA types describe shared LANs or networks reached through another area.
Read an example of R4's link information
An LSA describes the network; SPF calculates the route.
Flooding: pass the information to other routers
R4 creates an LSA and sends it to the routers with which it has an adjacency. They pass it on where OSPF requires. This controlled sharing is called flooding. It gives the routers in the area the information they need about R4's links, even if they do not connect directly to R4.
OSPF uses a sequence number to tell newer versions of a record from older ones. Routers confirm receipt and send records again when needed. This is how OSPF keeps its information up to date without treating every received copy as a new change.
Follow an update across the example network
- Destination subnet
- 192.168.4.0/24
- Next hop · router interface IP
- 10.0.12.2 · R2
- Forwarding path
- R1 → R2 → R4
- OSPF cost
- 3
- R4’s link information · interface IPv4
- R4 Gi0/1: 10.0.24.2/30 → R2; Gi0/2: 10.0.34.2/30 → R3; Gi0/0: 192.168.4.1/24 → LAN B.
- R1’s possible next hops
- Gi0/1: 10.0.12.1/30 → R2 at 10.0.12.2; Gi0/2: 10.0.13.1/30 → R3 at 10.0.13.2. RIDs in the OSPF views: R1 1.1.1.1, R2 2.2.2.2, R3 3.3.3.3, R4 4.4.4.4.
- LAN gateways
- PC-A uses R1 Gi0/0 at 192.168.1.1; the server uses R4 Gi0/0 at 192.168.4.1. Both endpoint masks are /24 (255.255.255.0). Transit links are /30 (255.255.255.252).
- Routing example
- OSPF area 0; link costs are configured metrics, not measured speed. Dashed arrows carry link-state information; a solid coloured path carries the user’s request.
The LSDB and SPF: same map, different starting points
Each router stores OSPF records in its link-state database (LSDB). Think of it as the network information used to build a map. Once information has finished spreading, routers in the same area agree on that area's topology records.
Shortest path first (SPF) is the calculation that finds the lowest-cost paths from that map. Each router starts the calculation from its own position. R1 and R4 can have the same map but choose different next hops because they start in different places.
The routing table is the list used to choose where packets go. OSPF offers its calculated routes to this table. A route from another source can be preferred, so an LSA in the database does not automatically mean an OSPF route is installed.
Compare the database with the routes each router chooses
Cost: how paths are compared
Lowest total outgoing cost wins. Cisco derives cost from configured interface bandwidth, or you can override it with ip ospf cost.
cost = reference bandwidth ÷ interface bandwidth, with a default reference bandwidth of 100 Mbps and a minimum cost of 1.
10 Mbps → 100 ÷ 10 = 10 · 100 Mbps → 1 · 1 Gbps → 0.1, rounded up to 1.
Cost is a number used to compare OSPF paths, not a measurement of the time a packet takes to arrive. Add the costs of the outgoing interfaces along a path, including the interface towards the destination network. Compare paths to the same network and of the same OSPF route type; the lower total is preferred.
Compare the two path costs
R1#show ip route ospf O 192.168.4.0/24 [110/3] via 10.0.12.2, 00:02:11, GigabitEthernet0/1
You can set an explicit interface cost with ip ospf cost. Networks built on gigabit and faster links usually raise the reference bandwidth, and they do it on every router. Left at 100 Mbps, a 1 Gbps path and a 10 Gbps path look exactly the same to SPF.
Areas: keeping the map manageable
An area is a part of an OSPF network. Routers share detailed information about links inside that area. Splitting a large network into areas limits how far those details must spread.
Area 0 is the backbone used to connect other areas. A router connected to more than one area is an area border router (ABR). It shares reachability information between areas; it does not copy every internal link detail from one area into the other.
See a network split into areas
Keep the distinction: same topology map, different starting router. Each router calculates its own next hops and costs. A cost describes the route; it does not measure actual download speed.
Try it: follow the information into a route
On R1 in the diagram's configured OSPF lab, use privileged EXEC mode, the # prompt. Check neighbours first, stored records next, then the installed path to the server:
show ip ospf neighbor
show ip ospf database
show ip route 192.168.4.10R1 verification sequence. These inspect existing state; they do not create an OSPF configuration.
The neighbour output shows R1's direct OSPF peers. The database contains information received about the wider network. The route lookup should select R2 at 10.0.12.2 with cost 3 in the illustrated baseline. R4 can appear in the database without being R1's direct neighbour.
Test yourself on link-state basics
A router has no router-id command, a loopback 10.255.0.1 (up), and interfaces 192.168.1.1 and 10.0.12.1 (both up). What is its OSPF router ID?
What does a router normally do with a newer, valid area-scoped LSA received from an adjacent neighbour?
All routers in an area have identical LSDBs. Do they all have identical routing tables?
With the default reference bandwidth (100 Mbps), what is the OSPF cost of a 10 Mbps Ethernet interface?
R1's routing table shows O 192.168.4.0/24 [110/3] via 10.0.12.2. What is 3?
Further reading: Cisco: interpreting OSPF interface output and RFC 2328: OSPFv2.