Understanding OSPF Concepts

A static route stays exactly as typed; a routing protocol lets routers tell each other about subnets and choose new routes when something changes. OSPF is an interior gateway protocol, used inside one autonomous system, and a link-state protocol. It picks the route with the lowest cost: the sum of the interface costs along the path, based by default on bandwidth.

Free to read12 min read · 12 min video

By , founder of Daveth · Updated

Opening frame of the video walkthrough of Understanding OSPF Concepts

Watch this lesson

12 min · Video walkthrough

In Daveth Pro (Network)

The full 12 min walkthrough is in Daveth Pro (Network). See what the plan opens

What this page answers

What is OSPF (Open Shortest Path First)?

A link-state interior gateway protocol: routers inside one organisation's network share information about the links, each works out its own best routes, and the best route is the one with the lowest total cost, based by default on interface bandwidth. Read it in “Three families of IGP”

What is the difference between an IGP and an EGP?

An interior gateway protocol is designed for use inside a single autonomous system, a network under one organisation's control; an exterior gateway protocol is designed for use between autonomous systems. Read it in “Inside the company, and between companies”

What metric does OSPF use?

Cost: the sum of the interface costs of all the links in a route, each based by default on the interface's bandwidth. RIPv2 counts hops; EIGRP uses bandwidth and delay. Read it in “Hops or cost”

Typed routes don't notice

Monday, month three, week three. After Friday's missing route, Maya counts the ip route lines the company's routers now depend on: three on BR1 (one of them the floating backup), one on R1, two on R3. Every new subnet means more of them, typed on every router that needs to reach it, and each one stays exactly as typed until somebody changes it.

A paper map doesn't phone every driver when a road closes.

A router adds routes to its routing table in three ways: connected routes, static routes, and routes learned with a dynamic routing protocol (05.1.2). Ravi suggests the third. Two terms sound alike here and mean different things:

  • A routing protocol is a set of messages, rules and algorithms that routers use to learn routes: they exchange and analyse routing information, each chooses the best route to each subnet (path selection), and puts those best routes in its routing table. RIP, EIGRP, OSPF and BGP are routing protocols.
  • A routed protocol, or routable protocol, defines a packet structure and logical addressing, so routers can forward its packets. IPv4 and IPv6 are routed protocols.

They work closely together. The routing process forwards IP packets, and a router with no route matching a packet's destination discards it; the routing protocol is how the router learns the routes in the first place. Routers support several IP routing protocols, and they all perform the same general functions:

  1. Learn routing information about IP subnets from neighbouring routers.
  2. Advertise routing information about IP subnets to neighbouring routers.
  3. If more than one route to a subnet exists, pick the best one, based on a metric: a number each protocol gives a route to score how good it is, and the lowest wins.
  4. If the topology changes, say a link fails, advertise that some routes have failed and pick a new currently best route. This is convergence.

A neighbouring router is one connected to the same link as another router: the same WAN link, or the same Ethernet LAN. BR1 has two neighbours, R1 over the fibre and R3 over the backup line. Convergence is all the routers together realising something changed, telling every other router about it, and each choosing its currently best routes again. How quickly a protocol converges, without causing loops, is one of the most important things to weigh when choosing one.

Would a routing protocol have saved Friday? R3's route back to the branch would have been learned from its neighbours rather than typed as one line that a tidy-up could delete. A routing protocol can be set up wrongly too, of course; what it removes is the typing of every route by hand, and the waiting for a person when a link fails.

Inside the company, and between companies

IP routing protocols come in two categories. An interior gateway protocol (IGP) was designed for use inside a single autonomous system (AS); an exterior gateway protocol (EGP) was designed for use between different autonomous systems. (Both names say gateway because routers used to be called gateways.) An AS is a network under the administrative control of one organisation: a network one company builds and pays for is probably one AS, as is a school system's network, or a large government department's. Each ISP is also typically its own AS.

So Maya's company is one AS, and its ISP is another. Inside, the company's routers would run an IGP. Between organisations, today's only EGP in use is Border Gateway Protocol (BGP). Out on the Internet, enterprises and ISPs each run an IGP such as OSPF or EIGRP inside their own networks, and BGP between them.

