BGP Best Path Selection: The 13-Step Algorithm Explained - 夜莺博客

BGP Best Path Selection: The 13-Step Algorithm Explained

BGP routers commonly hear multiple paths to the same prefix, and the best path algorithm decides which one is installed in the routing table and advertised onward. The algorithm is a tiebreaker cascade: most paths never survive past the first few steps. Knowing which attribute decides in which scenario — and which knobs are local versus AS-wide — is the real skill. This guide walks the full Cisco 13-step order with a worked example of a dual-homed router.

The 13-Step Best Path Order

 0. Next hop must be reachable (drop unreachable paths)
 1. Weight           — highest wins (Cisco-proprietary, local to the router)
 2. Local preference — highest wins (advertised inside your AS)
 3. Locally originated (network/aggregate/redistribute) preferred
 4. AS_PATH          — shortest wins
 5. Origin           — IGP (i) > EGP (e) > incomplete (?)
 6. MED              — lowest wins, only if same neighbor AS
 7. eBGP over iBGP   — prefer external paths
 8. IGP metric to next hop — lowest wins
 9. maximum-paths: install multiple if equal here
10. Oldest eBGP path preferred
11. Lowest Router-ID
12. Lowest neighbor address

Worked Example: Three eBGP Paths

R1 hears 10.0.0.0/24 from three providers:

Path      Weight  LocalPref  AS-path    MED
ISP-A        0       200     65100 65200  100
ISP-B        0       200     65300        100
ISP-C      100       100     65400         50

Step 1 decides everything: ISP-C wins on weight (100 vs 0). Had weights been equal, local preference would have eliminated ISP-C (100 < 200), then AS-path length would pick ISP-B (one AS) over ISP-A (two).

Manipulating Selection

Weight forces one router to prefer an exit without affecting the rest of the AS — the classic remote-office-with-two-ISPs case. Set it inbound from a neighbor with a route-map:

route-map PREFER-ISP1 permit 10
 set weight 200
!
router bgp 65001
 neighbor 203.0.113.1 route-map PREFER-ISP1 in

Local preference is the AS-wide decision: set it at the eBGP edge and every iBGP router agrees on the exit. It is compared only among paths for the same prefix learned from the same... well, any neighbor — and it travels inside iBGP updates:

route-map SET-LP permit 10
 set local-preference 200
!
router bgp 65001
 neighbor 203.0.113.1 route-map SET-LP in

MED and Its Catch

MED (multi-exit discriminator) is a hint from the neighboring AS — lower is better, but it is only compared when the two paths come from the same neighbor AS. Otherwise the step is skipped. A common provider-side tuning technique is AS-path prepending: set as-path prepend 65001 65001 lengthens the path and makes a backup link less attractive.

Verification

show ip bgp
show ip bgp 10.0.0.0/24
show ip bgp neighbors 203.0.113.1 advertised-routes

show ip bgp <prefix> prints every path with weight, localpref and AS-path columns plus the > marker on the winner — read that output and you can always explain why a path won. Route dampening changes how flapping paths are treated (see BGP route dampening), and the same show commands apply on IOS XR BGP configurations.

原文链接:https://packetmentor.com/topics/bgp-path-selection