Skip to content

Router Lab II: OSPF and Path Costs

Introduction

You can find materials for Lab II in the ospf directory. In this lab, we need to connect 4 routers in the same AS using the OSPF protocol.

Note

We use OSPF version 2 defined by RFC 2328 in this lab. You need to refer to the OSPFv2 document. OSPFv3 is defined for the IPv6 network and is not covered by this lab. You can find more information about OSPFv3 in RFC 5340.

topology-lab1

Lab 2 Target Network Topology

OSPF Configurations

OSPF Routing

Deploy the lab environment using the following command:

Bash
sudo containerlab deploy -t start.clab.yaml --reconfigure

We can check that four containers are successfully started and network is properly configured.

Bash Session
1
2
3
4
5
6
❯ docker ps
CONTAINER ID   IMAGE              COMMAND                  CREATED          STATUS          PORTS     NAMES
1a4b4b5e367f   ceos:4.28.3        "bash -c '/mnt/flash…"   56 seconds ago   Up 55 seconds             R3
f658786bc521   ceos:4.28.3        "bash -c '/mnt/flash…"   56 seconds ago   Up 55 seconds             R4
cf0f039c9682   ceos:4.28.3        "bash -c '/mnt/flash…"   56 seconds ago   Up 55 seconds             R2
872ca28f383a   ceos:4.28.3        "bash -c '/mnt/flash…"   56 seconds ago   Up 55 seconds             R1

Checking the routing table before any configurations, we can find that each router contains only connected routes as expected.

Text Only
R1>show ip route

VRF: default
Codes: C - connected, S - static, K - kernel, 
    O - OSPF, IA - OSPF inter area, E1 - OSPF external type 1,
    E2 - OSPF external type 2, N1 - OSPF NSSA external type 1,
    N2 - OSPF NSSA external type2, B - Other BGP Routes,
    B I - iBGP, B E - eBGP, R - RIP, I L1 - IS-IS level 1,
    I L2 - IS-IS level 2, O3 - OSPFv3, A B - BGP Aggregate,
    A O - OSPF Summary, NG - Nexthop Group Static Route,
    V - VXLAN Control Service, M - Martian,
    DH - DHCP client installed default route,
    DP - Dynamic Policy Route, L - VRF Leaked,
    G  - gRIBI, RC - Route Cache Route

Gateway of last resort is not set

C        1.1.1.0/24 is directly connected, Loopback0
C        10.1.1.0/24 is directly connected, Ethernet2
C        10.1.4.0/24 is directly connected, Ethernet1
Text Only
R2>show ip route

VRF: default
Codes: C - connected, S - static, K - kernel, 
    O - OSPF, IA - OSPF inter area, E1 - OSPF external type 1,
    E2 - OSPF external type 2, N1 - OSPF NSSA external type 1,
    N2 - OSPF NSSA external type2, B - Other BGP Routes,
    B I - iBGP, B E - eBGP, R - RIP, I L1 - IS-IS level 1,
    I L2 - IS-IS level 2, O3 - OSPFv3, A B - BGP Aggregate,
    A O - OSPF Summary, NG - Nexthop Group Static Route,
    V - VXLAN Control Service, M - Martian,
    DH - DHCP client installed default route,
    DP - Dynamic Policy Route, L - VRF Leaked,
    G  - gRIBI, RC - Route Cache Route

Gateway of last resort is not set

C        2.2.2.0/24 is directly connected, Loopback0
C        10.1.1.0/24 is directly connected, Ethernet1
C        10.1.2.0/24 is directly connected, Ethernet2
Text Only
R3>show ip route

