Best-path selection

BGP keeps every path for a prefix and picks one best path for the routing table, recalculating when paths change. The order starts with weight, felt by one router only, then local preference, heard by the whole AS, then locally originated routes and AIGP, then AS_Path length and origin, which come from outside. A more specific prefix beats them all.

In Daveth Pro (Network)13 min video · lab: BGP paths and policy

By , founder of Daveth · Updated

Opening frame of the video walkthrough of Best-path selection

Watch this lesson

13 min · Video walkthrough

In Daveth Pro (Network)

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

Upcoming practical lab

Lab R6: BGP paths and policy

What the lab covers

What this page answers

What is the difference between BGP weight and local preference?

Weight is local to one router and never advertised. Local preference, a 32-bit well-known discretionary attribute, goes to every iBGP peer, so the whole AS agrees on the exit; it is not advertised between eBGP peers. Read it in “The lever the whole AS hears: local preference”

What is the BGP AIGP attribute?

The accumulated IGP metric: an attribute compared at step 4, after locally originated routes, that carries an IGP-style metric across BGP so paths can be chosen by accumulated cost. Lower wins. Read it in “Born here: local origination and AIGP”

How does AS_Path length affect BGP path selection?

The shorter AS_Path wins at its step; a confederation's sequence is not counted and an AS_SET counts as one. Prepending ASNs makes a path longer and less attractive. Read it in “Carried from outside: AS_Path and origin”

The strongest tool isn't on the list

Start with a design question, because it saves you from using best path where it doesn't apply.

The best-path algorithm decides how traffic enters or leaves an AS, and some configurations change BGP's attributes to steer inbound traffic, outbound traffic, or both, depending on what the design needs. Many network engineers don't understand best-path selection, and that often ends in suboptimal routing.

Here's a case. An organization owns 100.64.0.0/16 but only needs to advertise two subnets, 100.64.1.0/24 and 100.64.2.0/24, through two routers, R1 and R2, and it must survive a router failure. It could advertise both /24s from both routers. But then all the traffic might come in on R1, because that's where the providers' best-path decisions land.

None of the attributes. You could change attributes on the way out, but a service provider can have a routing policy that ignores them, and then traffic arrives wherever it arrives. What a router always does, before any BGP attribute, is pick the path with the longest prefix length: a /28 over a /26, a /26 over a /24. And that logic can steer BGP path selection. So advertise the summary, 100.64.0.0/16, from both routers, and one longer prefix from each: that makes traffic arrive deterministically and keeps a backup path through the other router. Drawn out: R1 advertises 100.64.1.0/24, R2 advertises 100.64.2.0/24, and both advertise 100.64.0.0/16. Whatever the provider's policy, each /24 is advertised out one router only, so its traffic comes in there, and the summary is the redundancy: if R1 crashes, devices reach 100.64.1.0/24 through R2's /16.

Network drawing: R1 → AS 100: 100.64.1.0/24 + 100.64.0.0/16, R2 → AS 200: 100.64.2.0/24 + 100.64.0.0/16, enterprise network: 100.64.0.0/16, AS 100, AS 200, R1, R2

Each /24 leaves through one router only; the /16 leaves through both, so it's the backup.

Two limits. The summaries you advertise must stay inside your own range, and providers typically don't accept IPv4 routes longer than /24 or IPv6 routes longer than /48. The reason: routes are restricted to control the size of the Internet routing table. So for IPv6 the same design would split a /47 or shorter into /48s.

Hold on to what just happened. Best path never compared 100.64.1.0/24 with 100.64.0.0/16. They're different prefixes, and the longer one is used for forwarding wherever it exists. Everything from here on is about the narrower question: two paths to the same prefix. Which one does BGP pick, and can you prove it?

One winner per prefix, and the order

Inside the Loc-RIB, BGP keeps every route with its path attributes and the best path already worked out; the best path goes to the RIB for installation, and if it disappears the router uses the paths it still holds to find a new best quickly. Four events make BGP recalculate a prefix's best path: a change in next-hop reachability, a failed interface toward an eBGP peer, a redistribution change, and paths for the route arriving or being removed.