An IGP works kind of like a company's internal mail room, which knows every desk in the building, and BGP like the courier service between companies. (Where it stops: a mail room carries the letters; routing protocols only swap routes, and the routed packets themselves are forwarded by each router's routing table.)

Each AS can have an AS number (ASN). Like public IP addresses, ASNs are controlled worldwide by the Internet Assigned Numbers Authority (IANA), which delegates the work to organisations around the world, usually the same ones that assign public addresses; in North America, that's the American Registry for Internet Numbers (ARIN).

Three families of IGP

A routing protocol's algorithm is the logic it uses to learn all the routes, choose the best route to each subnet, and converge when something changes. IGPs come in three main branches:

  • Distance vector, sometimes called Bellman-Ford after its creators. These came first, mainly in the early 1980s: RIP was the first widely used one for IP, and IGRP, one vendor's own protocol, followed a little later.
  • Link-state. By the early 1990s, distance vector's somewhat slow convergence and potential for routing loops drove new designs. OSPF (Open Shortest Path First) and Integrated IS-IS (Intermediate System to Intermediate System) solved the main problems, at a price: extra CPU and memory on the routers, and more planning from the engineers.
  • Advanced distance vector, sometimes called balanced hybrid. EIGRP, one vendor's own protocol from around the same time as OSPF, reused some features of the earlier IGRP and solved the same problems as link-state, with less planning. It uses more distance vector features than link-state ones, so it's usually filed here.

Most companies today run OSPF or EIGRP. This course's IPv4 routing protocol is OSPF version 2 (OSPFv2); every "OSPF" on this page means OSPFv2.

Hops or cost

A routing protocol picks the route with the lowest metric, and the IGPs measure different things:

IGPMetricWhat it measures
RIPv2Hop countThe number of routers (hops) between a router and the destination subnet
OSPFCostThe sum of the interface costs for all the links in a route; by default each cost is based on the interface's bandwidth
EIGRPBased on bandwidth and delayCalculated from the route's slowest link and the delay added up along every interface in the route

BR1 has two ways to head office's Staff LAN, 10.100.0.0/24, and they're very different. Today's new detail is the link speeds: the fibre and the R1-R3 link run at 1 Gbps, the backup line at 10 Mbps.

Network drawing: fibre: 1 Gbps, backup line: 10 Mbps, R1 to R3: 1 Gbps, Staff LAN 10.100.0.0/24, R3, Printer, BR1, R1, Gi0/1/0, Gi0/0

BR1's two paths to the Staff LAN: over the fibre to R1 and on to R3 (1 Gbps links), or over the backup line straight to R3 (10 Mbps).

That's hop count's weak spot: fewer routers isn't the same as faster. OSPF's cost can see the difference. For this page, take these example costs: 1 on each 1 Gbps link, 10 on the backup line, and 1 for R3's link to the Staff LAN. (How a router works out an interface's cost by default comes in a later OSPF topic.)

With costs that match the real link speeds, OSPF chooses the faster route, even though it passes through one more router. The cost defaults come from each interface's bandwidth setting, and setting it with the bandwidth interface subcommand doesn't change the link's physical speed at all: it only tells the router what speed to assume.

The route chooser: BR1 to 10.100.0.0/24

PathMetricResult
over the fibre, via R1 then R32 (R1, R3)usable, higher metric
over the backup line, straight to R31 (R3)lowest: BR1 uses this one

By hop count, BR1 sends packets for 10.100.0.0/24 over the backup line, straight to R3.

Fewer hops, slower trip: RIP would happily send the whole branch down the slow line.

Comparing the IGPs

FeatureRIPv2EIGRPOSPF
Classless: sends the mask in its updates, supports VLSMYesYesYes
AlgorithmDistance vectorAdvanced distance vectorLink-state
Supports manual summarizationYesYesYes
One vendor's own protocolNoYes (published as an informational RFC in 2013, so other vendors can implement it; the original vendor keeps the rights)No
Sends its updates to a multicast IP addressYesYesYes
ConvergenceSlowFastFast