VRF: default
Codes: C - connected, S - static, K - kernel, 
    O - OSPF, IA - OSPF inter area, E1 - OSPF external type 1,
    E2 - OSPF external type 2, N1 - OSPF NSSA external type 1,
    N2 - OSPF NSSA external type2, B - Other BGP Routes,
    B I - iBGP, B E - eBGP, R - RIP, I L1 - IS-IS level 1,
    I L2 - IS-IS level 2, O3 - OSPFv3, A B - BGP Aggregate,
    A O - OSPF Summary, NG - Nexthop Group Static Route,
    V - VXLAN Control Service, M - Martian,
    DH - DHCP client installed default route,
    DP - Dynamic Policy Route, L - VRF Leaked,
    G  - gRIBI, RC - Route Cache Route

Gateway of last resort is not set

C        3.3.3.0/24 is directly connected, Loopback0
C        10.1.3.0/24 is directly connected, Ethernet2
C        10.1.4.0/24 is directly connected, Ethernet1
Text Only
R4>show ip route

VRF: default
Codes: C - connected, S - static, K - kernel, 
    O - OSPF, IA - OSPF inter area, E1 - OSPF external type 1,
    E2 - OSPF external type 2, N1 - OSPF NSSA external type 1,
    N2 - OSPF NSSA external type2, B - Other BGP Routes,
    B I - iBGP, B E - eBGP, R - RIP, I L1 - IS-IS level 1,
    I L2 - IS-IS level 2, O3 - OSPFv3, A B - BGP Aggregate,
    A O - OSPF Summary, NG - Nexthop Group Static Route,
    V - VXLAN Control Service, M - Martian,
    DH - DHCP client installed default route,
    DP - Dynamic Policy Route, L - VRF Leaked,
    G  - gRIBI, RC - Route Cache Route

Gateway of last resort is not set

C        4.4.4.0/24 is directly connected, Loopback0
C        10.1.2.0/24 is directly connected, Ethernet1
C        10.1.3.0/24 is directly connected, Ethernet2

Let's start the configuration from R1 and R2. Use the router ospf <process_id> command to create an OSPFv2 process and enter its configuration mode.

Bash Session
1
2
3
4
R1>enable
R1#configure
R1(config)#router ospf 1
R1(config-router-ospf)#
Bash Session
1
2
3
4
R2>enable
R2#configure
R2(config)#router ospf 1
R2(config-router-ospf)#

Different from BGP which uses TCP as its transport protocol, OSPF delivers data using raw IP packets. As a consequence, instead of specifying the addresses of neighbors, we need to assign local addresses to OSPF areas within which OSPF advertises routes. We use one single area in this lab, but in practice areas are important to reduce the size of the advertised message and ensure in-time convergence of the underlying link-state algorithm.

Bash Session
R1(config-router-ospf)#network 10.1.1.1/32 area 0.0.0.0
Bash Session
R2(config-router-ospf)#network 10.1.1.2/32 area 0.0.0.0    

We can learn the current states of the OSPF protocol using commands show ip ospf summary, show ip ospf database and show ip ospf neighbor.

  • show ip ospf summary gives a summary of areas and received LSA (Link State Aggregation) packets.

    Bash Session
    1
    2
    3
    4
    5
    6
    7
    R1(config-router-ospf)#show ip ospf summary
    OSPF instance 1 with ID 1.1.1.1, VRF default
    Time since last SPF: 249 s
    Max LSAs: 12000, Total LSAs: 3
    Type-5 Ext LSAs: 0
    ID               Type   Intf   Nbrs (full) RTR LSA NW LSA  SUM LSA ASBR LSA TYPE-7 LSA
    0.0.0.0          normal 1      1    (1   ) 2       1       0       0       0     
    

  • show ip ospf neighbor list all active OSPF neighbors

    Bash Session
    1
    2
    3
    R1(config-router-ospf)#show ip ospf neighbor
    Neighbor ID     Instance VRF      Pri State                  Dead Time   Address         Interface
    2.2.2.2         1        default  1   FULL/DR                00:00:34    10.1.1.2        Ethernet2
    

  • show ip ospf database gives internal details of the link state routing protocols.

    Bash Session
    R1(config-router-ospf)#show ip ospf database
    
                OSPF Router with ID(1.1.1.1) (Instance ID 1) (VRF default)
    
    
                     Router Link States (Area 0.0.0.0)
    
    Link ID         ADV Router      Age         Seq#         Checksum Link count
    2.2.2.2         2.2.2.2         380         0x80000003   0x1fa    1
    1.1.1.1         1.1.1.1         379         0x80000004   0x3dc6   1
    
                     Network Link States (Area 0.0.0.0)
    
    Link ID         ADV Router      Age         Seq#         Checksum
    10.1.1.2        2.2.2.2         380         0x80000001   0x2ceb  
    

