Race Condition: IGP, BGP, and the Routing Information Base (RIB)

Same configuration, two different routing tables. Which one you get depends only on whether OSPF or eBGP reached the router first.

The Symptom

On a Cisco NX-OS switch, I saw BGP prefer a route it had redistributed locally from OSPF over the same prefix learned from an eBGP peer. After the route was cleared, the switch picked the eBGP path, which is the one I wanted. Nothing in the configuration changed between the two results.

This isn’t an NX-OS quirk. IOS and IOS XE apply the same rules, so the same thing can happen on any Cisco router that redistributes an IGP into BGP.

Why the Order Matters

A race condition is any situation where the outcome depends on the timing of events you don’t control. Here, the outcome depends on one thing: is the OSPF route already in the routing table (the URIB on NX-OS) when the eBGP update arrives?

Two rules make that timing matter.

  1. Redistribution reads the routing table. redistribute ospf 1 only picks up prefixes that OSPF currently owns in the URIB. If another protocol owns the prefix, there’s nothing to redistribute.
  2. Redistributed routes start with a big advantage in BGP. Cisco gives every locally originated path (redistributed, network, or aggregate) a weight of 32768. Paths learned from any peer get 0. Once BGP has confirmed a path is valid, weight is the first thing best-path compares.
Path Source Default Weight
Learned from an eBGP peer 0
Learned from an iBGP peer 0
Redistributed into BGP 32768
Originated with network or aggregate-address 32768

Weight is Cisco-specific and local to the router. It’s never sent to peers. It can be set from 0 to 65535, and higher is better.

Scenario 1: OSPF Arrives First

The OSPF route is installed in the URIB with AD 110. BGP redistributes it, creating a locally originated path with weight 32768. When the eBGP update arrives, its path has weight 0, so it loses at the very first best-path step. Because BGP’s best path is the redistributed one, BGP doesn’t offer anything new to the URIB, and the OSPF route stays.

Example output (trimmed):