The first path received is installed as the best path automatically. Each newer path for the same prefix length is compared against the current best, and on a tie the processing carries on until a winner is found. Keep "against the current best" in mind; it matters for MED.

One gate comes before any comparison. A path has to pass the route validity check, and its next-hop address has to be resolvable, before it can be a best path at all; some vendors and publications count that as the first step. You met the gate in the iBGP lesson: a path whose next hop doesn't resolve sits in the table with no >, and no attribute can rescue it. Put the other way: if the next hop isn't reachable, the router doesn't even start the process, because it considers the next hop inaccessible.

Now the order. The attributes are processed in this order:

  1. Prefer the highest weight.
  2. Prefer the highest local preference.
  3. Prefer the route originated by the local router.
  4. Prefer the path with the shorter Accumulated Interior Gateway Protocol (AIGP) metric.
  5. Prefer the shortest AS_Path.
  6. Prefer the best origin code.
  7. Prefer the lowest multi-exit discriminator (MED).
  8. Prefer an external path over an internal path.
  9. Prefer the path through the closest IGP neighbor.
  10. Prefer the path from the oldest eBGP session.
  11. Prefer the path with the lowest neighbor BGP router ID.
  12. Prefer the path with the minimum cluster list length.
  13. Prefer the path with the lowest neighbor IP address.

And the rule that makes it an order, not a scorecard: when BGP finds a difference, it stops and uses that attribute as the reason the path is best, and it looks no further. Nothing on step 9 can make up for a loss on step 2. The second criterion, then, is local preference.

Is that the protocol's order? Partly. RFC 4271, BGP-4's specification, has no weight and no "locally originated" step. Its first comparison is a degree of preference, which is the LOCAL_PREF value for a route from an internal peer and a policy result for one from an external peer; then its tie-breakers run AS_PATH length, origin, MED, external over internal, the interior cost to the NEXT_HOP, the lowest BGP Identifier, and the lowest peer address. Cisco's list adds weight on top, local origination and AIGP after local preference, and the oldest eBGP path; RFC 7311 puts AIGP before all of RFC 4271's tie-breakers, and RFC 4456 inserts the cluster list between the BGP Identifier and the peer address, which is where step 12 sits. The oldest-path step isn't in any of them.

One more thing before the experiments: not every attribute is allowed to travel. Policy varies from organization to organization. Some attributes are transitive and carry from AS to AS, so a change can affect routing for providers downstream; others are non-transitive and only shape routing inside the organization. Prefixes are matched on things like AS_Path length, a specific ASN or BGP communities. You know the four classes. Two more columns:

ClassSupported by all BGP implementationsAdvertised between ASs
Well-known mandatoryYesYes
Well-known discretionaryYesNo
Optional transitiveNoYes
Optional non-transitiveNoNo

Use that last column with care. Local preference fits it, but it breaks twice: MED is optional non-transitive and is sent to the neighboring AS on purpose (below), and the atomic aggregate from the route-advertisement lesson, well-known discretionary, reached a router two ASs away. Where an attribute actually reaches is something each step below shows for itself.

The lever one router feels: weight

First run. The lab: R4, R5 and R6 sit in AS 400 with a full mesh of iBGP between their loopbacks, and AS 200 (R2) and AS 300 (R3) carry transit to AS 100 (R1), which owns 172.16.0.0/24, 172.20.0.0/24 and 172.24.0.0/24. R4 faces R2 on 10.24.1.0/24, R6 faces R3 on 10.36.1.0/24, and R5 sits between them. The lever: R4 sets weight 222 on 172.16.0.0/24 as it arrives from R2, so that R4 uses R2 for that prefix, and R6 sets weight 333 on 172.24.0.0/24 from R3. An inbound route map on each edge does it. Writing that policy is the next lesson's; here you only need its result.

