Skip to content

IntermediateLesson 6 of 9

Choosing the best route ​

A router that runs BGP often hears about the same prefix from several neighbors: two transits, a peer at an exchange, and your other router. It puts only one of those routes in its routing table, and all traffic to that prefix goes that way. To choose, it compares the routes by a fixed list of attributes, one at a time. The first attribute on which they differ decides.

The order of comparison ​

Every route that BGP carries has a few attributes besides its prefix. These are the ones that decide here:

  • Local preference: a number your own routers give a route as it comes in. You set it, and it stays inside your AS.
  • AS path: the list of ASes the announcement passed through, as in BGP.
  • A BGP attribute that says how a route first entered BGP. IGP means the network announced it itself, and incomplete means it was copied in from another source, such as static routes. EGP, from the protocol before BGP, is almost never seen.: how the route first entered BGP, written as IGP, EGP, or incomplete.
  • MED: the Multi-exit discriminator: a number a neighboring AS puts on the routes it sends you, to say which of the links between you it wants you to use. Lower wins..

Only the routes that your import policy accepted take part. Of these, the router keeps the routes that are best on the first attribute. Then it compares only those by the next attribute, and so on, until one route is left:

  1. Highest local preference.
  2. Shortest AS path.
  3. The best origin, where IGP comes first, then EGP, then incomplete.
  4. Lowest MED.
  5. A route learned over eBGP, from another AS, before one learned over iBGP, from your own router.
  6. A tie-break: the lowest A number, written like an IPv4 address, that a router is known by to its BGP and OSPF neighbors. of the neighbor that sent the route.

This order is simplified

Real routers follow a longer list, and platforms differ in the details. Some check a weight of their own before local preference. Most compare MED only between routes from the same neighboring AS, but this lesson compares it between all routes. Between steps 5 and 6, many routers prefer the route whose next hop is closest inside your network. Some prefer the route they have held longest. Read your platform's documentation before you rely on anything below the AS path.

Try itPick the best of four routes

Four routes to 203.0.113.0/24, a network of Cloudflare, AS13335

Hurricane Electric, transiteBGP

AS path 6939 13335
Router ID 192.0.2.1

Out at step 2, AS path length

Cogent, transiteBGP

AS path 174 13335
Router ID 192.0.2.2

Out at step 2, AS path length

Cloudflare, peer at DE-CIXeBGP

AS path 13335
Router ID 192.0.2.3

Best route

edge-02, your other router, from ArelioniBGP

AS path 1299 13335
Router ID 198.51.100.2

Out at step 2, AS path length

  1. 1

    Local preference: highest wins

    Hurricane Electric 100, Cogent 100, Cloudflare 100, edge-02 100. A tie, so on to the next step.

  2. 2

    AS path length: shortest wins

    Hurricane Electric 2 ASes, Cogent 2 ASes, Cloudflare 1 AS, edge-02 2 ASes. Cloudflare is chosen.

  3. 3

    Origin: IGP, then EGP, then incomplete

    Not needed.

  4. 4

    MED: lowest wins

    Not needed.

  5. 5

    eBGP or iBGP: eBGP wins over iBGP

    Not needed.

  6. 6

    Tie-break: lowest router ID wins

    Not needed.

Cloudflare wins at step 2, AS path length: its AS path, 1 AS long, is the shortest.

Try these

  • To do Cogent is your cheapest contract. Make it the best route.
  • To do Make the choice come down to step 5, eBGP over iBGP.
  • To do Make the choice come down to the tie-break.
edge-01.ts, part of it
policies: {
    sanity: presets.bgp_sanity,
    "hurricane-electric-import": [{ call: "sanity" }, { set: { local_pref: 100 }, then: "accept" }],
    "cogent-import": [{ call: "sanity" }, { set: { local_pref: 100 }, then: "accept" }],
    "exchange-import": [{ call: "sanity" }, { set: { local_pref: 100 }, then: "accept" }],
},
// edge-02 sets the local preference of its route in its own import policy.

At the start, Cloudflare's route from the exchange wins at step 2. Its path is one AS long, because Cloudflare owns the prefix. Every route had the same local preference, so step 1 changed nothing.

Now look at what it took to make Cogent win: one change of local preference, from 100 to anything higher. Its longer path didn't matter, because step 2 is never reached once step 1 has a winner. That is the idea of the order: an earlier step always beats a later one, however large the difference there.

