BGP best-path selection

Before BGP chooses anything, the longest matching prefix wins. Among paths to the same prefix, BGP compares in order: weight, local preference, locally originated, AIGP, AS_Path length, origin, MED, eBGP over iBGP, IGP metric to the next hop, then tie-breakers down to the lowest neighbour address. The first difference decides.

In Daveth Pro (Network)10 min video · lab: eBGP

By , founder of Daveth · Updated

Opening frame of the video walkthrough of BGP best-path selection

Watch this lesson

10 min · Video walkthrough

In Daveth Pro (Network)

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

Upcoming practical lab

Lab E7: eBGP

What the lab covers

What this page answers

What is the BGP best path selection order?

Highest weight, highest local preference, locally originated, lowest AIGP, shortest AS_Path, origin (IGP, then EGP, then incomplete), lowest MED, eBGP over iBGP, lowest IGP metric to the next hop, oldest eBGP path, lowest router ID, shortest cluster list, lowest neighbour address. Read it in the lesson

What is BGP weight?

Cisco's own attribute and step 1: a 16-bit value, 0 to 65,535, set on the router and never advertised. Higher wins; the router's own routes start at 32,768. Read it in “4.1 Before BGP gets a say: longest match”

What is BGP local preference?

Step 2: a well-known discretionary attribute, 32 bits, higher wins, 100 by default. Set with a route map or for everything from one neighbour, it can make a longer AS_Path win. Read it in “4.1 Before BGP gets a say: longest match”

Does a more specific prefix beat BGP attributes?

Yes. A router always forwards on the longest matching prefix, so a /24 beats a /16 whatever their attributes. Advertising specifics and a summary steers traffic in a way no provider policy can override. Read it in “4.1 Before BGP gets a say: longest match”

4.1 Before BGP gets a say: longest match

The best-path algorithm influences how traffic enters and leaves an AS, and the guide is blunt that many engineers don't understand it and end up with suboptimal routing. So start with the rule that sits above it: a router always forwards on the longest matching prefix. A /28 beats a /26, and a /26 beats a /24, whatever BGP thinks of their attributes. It's basically like asking for directions: "the blue door on Park Road" beats "somewhere in town", however trustworthy the second person looks.

That gives you a steering tool no provider can override. In Figure 12-10, an organisation with 100.64.0.0/16 wants 100.64.1.0/24's traffic to arrive at R1 and 100.64.2.0/24's at R2. Changing attributes might not work, because a provider's policy can ignore them. So R1 advertises 100.64.1.0/24, R2 advertises 100.64.2.0/24, and both advertise the summary 100.64.0.0/16; if R1 fails, the summary through R2 still reaches 100.64.1.0/24. If you've ever given a delivery driver both your flat number and the building's main entrance, you've done the same: the specific one gets used, and the general one is there if it fails.

A /24 beats a /16 before BGP has even opened its rulebook: the one fight in routing that's over before the referee arrives.

Two limits: only advertise ranges that are yours, and expect providers to refuse IPv4 prefixes longer than /24 and IPv6 longer than /48, to keep the Internet table in check.

A BGP route is the NLRI (prefix and length) plus path attributes, and it can have several paths to the same destination. BGP keeps them all in the Loc-RIB, picks one best path, installs it in the routing table and advertises only that one; if it disappears, the others are already there to choose from. It recalculates on four events: a change in next-hop reachability, a failed interface to an eBGP peer, a redistribution change, and paths arriving or leaving.

The first path received is installed as best, and every newer path is compared against the current best one; a tie on one attribute moves the comparison to the next. It acts kind of like a title fight: a challenger has to beat the reigning champion, not the whole field. The order:

  1. Weight (higher)
  2. Local preference (higher)
  3. Locally originated (network statement or redistribution, then aggregation, then received)
  4. AIGP (lower)
  5. Shortest AS_Path
  6. Origin (IGP, then EGP, then incomplete)
  7. Lowest MED
  8. eBGP over iBGP
  9. Lowest IGP metric to the next hop
  10. If both paths are eBGP, the oldest (the path from the longest-established session)
  11. Lowest router ID
  12. Shortest cluster list
  13. Lowest neighbour address