Network drawing: AS 100, 172.16.0.0/24, 172.20.0.0/24, 172.24.0.0/24, AS 200, AS 300, AS 400 · iBGP full mesh, 10.24.1.0/24, 10.36.1.0/24, 10.45.1.0/24, 10.56.1.0/24, Lo0 192.168.4.4, Lo0 192.168.6.6, Lo0 192.168.5.5, iBGP, R1, R2, R3, R4, R6, R5

The lab for the weight, local preference, prepending and origin runs. The dashed line is an iBGP session with no cable of its own.

Take R4. If your instinct says "the external one, of course", hold that instinct and check it against the list.

R4 picks path B, and the reason is step 1. Weight is a Cisco-defined attribute and the first step in selecting the best path. It's a 16-bit value, 0 to 65,535, assigned locally on the router and not advertised to other routers, and the higher weight wins.

Here's how you prove it instead of arguing it. The command show bgp afi safi network [bestpath | best-path-reason] is the tool for comparing a prefix's paths: newer software adds the two keywords, bestpath to show only the best path, best-path-reason to show every path with the reason it wasn't chosen (which release added them, we haven't confirmed). Here's R4's entry, two paths, best #2, through R2, because of the weight:

R4# show bgp ipv4 unicast 172.16.0.0/24 best-path-reason
! Output omitted for brevity
BGP routing table entry for 172.16.0.0/24, version 11
Paths: (2 available, best #2, table default)
 300 100
   192.168.6.6 (metric 131072) from 192.168.6.6 (192.168.6.6)
     Origin IGP, metric 0, localpref 100, valid, internal
     Best Path Evaluation: Lower weight
 200 100
   10.24.1.2 from 10.24.1.2 (192.168.2.2)
     Origin IGP, localpref 100, weight 222, valid, external
     Best Path Evaluation: Overall best path

The losing path says why it lost: "Lower weight". The winner says "Overall best path". That's the receipt. Any time you can, read it before you reason from the columns. (Only these two reason strings have been seen printed; what the other steps print, we haven't confirmed.)

Now the two routers that didn't touch anything. R6 also holds two paths for 172.16.0.0/24: its own from R3, and R4's over iBGP. R5 holds two, one from each edge. Which does each of them pick for 172.16.0.0/24, and for 172.24.0.0/24, where R6 set 333? All three tables:

R4# show bgp ipv4 unicast | begin Network
     Network          Next Hop            Metric LocPrf Weight Path
* i  172.16.0.0/24    192.168.6.6              0    100      0 300 100 i
*>                    10.24.1.2                            222 200 100 i
* i  172.20.0.0/24    192.168.6.6              0    100      0 300 100 i
*>                    10.24.1.2                              0 200 100 i
* i  172.24.0.0/24    192.168.6.6              0    100      0 300 100 i
*>                    10.24.1.2                              0 200 100 i
R5# show bgp ipv4 unicast | begin Network
     Network          Next Hop            Metric LocPrf Weight Path
*>i  172.16.0.0/24    192.168.4.4              0    100      0 200 100 i
* i                   192.168.6.6              0    100      0 300 100 i
*>i  172.20.0.0/24    192.168.4.4              0    100      0 200 100 i
* i                   192.168.6.6              0    100      0 300 100 i
*>i  172.24.0.0/24    192.168.4.4              0    100      0 200 100 i
* i                   192.168.6.6              0    100      0 300 100 i
R6# show bgp ipv4 unicast | begin Network
     Network          Next Hop            Metric LocPrf Weight Path
* i  172.16.0.0/24    192.168.4.4              0    100      0 200 100 i
*>                    10.36.1.3                              0 300 100 i
* i  172.20.0.0/24    192.168.4.4              0    100      0 200 100 i
*>                    10.36.1.3                              0 300 100 i
* i  172.24.0.0/24    192.168.4.4              0    100      0 200 100 i
*>                    10.36.1.3                            333 300 100 i

The weight was set only locally on R4 and R6. It wasn't advertised to any router in AS 400, and every other path shows 0; the > marks the best path.

