BGP, the Border Gateway Protocol, is the glue that holds the internet together, but its path selection algorithm is surprisingly simple, not complex.

Let’s see BGP in action. Imagine two routers, R1 and R2, peering with each other. R1 is advertising a route to 192.0.2.0/24 to R2.

R1# show ip bgp neighbors 198.51.100.2 advertised-routes
  Network          Next Hop            Metric LocPrf Weight Path
*> 192.0.2.0/24     0.0.0.0                  1         32768 i

Here, > indicates the best path, * indicates a valid path, i means it’s an internal BGP (iBGP) learned route, Next Hop is the IP address of the next router in the path, Metric is the MED (Multi-Exit Discriminator), LocPrf is the local preference, and Weight is a Cisco-specific attribute.

Now, R2 learns about this route from R1. If R2 has multiple paths to 192.0.2.0/24, BGP will go through a rigorous decision process to pick the "best" one.

The BGP path selection process is a sequence of checks. If a check fails, it moves to the next.

  1. Weight: This is a Cisco proprietary attribute. A higher weight is always preferred. If a route is learned from a Cisco router and has a higher weight, it wins.

    • Diagnosis: show ip bgp on the local router. Look for the Weight column.
    • Fix: neighbor <neighbor-ip> weight <weight-value> in the BGP configuration. For example, neighbor 198.51.100.2 weight 100.
    • Why it works: Weight is a local attribute, meaning it’s only considered on the router where it’s set. It’s the first tie-breaker, heavily favoring routes explicitly influenced by local policy.
  2. Local Preference: This is an iBGP attribute. A higher local preference is preferred. It’s typically advertised to all iBGP peers.

    • Diagnosis: show ip bgp and look for the LocPrf column.
    • Fix: set local-preference <value> using a route-map applied to the neighbor. For example, route-map SET-LOCAL-PREF permit 10, set local-preference 150. Apply with neighbor 198.51.100.2 route-map SET-LOCAL-PREF in.
    • Why it works: Local preference signals preferred exit points within an autonomous system (AS). By setting a higher local preference for routes learned from a specific peer, you’re telling your internal routers to prefer that path for outbound traffic.
  3. Originator ID (iBGP only) and Router ID: The route originated by the lowest Router ID is preferred. This prevents routing loops within an iBGP mesh.

    • Diagnosis: show ip bgp and show ip bgp summary. Check the Router ID of the originating router.
    • Fix: Ensure your router IDs are consistently configured and unique across your AS, typically using the highest loopback IP address. bgp router-id <router-id-ip>.
    • Why it works: This tie-breaker ensures that if multiple iBGP speakers within the same AS advertise the same prefix, the path learned through the iBGP speaker with the numerically lowest Router ID is chosen, providing a deterministic choice.
  4. AS_PATH Length: Shorter AS paths are preferred. This is the most common metric for exterior BGP (eBGP).

    • Diagnosis: show ip bgp. The Path column shows the AS numbers. Count them.
    • Fix: This is typically influenced by network topology and peering agreements, not directly configurable on a per-route basis. You’d influence it by advertising routes via fewer AS hops.
    • Why it works: A shorter AS path generally implies a more direct connection, reducing the number of hops and potential points of failure between ASes.
  5. Origin Type: Routes with origin type IGP (originated within the AS) are preferred over EGP (external gateway protocol, rare now) and Incomplete (learned via redistribution).

    • Diagnosis: show ip bgp. The Path column will show i for IGP, E for EGP, or ? for incomplete.
    • Fix: Ensure routes are originated correctly within your AS using network commands or proper redistribution.
    • Why it works: Routes originating from within your own AS are generally considered more trustworthy and under your direct control than those learned from external sources.
  6. MED (Multi-Exit Discriminator): If routes are learned from the same AS (eBGP multi-hop), a lower MED is preferred.

    • Diagnosis: show ip bgp. Look for the Metric column.
    • Fix: set metric <value> using a route-map applied to the neighbor. For example, route-map SET-MED permit 10, set metric 50. Apply with neighbor <neighbor-ip> route-map SET-MED out.
    • Why it works: MED is used to influence how other ASes send traffic into your AS. A lower MED signals to external ASes that this is a preferred entry point.
  7. eBGP over iBGP: If paths are still tied, eBGP learned paths are preferred over iBGP learned paths.

    • Diagnosis: show ip bgp. Look at the Next Hop and neighbor type.
    • Fix: This is a fundamental BGP rule, not directly configurable. It reflects the trust placed in directly connected eBGP peers.
    • Why it works: eBGP peers are directly connected and represent external networks, while iBGP peers are internal. BGP prioritizes direct external connections to ensure reachability outside the local AS.
  8. Oldest Path: If all else is equal, the path that was learned first is preferred.

    • Diagnosis: This is difficult to see directly. It’s the last resort.
    • Fix: Not configurable. It’s a final deterministic choice.
    • Why it works: This ensures that BGP doesn’t flap routes unnecessarily when multiple identical paths exist.
  9. Neighbor IP Address: If still tied, the path learned from the neighbor with the lowest IP address is preferred.

    • Diagnosis: show ip bgp. Compare neighbor IP addresses.
    • Fix: Not configurable. A final tie-breaker based on IP address.
    • Why it works: Provides a final, deterministic selection when all other attributes are identical.

The surprising part is how many of these attributes are local policy or Cisco-specific. The internet relies heavily on AS_PATH length, but within your own network, Weight and Local Preference are your primary tools for shaping traffic.

Once BGP has selected the best path, you’ll see it marked with a > in show ip bgp. If this path is lost, BGP will re-evaluate and pick the next best path according to this sequence.

The next concept you’ll run into is how BGP attributes are manipulated using route-maps and prefix-lists to control routing policy.

Want structured learning?

Take the full Computer Networking course →