We need to configure OSPF to distribute routing to their local interface using redistribute connected.

Bash Session
R1(config-router-ospf)#redistribute connected
Bash Session
R2(config-router-ospf)#redistribute connected

Check the routing table again, we find that addresses of local interfaces are successfully advertised to OSPF neighbors.

Text Only
R1(config-router-ospf)#show ip route

VRF: default
Codes: C - connected, S - static, K - kernel, 
    O - OSPF, IA - OSPF inter area, E1 - OSPF external type 1,
    E2 - OSPF external type 2, N1 - OSPF NSSA external type 1,
    N2 - OSPF NSSA external type2, B - Other BGP Routes,
    B I - iBGP, B E - eBGP, R - RIP, I L1 - IS-IS level 1,
    I L2 - IS-IS level 2, O3 - OSPFv3, A B - BGP Aggregate,
    A O - OSPF Summary, NG - Nexthop Group Static Route,
    V - VXLAN Control Service, M - Martian,
    DH - DHCP client installed default route,
    DP - Dynamic Policy Route, L - VRF Leaked,
    G  - gRIBI, RC - Route Cache Route

Gateway of last resort is not set

C        1.1.1.0/24 is directly connected, Loopback0
O E2     2.2.2.0/24 [110/1] via 10.1.1.2, Ethernet2
C        10.1.1.0/24 is directly connected, Ethernet2
O E2     10.1.2.0/24 [110/1] via 10.1.1.2, Ethernet2
C        10.1.4.0/24 is directly connected, Ethernet1
Text Only
R2#show ip route

VRF: default
Codes: C - connected, S - static, K - kernel, 
    O - OSPF, IA - OSPF inter area, E1 - OSPF external type 1,
    E2 - OSPF external type 2, N1 - OSPF NSSA external type 1,
    N2 - OSPF NSSA external type2, B - Other BGP Routes,
    B I - iBGP, B E - eBGP, R - RIP, I L1 - IS-IS level 1,
    I L2 - IS-IS level 2, O3 - OSPFv3, A B - BGP Aggregate,
    A O - OSPF Summary, NG - Nexthop Group Static Route,
    V - VXLAN Control Service, M - Martian,
    DH - DHCP client installed default route,
    DP - Dynamic Policy Route, L - VRF Leaked,
    G  - gRIBI, RC - Route Cache Route

Gateway of last resort is not set

O E2     1.1.1.0/24 [110/1] via 10.1.1.1, Ethernet1
C        2.2.2.0/24 is directly connected, Loopback0
C        10.1.1.0/24 is directly connected, Ethernet1
C        10.1.2.0/24 is directly connected, Ethernet2
O E2     10.1.4.0/24 [110/1] via 10.1.1.1, Ethernet1

The two new routes are learned from OSPF (marked as O) and are external type 2 routes (marked as E2).

Note

If you are curious about the difference between the external type 1 (E1) route and external type 2 route (E2). When calculating the cost of a route, type 2 routes ignore the costs of all internal links while type 1 does not.

Note that 10.1.2.0/24 subnet is incorrectly regarded as an external route by R1 because we have not assigned them to OSPF area 0.

Text Only
R1(config-router-ospf)#network 10.1.4.0/24 area 0
Text Only
R2(config-router-ospf)#network 10.1.2.0/24 area 0

Now they are OSPF internal routes as shown below.