So weight decides before anything else, and it never leaves the router: a neighbor never learns it. R6 picks its own path through R3 for 172.16.0.0/24, at step 8, because R4's 222 never arrived. R5 picks R4's path for all three prefixes, 172.24.0.0/24 included, because R6's 333 never arrived either. R4, R5 and R6 fall back on later steps for the prefixes whose weight they didn't set; for R5, every column printed ties, so unless its IGP metric to 192.168.6.6 is the lower one, R5 takes 192.168.4.4: at step 9 if the metric to 192.168.4.4 is lower, at step 11, the lower router ID, if the two are equal (our reading, since the metrics aren't printed). The prepending run below can't keep that assumption. R4 is very sure about 222. Nobody else has heard of it.

That's why weight only influences outbound traffic from a router or an AS. From an AS only in the sense that the router's choice changes what it advertises; the number itself stays home. You can set it for specific routes with an inbound route map, or for every route from one neighbor, with set weight weight in a route map or neighbor ip-address weight weight under the address family. And because it's the first step, it's the attribute to use when nothing else should influence the best path for a prefix on that router. Of weight, local preference, AS_Path and MED, the locally significant one is weight.

The lever the whole AS hears: local preference

Second run, same lab, a different attribute. Local preference (LOCAL_PREF) is well-known discretionary and goes with path advertisements throughout an AS. It's a 32-bit value, 0 to 4,294,967,295, giving the preference for leaving the AS toward the prefix. It isn't advertised between eBGP peers, and it's typically used to steer the next hop for outbound traffic. Higher wins. If an edge router doesn't set it with an inbound route map when the prefix arrives, the default, 100, is used in the calculation and included in the advertisements to the other iBGP peers. bgp default local-preference default-local-preference changes that default. A route map sets it for specific routes with set local-preference preference, and because it isn't advertised outside the AS, it steers the other iBGP routers without touching eBGP peers. It can also be set for every route from one neighbor, with an inbound route map on that neighbor whose sequence has no match clause, which the route-map lesson showed matches everything.

The lab again: R4 sets local preference 222 on 172.16.0.0/24 from R2, making it AS 400's preferred path, and R6 sets 333 on 172.24.0.0/24 from R3. The same route maps, with set local-preference in place of set weight.

All three routers now send traffic for 172.16.0.0/24 through R4's link to R2 and for 172.24.0.0/24 through R6's link to R3; 172.20.0.0/24 needs a later step, and the default 100 is advertised to the iBGP routers for the other prefixes. And look at the counts: R4 has only one path for 172.16.0.0/24, R6 has only one for 172.24.0.0/24, and R5 has only one for each of those two. Here are the rows:

R4# show bgp ipv4 unicast | begin Network
     Network          Next Hop            Metric LocPrf Weight Path
*>   172.16.0.0/24    10.24.1.2                     222      0 200 100 i
* i  172.20.0.0/24    192.168.6.6              0    100      0 300 100 i
*>                    10.24.1.2                              0 200 100 i
*>i  172.24.0.0/24    192.168.6.6              0    333      0 300 100 i
*                     10.24.1.2                              0 200 100 i
R5# show bgp ipv4 unicast | begin Network
     Network          Next Hop            Metric LocPrf Weight Path
*>i  172.16.0.0/24    192.168.4.4              0    222      0 200 100 i
* i  172.20.0.0/24    192.168.6.6              0    100      0 300 100 i
*>i                   192.168.4.4              0    100      0 200 100 i
*>i  172.24.0.0/24    192.168.6.6              0    333      0 300 100 i
R6# show bgp ipv4 unicast | begin Network
     Network          Next Hop            Metric LocPrf Weight Path
*>i  172.16.0.0/24    192.168.4.4              0    222      0 200 100 i
*                     10.36.1.3                              0 300 100 i
* i  172.20.0.0/24    192.168.4.4              0    100      0 200 100 i
*>                    10.36.1.3                              0 300 100 i
*>   172.24.0.0/24    10.36.1.3                     333      0 300 100 i

Count them. Each edge still holds its own eBGP path for the prefix the other edge won, as a loser. What's gone are the iBGP copies: R4 has nothing from R6 for 172.16.0.0/24, and R6 nothing from R4 for 172.24.0.0/24.

Not because it lost. That's the misreading to avoid: a router doesn't discard a path that isn't chosen as best; it keeps it in the Loc-RIB, where it could be used later if the best path goes. R4 doesn't have R6's path because R6 took it back. If a router picks a different best path from the one it advertised, it withdraws the old advertisement and advertises the new best. So a router deletes a path only once the advertising neighbor withdraws it, and a router withdraws a path when it's gone or when it has to advertise a different best path. BGP routers don't remove inferior routes; a neighbor has to withdraw them.

So who withdrew what, and when? It happens in three phases, three time cycles. The figure runs the three phases on that lab: the edges' eBGP arrivals, then each exchange as one step, every router's table after each step, and each withdrawal drawn as it's sent. The step rule: each router advertises only its best path; it sends a path to its iBGP peers only if it learned it over eBGP (the iBGP rule); when its best changes to a path it may not send, it withdraws the one it sent; a path stays in a table until the router that sent it withdraws it.

  1. Network drawing: AS 400: R4 faces R2 (AS 200), R6 faces R3 (AS 300, or AS 200 in the MED runs), R5 in the middle, a full mesh of iBGP between their loopbacks, next-hop-self on the edges. Exchanges are drawn as rounds; real routers exchange as updates arrive. R5's IGP metrics aren't printed for this lab, so they're a switch, and no one setting reproduces every run's R5 row.

    Step 1 of 3 Phase I: R2 and R3 send AS 100's three prefixes to R4 and R6, and each edge applies its inbound policy as they arrive.

  2. Network drawing: AS 400: R4 faces R2 (AS 200), R6 faces R3 (AS 300, or AS 200 in the MED runs), R5 in the middle, a full mesh of iBGP between their loopbacks, next-hop-self on the edges. Exchanges are drawn as rounds; real routers exchange as updates arrive. R5's IGP metrics aren't printed for this lab, so they're a switch, and no one setting reproduces every run's R5 row.

    Step 2 of 3 Phase II: each edge's best paths cross the iBGP sessions, and every router compares what it now holds.

  3. Network drawing: AS 400: R4 faces R2 (AS 200), R6 faces R3 (AS 300, or AS 200 in the MED runs), R5 in the middle, a full mesh of iBGP between their loopbacks, next-hop-self on the edges. Exchanges are drawn as rounds; real routers exchange as updates arrive. R5's IGP metrics aren't printed for this lab, so they're a switch, and no one setting reproduces every run's R5 row.

    Step 3 of 3 Phase III: the withdrawals land. An edge whose best path now comes from iBGP may not send it to another iBGP peer, so it withdraws its own.

The three phases, in words:

  • Phase I. The edges process what arrives from outside. R4 receives 172.16.0.0/24 from R2 and sets local preference 222, receives the other two prefixes from R2, has no other paths, so all three are best, and advertises them to R5 and R6, the ones without a local preference with 100; R6 does the mirror with 333 on 172.24.0.0/24. In the Phase I tables below, only R4's 172.16.0.0/24 and R6's 172.24.0.0/24 carry a local preference value.
  • Phase II. R4 and R6 receive each other's routes and compare. R5 receives from both at the same time, so it holds both paths. R4 keeps R2's path for 172.16.0.0/24, ties 172.20.0.0/24 on local preference and goes on to later steps, and takes R6's 172.24.0.0/24 as best, sending R5 and R6 a withdrawal for its path from R2. R5 picks R4's 172.16.0.0/24 and R6's 172.24.0.0/24 and takes 172.20.0.0/24 to later steps, holding both paths for every prefix. R6 mirrors R4: R4's path for 172.16.0.0/24 wins, so R6 withdraws its path from R3; it ties 172.20.0.0/24; it keeps its own 172.24.0.0/24.
  • Phase III. The withdrawals land. R4 and R5 remove R6's 172.16.0.0/24, and R5 and R6 remove R4's 172.24.0.0/24. The final state is the table above.

The start, Phase I, before the edges have heard each other:

R4# show bgp ipv4 unicast | begin Network
     Network          Next Hop            Metric LocPrf Weight Path
*>   172.16.0.0/24    10.24.1.2                     222      0 200 100 i
*>   172.20.0.0/24    10.24.1.2                              0 200 100 i
*>   172.24.0.0/24    10.24.1.2                              0 200 100 i
R6# show bgp ipv4 unicast | begin Network
     Network          Next Hop            Metric LocPrf Weight Path
*>   172.16.0.0/24    10.36.1.3                              0 300 100 i
*>   172.20.0.0/24    10.36.1.3                              0 300 100 i
*>   172.24.0.0/24    10.36.1.3                     333      0 300 100 i

One path per prefix on each edge, its own, and every one of them best. R5 holds nothing yet.

The middle moment, after Phase II and before the withdrawals arrive, R5's rows as one capture prints them:

R5# show bgp ipv4 unicast | begin Network
     Network         Next Hop            Metric LocPrf Weig
*>i 172.16.0.0/24    192.168.4.4              0    222
* i                  192.168.4.4              0    100
* i 172.20.0.0/24    192.168.6.6              0    100
*>i                  192.168.6.6              0    100
*>i 172.24.0.0/24    192.168.6.6              0    333
* i                  192.168.4.4              0    100

Two things are wrong in that box, and both are worth catching. The second 172.16.0.0/24 row, local preference 100, can't come from 192.168.4.4: R4's path is the 222 one, and R5 holds one path from each edge, so that row is R6's path, next hop 192.168.6.6. And the two 172.20.0.0/24 rows print the same next hop twice; the final state lists them as 192.168.6.6 and 192.168.4.4.

Born here: local origination and AIGP

Step 3 asks whether the route was originated locally. The preference runs: routes advertised locally, then networks aggregated locally, then routes received from BGP peers.

You'll rarely see step 3 decide on a Cisco router, and the route-advertisement lesson showed why. A router writes weight 32,768 on its own paths and 0 on learned ones, so R1's own 10.12.1.0/24 beat R2's copy at step 1, not step 3. And when a route map gives R2's path weight 65,200, R2's path wins over R1's own. Step 3 gets a turn when the weights tie: two local paths, a network and an aggregate, or a learned path whose weight someone raised to 32,768. That also explains why, in the iBGP lesson's chain, R1 keeps its own 10.12.1.0/24, next hop 0.0.0.0, over the copy R2 advertised: if R1 originated it, its weight decides at step 1.

Step 4 is AIGP, the Accumulated Interior Gateway Protocol attribute: optional and non-transitive, carried with advertisements throughout an AS. Why would BGP want a metric at all? IGPs pick the lowest path metric but can't scale like BGP, and BGP treats an AS as one domain of routing policy; it doesn't use path metrics, both for scale and because each AS may calculate metrics with its own policy. AIGP lets BGP keep a conceptual path metric across several ASs, each with its own IGP, and that works because all those ASs are under one domain's control, with consistent BGP and IGP policies. Picture AS 100, 200 and 300 belonging to one service provider: AIGP is enabled on the sessions between all the routers, the IGPs are redistributed into BGP, and the AIGP metric crosses all three ASs for the best-path calculation. That's how an attribute carried throughout an AS ends up crossing three: it crosses a boundary only between peers that both have AIGP switched on, inside one administration.

Network drawing: AS 100 · IGP 1, AS 200 · IGP 2, AS 300 · IGP 3, AIGP on every BGP session: the path metric crosses all three ASs, R1, R2, R3, R4, R5, R6, R7

Three ASs under one provider. The dashed R3–R4 line is an iBGP session.

The two ends have to agree: AIGP metrics are only included between AIGP-enabled peers, switched on per neighbor with neighbor ip-address aigp under the address family. The metric is a 32-bit value, set during redistribution or on receipt with a route map's set aigp-metric {igp-metric | metric}; igp-metric takes the redistributing router's IGP metric for the route, and static routes and network advertisements fill it with the path metric to the route's next hop. (RFC 7311 carries the value as an unsigned 64-bit number on the wire; the 32-bit range is for the configured value, which we haven't confirmed.)

