AS path 6939 13335
Router ID 192.0.2.1
Out at step 2, AS path length
IntermediateLesson 6 of 9
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.
Every route that BGP carries has a few attributes besides its prefix. These are the ones that decide here:
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:
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.
Four routes to 203.0.113.0/24, a network of Cloudflare, AS13335
AS path 6939 13335
Router ID 192.0.2.1
Out at step 2, AS path length
AS path 174 13335
Router ID 192.0.2.2
Out at step 2, AS path length
AS path 13335
Router ID 192.0.2.3
Best route
AS path 1299 13335
Router ID 198.51.100.2
Out at step 2, AS path length
Local preference: highest wins
Hurricane Electric 100, Cogent 100, Cloudflare 100, edge-02 100. A tie, so on to the next step.
AS path length: shortest wins
Hurricane Electric 2 ASes, Cogent 2 ASes, Cloudflare 1 AS, edge-02 2 ASes. Cloudflare is chosen.
Origin: IGP, then EGP, then incomplete
Not needed.
MED: lowest wins
Not needed.
eBGP or iBGP: eBGP wins over iBGP
Not needed.
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
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.
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:
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.
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.
| Via | AS path | Length | Local pref |
|---|---|---|---|
| Hurricane Electric (chosen) | 6939 64500 | 2 | 100 |
| Cogent | 174 64500 | 2 | 100 |
Send traffic first. Then prepend toward the transit it came over, and send again. Last, give Arelion a preference of its own.
"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.
Stuck, or found a mistake? Ask here. You sign in with GitHub.