CheckRoute 1 has local preference 100 and an AS path of 2 ASes. Route 2 has local preference 150 and an AS path of 4 ASes. Which one does the router use?

Local preference decides how traffic leaves ​

When a device in your network sends traffic to the internet, your routers choose the way out. Local preference is how you make that choice on purpose, instead of leaving it to the length of the AS path.

You set it on import, per neighbor, with a set in a policy rule. The edge router example does it for its transit and its exchange. Here are its two import policies, in routing.policies:

ts
sanity: presets.bgp_sanity,
// A route from the exchange beats the same route from the transit: the higher local preference wins.
"transit-import": [
    { call: "sanity" },
    { description: "Bogons", if: { prefix_set: "bogons-v4" }, then: "reject" },
    { description: "Bogons", if: { prefix_set: "bogons-v6" }, then: "reject" },
    { if: { prefix_set: "ours" }, then: "reject" },
    { communities: "import" },
    { set: { local_pref: 100 }, then: "accept" },
],
"exchange-import": [
    { call: "sanity" },
    { description: "Bogons", if: { prefix_set: "bogons-v4" }, then: "reject" },
    { description: "Bogons", if: { prefix_set: "bogons-v6" }, then: "reject" },
    { if: { prefix_set: "ours" }, then: "reject" },
    { communities: "import" },
    { set: { local_pref: 200 }, then: "accept" },
],

Every route from the exchange gets 200, and the same route from the transit gets 100. So traffic to a network that is at both takes the exchange. You usually pay a transit for every bit it carries, while an exchange port usually costs a fixed fee. The networks there are often closer, too.

A common pattern gives routes from customers the highest value, then peers, then transit. The numbers are your choice: only their order matters. The other rules decide which routes are accepted at all, and Route policies explains them.

Local preference travels between your own routers over iBGP. That is why the route from edge-02 in the widget had one. It is never sent to another AS.

CheckYou give routes from the exchange local preference 200. Which routers use that number?

Prepending gives a hint for traffic coming in ​

Traffic coming to you is a different matter. Each network on the way chooses its own best route to your prefix, with its own local preference, and you can't set that for them.

What you can change is what you announce. Adding your own AS number to the AS path several more times before you announce a route, so the path looks longer and less traffic comes in that way. makes the path through one neighbor look longer. A network that reaches you both ways, and decides by the length of the path, then prefers the other way in.

Try itSteer the traffic coming in
Your networkAS64500Hurricane ElectricAS6939CogentAS174ArelionAS1299
Prepend toward Hurricane Electric
Prepend toward Cogent
What Arelion itself prefers
The routes Arelion has for your 198.51.100.0/24
ViaAS pathLengthLocal pref
Hurricane Electric (chosen)6939 645002100
Cogent174 645002100

Send traffic first. Then prepend toward the transit it came over, and send again. Last, give Arelion a preference of its own.

edge-01.ts, your exports
"hurricane-electric-export": [
    { description: "Announce 198.51.100.0/24", if: { prefix: "198.51.100.0/24" }, then: "accept" },
    { description: "Nothing else leaves", then: "reject" },
],
"cogent-export": [
    { description: "Announce 198.51.100.0/24", if: { prefix: "198.51.100.0/24" }, then: "accept" },
    { description: "Nothing else leaves", then: "reject" },
],

Prepending once was enough to move the traffic, because Arelion compared the AS path. Once Arelion set a local preference of its own, nothing you prepended made a difference. It decided at step 1, before it looked at the path.

That is the limit of prepending. It is a hint, and it works only on networks that reach the AS path step. Many transit providers prefer routes from their own customers by local preference. So prepending toward a transit often moves less traffic than you hoped. Some providers let you ask them for a lower preference with a community, a tag you meet in the next lesson.

In Circuit, prepending is set: { prepend: 2 } on the export rule that announces your prefix, as in the widget's file. The number is how many more times your AS is added, so the far network sees your AS three times in all. The same set can also have med: a number you give a neighbor that you reach over several links, to say which link it should send through.

CheckYou prepend three times toward Hurricane Electric, but a far network still sends all its traffic to you through Hurricane Electric. How can that be?

Discussion

Stuck, or found a mistake? Ask here. You sign in with GitHub.

Released under the Business Source License 1.1.