Then the comparison rules:

  • a path with an AIGP metric beats a path without one;
  • if the next hop needs a recursive lookup, the path gets a derived metric that adds the distance to the next hop, so the cost to the BGP edge router counts: derived AIGP metric = original AIGP metric + next-hop AIGP metric, added again for every further lookup;
  • if several AIGP paths exist and one next-hop address carries an AIGP metric and the other doesn't, the path without isn't used;
  • paths are compared on the derived metric (recursive next hop) or the actual metric (non-recursive), lower wins;
  • when R2 advertises an AIGP path it learned from R1 and changes the next hop to itself, it adds the IGP distance between R1 and R2 to the metric.

RFC 7311 agrees on the position: the AIGP comparison runs before any of RFC 4271's tie-breakers, and if any path has the attribute, the paths without it are removed first. The RFC's defaults: AIGP enabled on iBGP sessions, and disabled on eBGP sessions outside a confederation; here you enable it per neighbor, and what a current router does by default, we haven't confirmed. The troubleshooting walk below treats AIGP as checked only if it's configured. So: after local origination comes AIGP.

Carried from outside: AS_Path and origin

Third run. The next factor is AS_Path length, which usually tracks the number of AS hops: the shorter path wins. Two counting rules: a confederation's AS_CONFED_SEQUENCE isn't counted, and an aggregate's AS_SET counts as one entry however many ASNs it holds.