Think about the last time you chose a flat to rent: price first, and only if two tie on price do you even look at distance, then light, then the landlord. BGP is that strict: the first difference decides, and everything below it is never read. So a path through two ASes can beat a path through one, if its local preference is higher: step 2 settles it and step 5 is never reached. The memory hook: first difference wins, and longest match wins before BGP starts.

BGP's list has thirteen steps, which is still fewer than a family needs to choose a film.

This is the order the guide teaches, and the exam asks for. RFC 4271's own tie-break list is shorter: AS_Path length, origin, MED, eBGP over iBGP, interior cost, BGP identifier, peer address, after a "degree of preference" that is the local preference for routes from internal peers and comes from local policy for external ones. Weight, locally originated, AIGP, the oldest-path step and cluster list length are additions on top. Some attributes are transitive and change routing in other ASes too; others stay inside your organisation. Which routes get which attribute values is decided by matching them on things like AS_Path length, a specific ASN or communities, with the tools in 02-12 (communities: bonus B-04).

Weight is Cisco's own attribute and step 1: a 16-bit value (0 to 65,535) set on the router, never advertised to anyone. Higher wins. It's set per route with an inbound route map, or for everything from one neighbour, and it only influences outbound traffic from a router or an AS; use it when nothing else should get a vote. In Example 12-28, R2 has two paths to 172.16.1.0/24, and the one through AS 65300 has weight 123 against the other's 0, so it's "best #1". Consider it your own personal favourite: it matters to you and nobody else ever hears of it.

Local preference is step 2: a well-known discretionary attribute, 32-bit, higher wins, 100 by default. Like weight, it's set per route with a route map or for everything from one neighbour. It's shared with every iBGP peer in the AS and never sent to eBGP peers, so it makes the whole AS agree on its exit. In Example 12-29 the path through AS 65300 has 333 against 111, and wins. It's a bit like a household's agreed favourite takeaway: everyone in the house uses it, and the neighbours are never told.

Your own routes start at weight 32,768, halfway up a ladder that only goes to 65,535, before any other path has climbed a rung.

Locally originated is step 3: a route this router advertised itself (network statement or redistribution) beats one it aggregated, which beats one received from a peer. Home-grown beats bought-in. Look back at 02-10's 10.12.1.0/24, known twice on R1: the local path won, but at step 1, not step 3, because a local route carries weight 32,768 and R2's path 0.

AIGP is step 4: an optional non-transitive attribute for ASes that are all under one organisation's control, each with its own IGP. BGP normally doesn't use a path metric, because every AS measures differently; AIGP carries one between those ASes, with AIGP enabled on every BGP session and the IGPs redistributed into BGP (Figure 12-11). It acts kind of like one hotel group running a single loyalty scheme across all its brands: points add up across hotels only because one owner set the rules. A path with an AIGP metric beats one without; if the next hop needs a recursive lookup, the metric to reach it is added, again at every lookup; the lower AIGP metric wins; and a router that changes the next hop adds its own IGP distance.

Before the MED rule, a prediction: two eBGP paths tie on everything down to step 6. One comes from AS 65100 with MED 50, the other from AS 65300 with MED 10. Which wins at step 7? D1's "MED, two ASes" answers it.

Shortest AS_Path is step 5: fewer ASes wins. Prepending extra copies of an ASN, usually your own, makes a path look longer and less attractive. Prepend in what you advertise to other ASes to steer traffic coming in; prepend in what you receive to steer traffic going out. In Example 12-30, AS 65300 advertised "65300 65300", so R2 picks the one-AS path through 65100. If you scaled that down to a bus company, prepending would be adding a few pointless stops to your own route so passengers choose the other line. And it's why the guide says peering with more than one provider tends to give better routing: one may be a single AS hop from a destination another reaches only the long way round.

Prepending your own AS three times is BGP for "honestly, I'm miles away, try the other route".

Origin is step 6: IGP (i, a network statement) beats EGP (e), which beats incomplete (?, redistributed). In Example 12-31 the redistributed path loses.