Text Only
R1(config-router-ospf)#show ip route

VRF: default
Codes: C - connected, S - static, K - kernel, 
    O - OSPF, IA - OSPF inter area, E1 - OSPF external type 1,
    E2 - OSPF external type 2, N1 - OSPF NSSA external type 1,
    N2 - OSPF NSSA external type2, B - Other BGP Routes,
    B I - iBGP, B E - eBGP, R - RIP, I L1 - IS-IS level 1,
    I L2 - IS-IS level 2, O3 - OSPFv3, A B - BGP Aggregate,
    A O - OSPF Summary, NG - Nexthop Group Static Route,
    V - VXLAN Control Service, M - Martian,
    DH - DHCP client installed default route,
    DP - Dynamic Policy Route, L - VRF Leaked,
    G  - gRIBI, RC - Route Cache Route

Gateway of last resort is not set

C        1.1.1.0/24 is directly connected, Loopback0
O E2     2.2.2.0/24 [110/1] via 10.1.1.2, Ethernet2
C        10.1.1.0/24 is directly connected, Ethernet2
O        10.1.2.0/24 [110/20] via 10.1.1.2, Ethernet2
C        10.1.4.0/24 is directly connected, Ethernet1
Text Only
R2(config-router-ospf)#show ip route

VRF: default
Codes: C - connected, S - static, K - kernel, 
    O - OSPF, IA - OSPF inter area, E1 - OSPF external type 1,
    E2 - OSPF external type 2, N1 - OSPF NSSA external type 1,
    N2 - OSPF NSSA external type2, B - Other BGP Routes,
    B I - iBGP, B E - eBGP, R - RIP, I L1 - IS-IS level 1,
    I L2 - IS-IS level 2, O3 - OSPFv3, A B - BGP Aggregate,
    A O - OSPF Summary, NG - Nexthop Group Static Route,
    V - VXLAN Control Service, M - Martian,
    DH - DHCP client installed default route,
    DP - Dynamic Policy Route, L - VRF Leaked,
    G  - gRIBI, RC - Route Cache Route

Gateway of last resort is not set

O E2     1.1.1.0/24 [110/1] via 10.1.1.1, Ethernet1
C        2.2.2.0/24 is directly connected, Loopback0
C        10.1.1.0/24 is directly connected, Ethernet1
C        10.1.2.0/24 is directly connected, Ethernet2
O        10.1.4.0/24 [110/20] via 10.1.1.1, Ethernet1

We can configure R3 and R4 following identical steps.

Text Only
1
2
3
4
R3(config)#router ospf 1
R3(config-router-ospf)#network 10.1.4.3/24 area 0.0.0.0
R3(config-router-ospf)#network 10.1.3.3/24 area 0.0.0.0
R3(config-router-ospf)#redistribute connected
Text Only
1
2
3
4
R4(config)#router ospf 1
R4(config-router-ospf)#network 10.1.2.4/24 area 0.0.0.0
R4(config-router-ospf)#network 10.1.3.4/24 area 0.0.0.0
R4(config-router-ospf)#redistribute connected

Now we can see that all routers have learned all the routes. Take the routing table of R1 as an example.

Text Only
R1>show ip route

VRF: default
Codes: C - connected, S - static, K - kernel, 
    O - OSPF, IA - OSPF inter area, E1 - OSPF external type 1,
    E2 - OSPF external type 2, N1 - OSPF NSSA external type 1,
    N2 - OSPF NSSA external type2, B - Other BGP Routes,
    B I - iBGP, B E - eBGP, R - RIP, I L1 - IS-IS level 1,
    I L2 - IS-IS level 2, O3 - OSPFv3, A B - BGP Aggregate,
    A O - OSPF Summary, NG - Nexthop Group Static Route,
    V - VXLAN Control Service, M - Martian,
    DH - DHCP client installed default route,
    DP - Dynamic Policy Route, L - VRF Leaked,
    G  - gRIBI, RC - Route Cache Route