Prepending ASNs makes the AS_Path longer and the path less attractive, with set as-path prepend as-number in a route map, and typically the network's owner prepends its own ASN. In general a prepended path isn't chosen, because its path is longer than the one without; you influence inbound traffic by prepending what you send to other ASs, and outbound traffic by prepending what you receive from them. The lab uses the second: R4 prepends 222 210 to 172.24.0.0/24 from R2, and R6 prepends 333 321 to 172.16.0.0/24 from R3, each making that path the least preferred for AS 400. (The ASNs prepended aren't AS 400's. That fits: the owner's own ASN is the usual choice when you prepend what you send; when you prepend what you receive, the path only has to be longer.) The configuration is an inbound route map on each edge whose one matched prefix gets set as-path prepend.

All three routers pick R2's path for 172.16.0.0/24, AS_Path length 2 against 4, and R3's for 172.24.0.0/24. The tables:

R4# show bgp ipv4 unicast | begin Network
     Network          Next Hop            Metric LocPrf Weight Path
*>   172.16.0.0/24    10.24.1.2                              0 200 100 i
*>   172.20.0.0/24    10.24.1.2                              0 200 100 i
* i                   192.168.6.6              0    100      0 300 100 i
*    172.24.0.0/24    10.24.1.2                              0 222 210 200 100 i
*>i                   192.168.6.6              0    100      0 300 100 i
R5# show bgp ipv4 unicast | begin Network
     Network          Next Hop            Metric LocPrf Weight Path
*>i  172.16.0.0/24    192.168.4.4              0    100      0 200 100 i
* i  172.20.0.0/24    192.168.4.4              0    100      0 200 100 i
*>i                   192.168.6.6              0    100      0 300 100 i
*>i  172.24.0.0/24    192.168.6.6              0    100      0 300 100 i
R6# show bgp ipv4 unicast | begin Network
     Network          Next Hop            Metric LocPrf Weight Path
*>i  172.16.0.0/24    192.168.4.4              0    100      0 200 100 i
*                     10.36.1.3                              0 333 321 300 100 i
* i  172.20.0.0/24    192.168.4.4              0    100      0 200 100 i
*>                    10.36.1.3                              0 300 100 i
*>   172.24.0.0/24    10.36.1.3                              0 300 100 i

R6 holds R4's 172.16.0.0/24 as best and its own prepended path as the loser; R4 the mirror for 172.24.0.0/24; R5 one path for each, because each edge withdrew the path it no longer uses.

Now the router nobody touched, on the prefix nobody touched. What should R5 pick for 172.20.0.0/24? Both paths have the same weight, local preference, AS_Path length, origin and MED, and both are internal.

By the printed columns, R5 should pick what it picked in the weight and local-preference runs, R4's path, on the router ID, unless its IGP metric to the two loopbacks differs. The table prints *>i on R6's path, 192.168.6.6, and so does the origin run, next. Nothing printed explains the switch. It's reproduced only if R5's IGP metric to 192.168.6.6 is lower than to 192.168.4.4, step 9, a number never shown. And that can't be the lab the weight run read. The weight and local-preference runs need R5's metric to 192.168.6.6 to be no lower than to 192.168.4.4; the prepending and origin runs need it lower. No single setting reproduces them all, so these tables don't come from one unchanged lab: either the lab changed between runs, or the outputs weren't all taken from one consistent lab. When the printed columns all tie, the deciding input is often one you haven't printed. best-path-reason would have named it.

Fourth run: origin. It's the next decision factor, and a well-known mandatory attribute: on a Cisco router a network statement sets the IGP origin, i, and redistribution sets incomplete, ?. The order runs IGP (most preferred), then EGP, then incomplete (least). A route map's set origin {igp | incomplete} changes it, and the EGP origin can't be set by hand. Spelled out: IGP better than EGP better than incomplete, nothing to do with iBGP against eBGP; IGP means the route came from a network mask (or aggregate-address) command, incomplete means it was redistributed, and EGP means it came from EGP, BGP's predecessor.

The lab: AS 100 advertises all three prefixes with the IGP origin; R4 sets incomplete on 172.24.0.0/24 from R2 and R6 on 172.16.0.0/24 from R3, making each the least preferred path for the AS, with the same inbound route maps and set origin incomplete on the one matched prefix. A path with an incomplete origin is never best, because IGP beats incomplete, and the origin codes sit at the far right, after the AS_Path. This time with the header:

R4# show bgp ipv4 unicast
BGP table version is 21, local router ID is 192.168.4.4
Status codes: s suppressed, d damped, h history, * valid, >
             r RIB-failure, S Stale, m multipath, b backup-
             x best-external, a additional-path, c RIB-comp
             t secondary path, L long-lived-stale,
Origin codes: i - IGP, e - EGP, ? - incomplete
RPKI validation codes: V valid, I invalid, N Not found
! the status lines are cut at the right edge
     Network          Next Hop            Metric LocPrf Weight Path
*>   172.16.0.0/24    10.24.1.2                              0 200 100 i
* i  172.20.0.0/24    192.168.6.6              0    100      0 300 100 i
*>                    10.24.1.2                              0 200 100 i
*>i  172.24.0.0/24    192.168.6.6              0    100      0 300 100 i
*                     10.24.1.2                              0 200 100 ?
R6# show bgp ipv4 unicast | begin Network
     Network          Next Hop            Metric LocPrf Weight Path
*>i  172.16.0.0/24    192.168.4.4              0    100      0 200 100 i
*                     10.36.1.3                              0 300 100 ?
* i  172.20.0.0/24    192.168.4.4              0    100      0 200 100 i
*>                    10.36.1.3                              0 300 100 i
*>   172.24.0.0/24    10.36.1.3                              0 300 100 i

You've read this legend before, so only a few things in it are new. The local router ID, 192.168.4.4, is R4's own: the value R5 and R6 compare at step 11 when they hold R4's path. And the status codes include m for multipath, which no table here prints, because no example turns multipath on (below). The RPKI validation codes aren't used here. The rows match the prepending run row for row, with the losing rows now ending in ? instead of a longer path: R4's 172.24.0.0/24 via 10.24.1.2, R6's 172.16.0.0/24 via 10.36.1.3. R5's table, not shown, matches the prepending run's, including its unexplained 172.20.0.0/24.