This lab continues from BGP peering and next-hop resolution. We add a second border router to AS 65100, so the same external prefix arrives over two paths and the routers have to select one. We find out which step of the path-selection algorithm picked the default path, then set BGP attributes to select a different one.
Overview
R3 becomes a second border router. It gets a directly connected EBGP session to R4 over 172.16.34.0/30, so 198.51.100.0/24 enters AS 65100 at two points, and AS 65100's loopbacks reach R4 from two points.
We will perform the following tasks:
- Add the second EBGP session between R3 and R4, with next-hop self and the internal-prefix export policy on the new border.
- Confirm that R2 has two IBGP paths to
198.51.100.0/24, and find out why one of them is active. - Use local preference to move the AS exit from R1 to R3, and verify with a traceroute from R2.
- Use MED and AS-path prepend to influence which border R4 uses to enter AS 65100, and verify with a reverse traceroute from H1.
- Look at origin manipulation, and why it is not a traffic-engineering tool.
- List the Junos best-path order and map each step to a field in the CLI.
Topology
R2 shows the outbound selection. It has no EBGP session, so both paths to 198.51.100.0/24 arrive over IBGP, one from R1 and one from R3. R4 makes the inbound selection, because it has two ways to reach AS 65100's loopbacks.
Adding the second border
R3
R3 already has its internal interfaces, OSPF and the IBGP mesh from the previous lab. We add the external interface, the external BGP group, and the two policies R1 already runs.
set interfaces ge-0/0/2 unit 0 family inet address 172.16.34.1/30set protocols bgp group ext-as65200 type externalset protocols bgp group ext-as65200 peer-as 65200set protocols bgp group ext-as65200 neighbor 172.16.34.2set protocols bgp group ext-as65200 export ADV-INTERNALset policy-options policy-statement ADV-INTERNAL term loopbacks from route-filter 10.0.0.0/24 orlongerset policy-options policy-statement ADV-INTERNAL term loopbacks from protocol directset policy-options policy-statement ADV-INTERNAL term loopbacks from protocol ospfset policy-options policy-statement ADV-INTERNAL term loopbacks then acceptset policy-options policy-statement ADV-INTERNAL term reject then rejectset policy-options policy-statement NHS then next-hop selfset protocols bgp group ibgp-65100 export NHSR4
R4 needs the interface and the second neighbor. The neighbor goes into the existing ext-as65100 group, so it inherits peer-as 65100 and the ADV-CUSTOMER export policy.
set interfaces ge-0/0/1 unit 0 family inet address 172.16.34.2/30set protocols bgp group ext-as65100 neighbor 172.16.34.1Verifying the starting state
We commit and check the sessions on R4:
show bgp summaryBoth external peers, 172.16.14.1 (R1) and 172.16.34.1 (R3), should be Established.
An established session does not mean the prefixes are being advertised, so we check what R4 received from each border for R2's loopback:
show route receive-protocol bgp 172.16.14.1 10.0.0.2/32 detailshow route receive-protocol bgp 172.16.34.1 10.0.0.2/32 detailBoth commands should return the prefix. On R2, the other direction:
show route 198.51.100.0/24R2 has two BGP paths to the prefix, one via 10.0.0.1 (R1) and one via 10.0.0.3 (R3). Only one is active, marked with *.
Why R2 selected the path via R1
On R2 the two paths are equal on every attribute compared:
- Local preference is 100 on both, the default.
- AS path is
65200on both, length 1. - Origin is IGP on both. R4 put
198.51.100.0/24into BGP from a direct route, and Junos sets origin to IGP by default, so the AS path ends inI, not?. IOSredistributedoes the opposite. - MED is not set on either path, and a missing MED is treated as 0.
Selection continues to the later steps. Both paths are IBGP on R2, so the EBGP-over-IBGP step does not separate them. R2's OSPF cost to 10.0.0.1 and to 10.0.0.3 is the same, one hop each way around the triangle, so the IGP metric ties as well. The deciding step is the router ID of the advertising peer: R1 is 10.0.0.1, R3 is 10.0.0.3, and lower wins.
We can confirm this on R2:
show route 198.51.100.0/24 detailThe path via R1 is active. The path via R3 shows an Inactive reason: field naming the router-ID comparison, usually Router ID or Not Best in its group - Router ID.
We check that the forwarding plane matches:
traceroute 198.51.100.10 source 10.0.0.2The path is R2, R1, R4, H1. R1 is the exit because its router ID is lower, not because we chose it. We set it explicitly next.
Local preference sets the exit
We want AS 65100 to exit through R3 for 198.51.100.0/24, regardless of router IDs.
Local preference is the first attribute compared after route preference. It defaults to 100, higher wins, and it propagates across IBGP. Because it propagates, setting it on one border makes every router in the AS use the same exit.
We apply an import policy on R3 that raises local preference as the prefix arrives from R4:
set policy-options policy-statement LP-IN term p from route-filter 198.51.100.0/24 exactset policy-options policy-statement LP-IN term p then local-preference 200set policy-options policy-statement LP-IN term p then acceptset protocols bgp group ext-as65200 import LP-INWe commit on R3 and check R2:
show route 198.51.100.0/24 detailshow route receive-protocol bgp 10.0.0.1 198.51.100.0/24The active path is now via 10.0.0.3. The path from R1 is usually absent rather than inactive. R1 selects R3's path as well, so R1's own external path is no longer active, and only the active paths are advertised. R1 therefore withdraws it from its IBGP peers, and the receive-protocol command returns nothing.
R1 shows the comparison:
show route 198.51.100.0/24 detailR1 has its own EBGP path with local preference 100 and R3's IBGP path with local preference 200. The EBGP path is inactive with Inactive reason: Local Preference. R1 forwards across the AS even though it has a working external session. Local preference is compared before the EBGP-over-IBGP step, so it overrides it.
Data plane:
traceroute 198.51.100.10 source 10.0.0.2The path is now R2, R3, R4, H1. The exit moved to R3.
We remove the policy before the next section:
delete protocols bgp group ext-as65200 import LP-INdelete policy-options policy-statement LP-INLocal preference works outbound only. It is stripped when the route is advertised over EBGP, so it never reaches R4 and says nothing about how R4 should reach us. That is the next section.
MED and prepend influence the entrance
R4 has two ways into AS 65100. We use R2's loopback 10.0.0.2/32 as the test prefix, which both borders advertise through ADV-INTERNAL.
R4 uses R1 by default. Both paths already carry MED 1, which Junos copied from the OSPF metric when ADV-INTERNAL exported the loopbacks, so the MED step ties too. R1's path was learned first, and with the attributes tied and both paths external, Junos keeps the older active path to avoid flapping. The inactive path reads Inactive reason: Not Best in its group - Active preferred. If that did not apply, the peer router ID would decide, and R1 wins that too because 10.0.0.1 is lower than 10.0.0.3.
MED advertises a metric to R4. Prepend makes R1's AS path longer. Both move the entrance to R3, at different steps of the algorithm.
MED
MED is advertised to the neighbor and asks it to prefer the lower value. MED is compared only between paths from the same neighbor AS by default. We set it per neighbor with metric-out, high on R1 and low on R3.
On R1:
set protocols bgp group ext-as65200 neighbor 172.16.14.2 metric-out 100On R3:
set protocols bgp group ext-as65200 neighbor 172.16.34.2 metric-out 10We commit both and check R4, which makes the selection:
show route 10.0.0.2/32 detailshow route receive-protocol bgp 172.16.14.1 10.0.0.2/32 detailshow route receive-protocol bgp 172.16.34.1 10.0.0.2/32 detailR4 selects the path with MED 10 via R3 over MED 100 via R1. The two receive-protocol commands show the values as received. The inactive path reads Inactive reason: Not Best in its group - Route Metric or MED comparison, and the Metric fields on the two paths show the values being compared.
We check the data plane from the host, so the traffic enters through the border R4 selected:
ping 10.0.0.2 -c 1trace 10.0.0.2The trace goes R4, R3, R2 instead of R4, R1, R2.
Production designs usually set MED per prefix with a then metric action in a policy. A policy metric overrides metric-out, and any configured metric overrides a MED received with the route. We use metric-out here to keep the value per neighbor.
We remove both values before testing prepend. On R1:
delete protocols bgp group ext-as65200 neighbor 172.16.14.2 metric-outOn R3:
delete protocols bgp group ext-as65200 neighbor 172.16.34.2 metric-outAS-path prepend
Prepend reaches the same result. Instead of a metric, we make R1's advertisement carry a longer AS path, so R4 selects R3 on path length. We add the action to R1's existing ADV-INTERNAL export:
set policy-options policy-statement ADV-INTERNAL term loopbacks then as-path-prepend "65100 65100"We commit on R1. R4 now sees 65100 65100 65100 via R1 and 65100 via R3.
On R4:
show route 10.0.0.2/32 detailR3's path is active. R1's is inactive with Inactive reason: AS path. Same entrance as the MED test, decided one step earlier, because AS path is compared before MED.
We remove the prepend:
delete policy-options policy-statement ADV-INTERNAL term loopbacks then as-path-prependMED versus prepend
MED is non-transitive. R4 uses it but does not pass it on, so it only affects the neighbor AS attached to us. Prepend is part of the AS path, which is transitive, so it can affect selection several ASes away. MED is also compared only between paths from the same neighbor AS unless always-compare-med is set, which we do not use here.
Use prepend when the target is beyond the directly attached neighbor, or when several upstreams are involved. Use MED when one neighbor AS has agreed to act on it and you need per-prefix values.
Origin
Origin is the third comparison: IGP is preferred over EGP, and EGP over Incomplete. We set it on R1's ADV-INTERNAL export term:
set policy-options policy-statement ADV-INTERNAL term loopbacks then origin incompleteOn R4:
show route 10.0.0.2/32 detailshow route receive-protocol bgp 172.16.14.1 10.0.0.2/32 detailshow route receive-protocol bgp 172.16.34.1 10.0.0.2/32 detailR4 sees ? on R1's advertisement and I on R3's, and selects R3 at the origin step.
It works, but origin is not a traffic-engineering tool. It records how the route entered BGP, and changing it hides that. We will remove it:
delete policy-options policy-statement ADV-INTERNAL term loopbacks then originBGP path-selection algorithm
- The BGP next-hop must be reachable and the path must be eligible, including the AS-loop check.
- Lowest route preference. All BGP routes are 170 by default, so this normally ties and step 3 is the first real decision.
- Highest local preference.
- Lowest AIGP metric, if AIGP is enabled.
- Shortest AS path.
- Lowest origin: IGP (0) < EGP (1) < Incomplete (2).
- Lowest MED, between paths from the same neighbor AS by default.
- Prefer strictly internal paths, including IGP and locally generated routes.
- Prefer a path received over EBGP to an external path learned through IBGP.
- Lowest IGP metric to the BGP next-hop.
- With multiple external paths, prefer the older active path to avoid flapping.
- Lowest peer router ID, or the originator ID where that attribute exists.
- Shortest cluster list.
- Lowest peer IP address.
- Prefer a primary route to a secondary route.
Key points:
- Local preference is the first attribute compared after route preference. Default 100, higher wins, well-known discretionary, propagates across IBGP, stripped on EBGP. It sets the AS exit, and because it is compared before the EBGP-over-IBGP step, it can move traffic away from a border's own external session.
- AS path length is compared after local preference. Prepend lengthens it to make an entrance less preferred. In Junos this is
then as-path-prepend "65100 65100"in an export policy. - Origin is IGP < EGP < Incomplete. Junos sets origin to IGP by default for routes it puts into BGP by policy, unlike Cisco's
redistribute, which uses Incomplete. Changing origin for path selection works but is not recommended. - MED is optional non-transitive, lower wins, and by default is compared only between paths from the same neighbor AS. Set it per neighbor with
metric-outor per prefix withthen metric. A missing MED is 0, a policy metric overridesmetric-out, and any configured metric overrides a received one. - MED versus prepend: MED affects only the directly attached neighbor and is not passed on. Prepend rides in the transitive AS path and can affect selection several ASes away.
- Outbound versus inbound. Local preference sets the exit. MED and prepend only influence the entrance, because the neighbor AS runs its own selection and can ignore both.