R1# show bgp ipv4 unicast 10.10.0.0/16
BGP routing table entry for 10.10.0.0/16
Paths: (2 available, best #1)

  Path type: redist, path is valid, is best path
  AS-Path: NONE, path locally originated
    0.0.0.0 (metric 0) from 0.0.0.0 (10.0.0.1)
      Origin incomplete, MED 20, localpref 100, weight 32768

  Path type: external, path is valid, not best reason: Weight
  AS-Path: 65002
    192.0.2.1 (metric 0) from 192.0.2.1 (192.0.2.1)
      Origin IGP, localpref 100, weight 0

R1# show ip route 10.10.0.0/16
10.10.0.0/16, ubest/mbest: 1/0
    *via 10.1.1.2, Eth1/1, [110/20], ospf-1, intra

Scenario 2: eBGP Arrives First

This time the eBGP path is the only one in the BGP table, so it’s best. BGP installs it in the URIB with AD 20. When OSPF finishes SPF and offers its route, the URIB rejects it: 110 is worse than 20. Since OSPF doesn’t own the prefix in the URIB, there’s nothing to redistribute, and the redistributed path never appears.

Example output (trimmed):

R1# show bgp ipv4 unicast 10.10.0.0/16
BGP routing table entry for 10.10.0.0/16
Paths: (1 available, best #1)

  Path type: external, path is valid, is best path
  AS-Path: 65002
    192.0.2.1 (metric 0) from 192.0.2.1 (192.0.2.1)
      Origin IGP, localpref 100, weight 0

R1# show ip route 10.10.0.0/16
10.10.0.0/16, ubest/mbest: 1/0
    *via 192.0.2.1, [20/0], bgp-65001, external, tag 65002

Where Administrative Distance Fits In

BGP’s best-path algorithm never looks at administrative distance, and neither does OSPF’s SPF or EIGRP’s DUAL. AD is used only by the URIB, to choose between routes that different protocols offer for the same prefix. The race comes from a feedback dependency between the two: the URIB’s choice decides what BGP can redistribute, and BGP’s best-path choice decides whether it offers the eBGP route to the URIB at AD 20. The eBGP path’s AD only matters after it has won best-path.

Route Source Default AD
eBGP 20
EIGRP internal 90
OSPF 110
EIGRP external (entered EIGRP through redistribution) 170
iBGP 200
Locally originated BGP (NX-OS) 220

What It Costs You

The real damage is that two identically configured routers, or the same router before and after a reload, can forward the same prefix differently. For example, after a reload R1 could send traffic for 10.10.0.0/16 into the core instead of out to the ISP. Clearing the route “fixes” it, which makes it look like a one-off glitch. In practice, you get:

  • Unexpected or suboptimal paths that come and go with reloads, link flaps, or session resets.
  • Asymmetric routing between border routers, which can break stateful firewalls and makes troubleshooting slow.
  • Confusing troubleshooting. The configs match, but the tables don’t, and the problem disappears when you clear it.

Fixing It: Make the Winner Explicit

Timers and convergence tuning can change which protocol usually wins the race, but they can’t remove it: after the next reload or flap, the other order can still happen. A real fix produces the same best path whichever route arrives first.

The first question is which path you actually want. In my network, the eBGP path should own this prefix and OSPF should be the fallback.

A common mistake: setting set weight 0 on the redistribution route-map, or setting a modest weight such as 100 on the eBGP neighbor. Neither works. A weight of 100 still loses to 32768, and with both paths at 0 the redistributed path wins two steps later because it’s locally originated.

If the eBGP Path Should Win

Option A: raise the eBGP path’s weight above 32768 for that prefix. Scope it with a prefix list so other routes from the peer keep their normal behavior.

ip prefix-list EBGP-PREFERRED seq 5 permit 10.10.0.0/16

route-map FROM-ISP permit 10
  match ip address prefix-list EBGP-PREFERRED
  set weight 40000
route-map FROM-ISP permit 20

router bgp 65001
  neighbor 192.0.2.1
    remote-as 65002
    address-family ipv4 unicast
      route-map FROM-ISP in

Option B: demote the redistributed copy before the “locally originated” step. Local preference is compared before it, so a lower local preference settles it. Unlike weight, local preference is sent to iBGP peers, which is usually what you want for a backup path.

route-map OSPF-TO-BGP permit 10
  match ip address prefix-list EBGP-PREFERRED
  set weight 0
  set local-preference 50
route-map OSPF-TO-BGP permit 20

router bgp 65001
  address-family ipv4 unicast
    redistribute ospf 1 route-map OSPF-TO-BGP

With A or B, the order no longer matters. If OSPF arrives first, the eBGP path still wins when it shows up. BGP then installs it at AD 20, the OSPF route leaves the URIB, and the redistributed copy is withdrawn. If the eBGP path goes away, OSPF is installed again, gets redistributed, and becomes the backup.

Option C: don’t redistribute the prefix at all. This is the cleanest option if R1 doesn’t need to advertise the OSPF version into BGP as a backup.

route-map OSPF-TO-BGP deny 5
  match ip address prefix-list EBGP-PREFERRED
route-map OSPF-TO-BGP permit 10

router bgp 65001
  address-family ipv4 unicast
    redistribute ospf 1 route-map OSPF-TO-BGP

NX-OS already requires a route-map on every redistribute statement, so that route-map is the natural place for this policy.

If the IGP Path Should Win

Here Scenario 2 is the broken case, because the eBGP route at AD 20 locks OSPF out.

Option D: filter the prefix inbound from the eBGP peer.

ip prefix-list IGP-OWNED seq 5 permit 10.10.0.0/16

route-map FROM-ISP deny 5
  match ip address prefix-list IGP-OWNED
route-map FROM-ISP permit 10

router bgp 65001
  neighbor 192.0.2.1
    address-family ipv4 unicast
      route-map FROM-ISP in

Filtering removes the eBGP path completely, so it can’t act as a backup.

Option E: raise the AD of just this prefix with a BGP table-map. If you need the eBGP path as a backup, NX-OS can change the administrative distance of specific BGP routes before they’re sent to the URIB. Setting it above OSPF’s 110 lets OSPF win whichever route arrives first, while the eBGP path stays in the BGP table and takes over if OSPF loses the route.

route-map BGP-TO-RIB permit 10
  match ip address prefix-list IGP-OWNED
  set distance 120
route-map BGP-TO-RIB permit 20

router bgp 65001
  address-family ipv4 unicast
    table-map BGP-TO-RIB

Don’t add the filter keyword to table-map here. With filter, routes denied by the route-map aren’t installed in the URIB, and you only want to change the distance. After applying a table-map, check show ip route 10.10.0.0/16 to confirm the new AD on the existing route.

Last resort: change the distance globally. This affects every eBGP route in the address family (and VRF), not just this prefix.

! eBGP routes now lose to OSPF (110) for this whole address family
router bgp 65001
  address-family ipv4 unicast
    distance 120 200 220

Summary of Fixes

Option Preferred Path Change Where the Race Is Settled
A eBGP Inbound route-map, set weight 40000 on the prefix Best-path step 1 (weight)
B eBGP Redistribution route-map, set weight 0 and set local-preference 50 Best-path step 2 (local preference)
C eBGP Deny the prefix in the redistribution route-map No redistributed path exists
D IGP, no eBGP backup Deny the prefix in the inbound eBGP route-map No eBGP path exists
E IGP, eBGP as backup BGP table-map with set distance 120 on the prefix URIB (AD 120 vs 110)
Global distance IGP, eBGP as backup distance 120 200 220 under the BGP address family URIB, for every eBGP route in the address family

Prove It

Test both orders in a lab or a maintenance window:

  1. Shut the eBGP session, let OSPF install the route, then bring the session back up.
  2. Shut the OSPF adjacency, let the eBGP route install, then bring OSPF back up.
  3. Reload the router.

After each step, run show bgp ipv4 unicast 10.10.0.0/16 and show ip route 10.10.0.0/16. With the fix in place, all three runs should show the same best path and the same URIB entry.

The control plane is only half the test. Also confirm that:

  • Forwarding matches the URIB. Check show forwarding ipv4 route 10.10.0.0/16 and run a traceroute to a host in the prefix.
  • The backup takes over. Remove the preferred path, confirm traffic moves to the backup, then restore it and confirm traffic moves back.

A Related Problem: Redistributing BGP Back Into the IGP

Everything above involves only OSPF → BGP. If you also redistribute BGP into OSPF or EIGRP, you add two separate problems.

Routing loops from mutual redistribution. A prefix can go OSPF → BGP → OSPF and come back with a different origin and metric. Tag routes as they leave BGP and refuse them on the way back:

route-map BGP-TO-OSPF permit 10
  set tag 65001

route-map OSPF-TO-BGP deny 1
  match tag 65001
! ...followed by the permit entries from the options above

router ospf 1
  redistribute bgp 65001 route-map BGP-TO-OSPF

Blackholing at startup. After a reload, OSPF can attract traffic to the router before BGP has learned its routes. That’s a different race, with a specific fix: keep the router out of the forwarding path until BGP converges.

router ospf 1
  max-metric router-lsa on-startup wait-for bgp 65001

Takeaway

Redistributed routes win on weight, and as locally originated routes, before AS path or any other path attribute is compared. If you redistribute an IGP into BGP and also learn the same prefixes from a peer, a route-map should decide the winner explicitly. Otherwise, the order the routes arrive in decides it for you.

A deterministic routing design shouldn’t depend on which protocol converges first. Routing policy, not convergence timing, should decide the forwarding path.