Gateway of last resort is not set

C        1.1.1.0/24 is directly connected, Loopback0
O E2     2.2.2.0/24 [110/1] via 10.1.1.2, Ethernet2
O E2     3.3.3.0/24 [110/1] via 10.1.4.3, Ethernet1
O E2     4.4.4.0/24 [110/1] via 10.1.4.3, Ethernet1
                            via 10.1.1.2, Ethernet2
C        10.1.1.0/24 is directly connected, Ethernet2
O        10.1.2.0/24 [110/20] via 10.1.1.2, Ethernet2
O        10.1.3.0/24 [110/20] via 10.1.4.3, Ethernet1

It's interesting to find that there are two ways to reach R4 from R1. Which one will be used? We can find out by using traceroute command.

Bash Session
1
2
3
4
R1>traceroute 4.4.4.4
traceroute to 4.4.4.4 (4.4.4.4), 30 hops max, 60 byte packets
 1  10.1.1.2 (10.1.1.2)  0.055 ms  0.016 ms  0.018 ms
 2  4.4.4.4 (4.4.4.4)  0.051 ms  0.029 ms  0.031 ms

We will learn how to tune the routing table using path costs in the next section.

Path Cost

OSPF prefers a path with lower "cost". The cost of a path is the sum of the costs of all links it traverses. Generally speaking, link with larger capacity has lower cost. When all links have an identical bandwidth, paths with the least hops win. We can change the cost of an interface manually using the command ip ospf cost to tune the routing decision.

Let's change the path cost of Ethernet2 on R1 to 100, and see what will happen.

Bash Session
R1(config)#interface Ethernet2
R1(config-if-Et2)#ip ospf cost 100

Check the routing table of R1 again, we will find that there is only one route to R4 now. The default cost of a 1000Mbps link is 0.01 by default. So the path R1 -> R3 -> R4 has a much lower cost than R1 -> R2 -> R4 now, making OSPF prefers the former one.

Text Only
R1>show ip route

VRF: default
Codes: C - connected, S - static, K - kernel, 
       O - OSPF, IA - OSPF inter area, E1 - OSPF external type 1,
       E2 - OSPF external type 2, N1 - OSPF NSSA external type 1,
       N2 - OSPF NSSA external type2, B - Other BGP Routes,
       B I - iBGP, B E - eBGP, R - RIP, I L1 - IS-IS level 1,
       I L2 - IS-IS level 2, O3 - OSPFv3, A B - BGP Aggregate,
       A O - OSPF Summary, NG - Nexthop Group Static Route,
       V - VXLAN Control Service, M - Martian,
       DH - DHCP client installed default route,
       DP - Dynamic Policy Route, L - VRF Leaked,
       G  - gRIBI, RC - Route Cache Route

Gateway of last resort is not set

 C        1.1.1.0/24 is directly connected, Loopback0
 O E2     2.2.2.0/24 [110/1] via 10.1.4.3, Ethernet1
 O E2     3.3.3.0/24 [110/1] via 10.1.4.3, Ethernet1
 O E2     4.4.4.0/24 [110/1] via 10.1.4.3, Ethernet1
 C        10.1.1.0/24 is directly connected, Ethernet2
 O        10.1.2.0/24 [110/30] via 10.1.4.3, Ethernet1
 O        10.1.3.0/24 [110/20] via 10.1.4.3, Ethernet1
 C        10.1.4.0/24 is directly connected, Ethernet1

Similarly, R3 now prefers R4 as the intermediate router to reach R2. But what happened to R4? Nothing changes indeed. We will find that it still has two routes to R1.

Text Only
R4>show ip route