MED is step 7: the multi-exit discriminator, a non-transitive 32-bit metric, lower wins; it's the value show bgp prints as "metric" (Example 12-32's "metric 33"). BGP sets it from the IGP metric when a route is advertised or redistributed (02-10's Example 11-13). A MED received over eBGP can be passed to iBGP peers but shouldn't leave the AS that received it; its job is to tell a neighbouring AS which of your entry points to use for traffic coming in. And MED is only compared between paths from the same neighbouring AS. That answers the prediction: from AS 65100 and AS 65300, the MEDs are never compared, and a later step decides. Have you ever noticed how a hospital asks ambulances to use the emergency entrance rather than the main door? MED is that request to one neighbour, and it means nothing when you compare two different hospitals.

A path with no MED counts as MED 0, the best possible: RFC 4271 says so, RFC 4451 describes the same default, and IOS advertises MED 0 to iBGP peers. Older specifications gave a missing MED the worst value instead, and some organisations still require a MED on every prefix and treat a missing one as least preferred. Which makes the guide's warning a real trap: if a provider sets a MED on only one of its two links to you, the link with no MED counts as 0 and wins, the opposite of what the provider meant. In Example 12-32 both paths come from AS 65300, so MED 0 beats MED 33.

eBGP over iBGP is step 8, with confederation peers in between (confederations are outside the exam). Lowest IGP metric to the next hop is step 9. In Figure 12-12, AS 400's routers peer in a full mesh on their loopbacks and R1 advertises 172.16.0.0/24 to R2 and R4. The figure's "Metric" column there is the IGP cost to each next hop, not show bgp's MED column. R3 prefers R2's path (IGP cost 10 against 20), and R5 prefers R4's (10 against 20). R2 and R4 each prefer their own eBGP path, but that was settled a step earlier, at step 8. Imagine if two job candidates were equal on everything that matters: you'd pick the one who lives closer, and that's step 9.

Then the last-resort steps.

  • Step 10: if both paths are eBGP, the one from the oldest session wins. Unstable sessions would otherwise make BGP recalculate constantly, so it sticks with the path it's had longest; the price is that you can't design around it.
  • Step 11: the lowest router ID of the advertising router (for a route-reflected route, the originator ID).
  • Step 12: the shortest cluster list, which route reflectors append their cluster ID to, as loop prevention; the shortest list is the path that crossed the fewest iBGP advertisement hops. The guide leaves route reflectors out.
  • Step 13: the lowest neighbour address, which only iBGP paths reach, because eBGP ties were already settled by age. In Figure 12-13, R1 and R2 have two iBGP sessions over 10.12.1.0/24 and 10.12.2.0/24. Both paths come from the same router, so they tie on router ID at step 11, and R2 picks the path from 10.12.1.1.

You can think of it as the final tie-break at a quiz night: when every score is level, the rules still name one winner. The same steps apply to IPv6 routes, where step 13 compares IPv6 neighbour addresses.

When everything else ties, BGP picks the lowest neighbour address: routers settle their last arguments by comparing addresses.

D1 runs the whole list on the guide's examples. Predict the deciding step before you read the answer.

D1

  1. BGP best-path drawing: two paths to the same prefix, side by side, before any step of the list is read.

    Step 1 of 2 Two paths to the same prefix.

  2. BGP best-path drawing: step 1, weight, decides; path A with weight 123 beats path B with 0, and the later steps are greyed out.

    Step 2 of 2 Step 1, weight (higher), decides: path A is best, with A’s weight 123 against B’s 0. Every step below it is never read.

The attributes split by the direction of traffic they influence, like the controls in a car: some you work yourself, and some only work if the driver behind reads your indicator:

To influenceUseWhy it works
Traffic leaving a router or the ASweightit's local to the router and never advertised
Traffic leaving the ASlocal preferenceevery iBGP peer shares it, no eBGP peer sees it
Traffic entering the ASAS_Path prepending (on what you advertise)other ASes prefer the shorter path
Traffic entering from one neighbouring ASMEDthat neighbour compares your MEDs between its links to you
Traffic entering, whatever the provider's policya longer prefix from the entry you wantlongest match wins before BGP

Picture yourself running a shop with two doors: you decide which door you leave by, and for customers you can only put up signs, which some won't read. Except for the last row: there you don't put up a sign, you change the address on the letter.

For inbound traffic, attributes only let you ask nicely; the longer prefix is the one way to insist, which is also the situation with teenagers.

Every knob you set per route is set the same way: a route map, attached to one neighbour, in one direction. (Weight and local preference can also be set for everything from one neighbour, section 4.3; locally originated isn't a value you set but where the route came from, and AIGP's configuration is out of scope.) "The route map is associated with the BGP neighbor with the command neighbor ip-address route-map route-map-name {in|out} under the specific address family". Inbound, it changes what you believe about the paths you receive (weight, local preference); outbound, it changes what you tell that neighbour (a prepended AS_Path, a MED). That's roughly equivalent to the two desks at a border crossing: arrivals stamps what comes in, departures stamps what goes out, and neither touches the other's queue.

The guide's R1 (AS 65100, 10.12.1.1) runs everything it hears from R2 (AS 65200, 10.12.1.2) through a four-step map, AS65200IN (Example 12-17, descriptions left out):

ip prefix-list FIRST-RFC1918 permit 192.168.0.0/15 ge 16
ip as-path access-list 1 permit _65200$
ip prefix-list SECOND-CGNAT permit 100.64.0.0/10 ge 11
!
route-map AS65200IN deny 10
 match ip address prefix-list FIRST-RFC1918
!
route-map AS65200IN permit 20
 match ip address prefix-list SECOND-CGNAT
 match as-path 1
 set local-preference 222
!
route-map AS65200IN permit 30
 match as-path 1
 set weight 65200
!
route-map AS65200IN permit 40
!
router bgp 65100
 address-family ipv4 unicast
  neighbor 10.12.1.2 route-map AS65200IN in

(The guide prints the last line as neighbor 10.12.1.1, R1's own address on that link; the peer is R2, 10.12.1.2, as Example 12-16's next hops show.) 02-12 teaches route-map matching in full; three rules are enough here: sequences in order, first match wins, and two different match lines in one sequence must both hit. ge 11 in SECOND-CGNAT means any longer prefix inside 100.64.0.0/10 (prefix lists: 02-12). _65200$ means "the path ends in 65200", so the route was originated in AS 65200 (the guide's step 2: "originating from AS 65200") (AS_Path regex: 02-12). Before you open D2, predict where 100.64.3.0/25 lands: it's inside 100.64.0.0/10, and its path is 65200 65300 300.

D2

Route map AS65200IN laid out sequence by sequence (deny 10, permit 20 setting local preference 222, permit 30 setting weight 65200, permit 40), above the eight paths R2 sent.

Example 12-18 shows the result:

  • 192.168.2.2/32 and 192.168.3.3/32 are gone: sequence 10 denied them.
  • 100.64.2.0/25 and 100.64.2.192/26 show LocPrf 222: inside 100.64.0.0/10 and originated in 65200 (sequence 20).
  • 10.12.1.0/24 and 10.23.1.0/24 show Weight 65200: originated in 65200 but outside 100.64.0.0/10 (sequence 30).
  • 100.64.3.0/25 (path 65200 65300 300) and 10.3.3.0/24 pass unchanged through sequence 40: they sit behind 65200, not in it.

Now look at 10.12.1.0/24. R1 has it twice: its own connected network (weight 32768) and R2's copy, now weight 65200. Weight is step 1, so R2's path is best, and the line reads r>. The r is "RIB-failure" in the status codes: BGP chose a best path that the routing table didn't take, because the connected route is already there. Just like when you tell the satnav to prefer a route and the road you're standing on is already the shortest: the advice is recorded, and ignored.

R1 gave R2's copy of its own subnet a weight of 65200, a vote of confidence so large the router's own routing table politely declined it.

A policy change doesn't touch paths already processed until they go through it again. A hard reset "tears down the BGP session", which is the disruptive way. A soft reset doesn't: for one family it's clear bgp afi safi {ip-address|*} soft [in | out]. With in, the router asks the peer to send its routes again (a route refresh, when the session supports it), so the new inbound policy runs over them; with out, it sends its own routes to the peer again, through the new outbound policy.