Look at the first row. A classless routing protocol sends the subnet mask with each route in its messages, so it supports VLSM and manual route summarization (04.5, 04.7). The older classful protocols, RIPv1 and IGRP, send no masks, so they don't. The older ones also sent their messages to IP broadcast addresses; the newer ones all use multicast destination addresses. That's more efficient, and it causes fewer problems for the devices on the subnet that don't run the protocol.

And sometimes two routing protocols run at once: say two companies join their networks, one running OSPF and the other EIGRP. A router running both can take routes learned by one and advertise them into the other, route redistribution. Now two protocols may offer routes to the same subnet, with metrics that can't be compared: an OSPF metric of 101 against an EIGRP metric of 2,195,416 says nothing about which route is better. That's the job of administrative distance (05.1.2): RIP 120, OSPF 110, EIGRP 90 by default, lowest most believable, set on each router and never sent to others; it's the first number in a route's brackets in show ip route, and the metric is the second. It can be changed for a whole routing protocol, for one route, or for a static route, which is why BR1's floating default route works: with administrative distance 200, it stays out of the table whenever a better-believed source has a route to 0.0.0.0/0.

101 against 2,195,416: one looks tiny, one looks huge, and neither means a thing to the other protocol.

How OSPF learns

Link-state protocols build routes in two big steps. First the routers together gather lots of information about the network (routers, links, IP addresses, status) and flood it, so every router knows the same things. Then each router calculates routes to every subnet, from its own point of view.

OSPF, the most popular link-state IP routing protocol, organises that information as link-state advertisements (LSAs): each LSA is a data structure holding some specific information about the topology. The link-state database (LSDB) is the collection of all the LSAs a router knows. A router creates an LSA describing itself (for each interface, things like its address and mask, whether it's up, and its cost) and floods it: every router passes it on until every router has a copy. To stop LSAs going round in circles, a router asks a neighbour, in effect, "Do you already have this LSA?", and sends it only if the neighbour doesn't. Compared with distance vector, it's a lot of work.

Routers also reflood each LSA from time to time: when some information changes, like a link going up or down, and otherwise when that LSA's own aging timer runs out (30 minutes by default). You can think of it as every router in the company holding the same printed map, reprinted and handed round whenever a road opens or closes. (Where it stops: a printed map comes from one mapmaker; every router adds its own piece, and the copies travel neighbour to neighbour.)

The flooding gives every router an identical LSDB, and the LSDB doesn't state anyone's best route. To get routes, every link-state router runs the Dijkstra Shortest Path First (SPF) algorithm on its LSDB. SPF works out the routes the local router should add to its routing table: a subnet number and mask, an outgoing interface, and a next-hop address. Packets never ask OSPF anything; they're forwarded by the routing table, and OSPF's job is keeping that table's routes current.

Convergence isn't instant, either. Between a link failing and every router finishing its new calculation, packets already heading for the dead link can be lost. If you've ever had a sat-nav recalculate after a missed turn, it's the same short pause before the new route. (Where it stops: the sat-nav alone decides; here every router has to hear the news and rerun SPF, each on its own copy.)

OSPF does all this in three phases, and 05.2.2 takes each one apart:

  1. Becoming neighbours: two routers on the same data link form a relationship, so they have a way to exchange their LSDBs.
  2. Exchanging databases: they send each other LSAs, until all the routers know the same LSAs.
  3. Adding the best routes: each router independently runs SPF on its own copy of the LSDB, calculates the best routes, and adds them to its IPv4 routing table.

Five lines to keep

  1. A routing protocol learns routes; a routed protocol, such as IPv4, is what gets forwarded.
  2. The four jobs: learn, advertise, pick the lowest metric, converge.
  3. IGPs work inside one AS; BGP, the only EGP in use, works between them.
  4. RIP counts hops; OSPF adds up costs, by default based on bandwidth, so it can prefer a longer, faster path.
  5. OSPF floods LSAs into an identical LSDB on every router; each router runs SPF from its own position to choose its routes.

Next, 05.2.2: how two OSPF routers become neighbours, swap their databases, and turn costs into routes.

The interactive lesson

The whole lesson, with its drawings and its questions. Free with a Daveth account.

Open the lesson