VRF: default
Codes: C - connected, S - static, K - kernel, 
       O - OSPF, IA - OSPF inter area, E1 - OSPF external type 1,
       E2 - OSPF external type 2, N1 - OSPF NSSA external type 1,
       N2 - OSPF NSSA external type2, B - Other BGP Routes,
       B I - iBGP, B E - eBGP, R - RIP, I L1 - IS-IS level 1,
       I L2 - IS-IS level 2, O3 - OSPFv3, A B - BGP Aggregate,
       A O - OSPF Summary, NG - Nexthop Group Static Route,
       V - VXLAN Control Service, M - Martian,
       DH - DHCP client installed default route,
       DP - Dynamic Policy Route, L - VRF Leaked,
       G  - gRIBI, RC - Route Cache Route

Gateway of last resort is not set

 O E2     1.1.1.0/24 [110/1] via 10.1.2.2, Ethernet1
                             via 10.1.3.3, Ethernet2
 O E2     2.2.2.0/24 [110/1] via 10.1.2.2, Ethernet1
 O E2     3.3.3.0/24 [110/1] via 10.1.3.3, Ethernet2
 C        4.4.4.0/24 is directly connected, Loopback0
 O        10.1.1.0/24 [110/20] via 10.1.2.2, Ethernet1
 C        10.1.2.0/24 is directly connected, Ethernet1
 C        10.1.3.0/24 is directly connected, Ethernet2
 O        10.1.4.0/24 [110/20] via 10.1.3.3, Ethernet2

So remember that path cost is unidirectional. It is not symmetric. When we change the link cost from R1 to R2, the link cost from R2 to R1 is not changed.

With careful planning, we can enforce desired routing rules with implicit and distributed path costs. It partially explains why OSPF is only applied as an intra-AS routing protocol. When the scale of the network increases, it is hard to manage the routing table in such a distributed manner.

Configuration Review

Bash Session
R1>enable
R1#configure
R1(config)#interface Ethernet2
R1(config-if-Et2)#ip ospf cost 100
R1(config-if-Et2)#exit
R1(config)#router ospf 1
R1(config-router-ospf)#network 10.1.1.1/24 area 0.0.0.0
R1(config-router-ospf)#network 10.1.4.1/24 area 0.0.0.0
R1(config-router-ospf)#redistribute connected
R1(config-router-ospf)#exit
R1(config)#exit
R1#exit
Bash Session
1
2
3
4
5
6
7
8
9
R2>enable
R2#configure
R2(config)#router ospf 1
R2(config-router-ospf)#network 10.1.1.2/24 area 0.0.0.0
R2(config-router-ospf)#network 10.1.2.2/24 area 0.0.0.0
R2(config-router-ospf)#redistribute connected
R2(config-router-ospf)#exit
R2(config)#exit
R2#exit
Bash Session
1
2
3
4
5
6
7
8
9
R3>enable
R3#configure
R3(config)#router ospf 1
R3(config-router-ospf)#network 10.1.4.3/24 area 0.0.0.0
R3(config-router-ospf)#network 10.1.3.3/24 area 0.0.0.0
R3(config-router-ospf)#redistribute connected
R3(config-router-ospf)#exit
R3(config)#exit
R3#exit
Bash Session
1
2
3
4
5
6
7
8
9
R4>enable
R4#configure
R4(config)#router ospf 1
R4(config-router-ospf)#network 10.1.2.4/24 area 0.0.0.0
R4(config-router-ospf)#network 10.1.3.4/24 area 0.0.0.0
R4(config-router-ospf)#redistribute connected
R4(config-router-ospf)#exit
R4(config)#exit
R4#exit

Test

Once the configuration is done, we can test the connectivity between the four routers using the script test.py in the ospy directory.

Bash Session
1
2
3
4
5
6
7
8
❯ ./test.py
R1 -> R1: True R2: True R3: True R4: True 
R2 -> R1: True R2: True R3: True R4: True 
R3 -> R1: True R2: True R3: True R4: True 
R4 -> R1: True R2: True R3: True R4: True 
Pass Connectivity Test
Path R1 -> R4: 1 -> 3 -> 4
Pass Path Test

OSPF Under the Curtain (Not Completed Yet)