Supported Platforms
Related Documentation
- ACX, J, M, MX, SRX Series
- Understanding External BGP Peering Sessions
- BGP Configuration Overview
Example: Configuring BGP Path Selection
Understanding BGP Path Selection
For each prefix in the routing table, the routing protocol process selects a single best path. After the best path is selected, the route is installed in the routing table. The best path becomes the active route if the same prefix is not learned by a protocol with a lower (more preferred) global preference value, also known as the administrative distance. The algorithm for determining the active route is as follows:
- Verify that the next hop can be resolved.
- Choose the path with the lowest preference value (routing
protocol process preference).
Routes that are not eligible to be used for forwarding (for example, because they were rejected by routing policy or because a next hop is inaccessible) have a preference of –1 and are never chosen.
- Prefer the path with higher local preference.
For non-BGP paths, choose the path with the lowest preference2 value.
- If the accumulated interior gateway protocol (AIGP) attribute is enabled, prefer the path with the lower AIGP attribute.
- Prefer the path with the shortest autonomous system (AS)
path value (skipped if the as-path-ignore statement is
configured).
A confederation segment (sequence or set) has a path length of 0. An AS set has a path length of 1.
- Prefer the route with the lower origin code.
Routes learned from an IGP have a lower origin code than those learned from an exterior gateway protocol (EGP), and both have lower origin codes than incomplete routes (routes whose origin is unknown).
- Prefer the path with the lowest multiple exit discriminator
(MED) metric.
Depending on whether nondeterministic routing table path selection behavior is configured, there are two possible cases:
- If nondeterministic routing table path selection behavior is not configured (that is, if the path-selection cisco-nondeterministic statement is not included in the BGP configuration), for paths with the same neighboring AS numbers at the front of the AS path, prefer the path with the lowest MED metric. To always compare MEDs whether or not the peer ASs of the compared routes are the same, include the path-selection always-compare-med statement.
- If nondeterministic routing table path selection behavior is configured (that is, the path-selection cisco-nondeterministic statement is included in the BGP configuration), prefer the path with the lowest MED metric.
Confederations are not considered when determining neighboring ASs. A missing MED metric is treated as if a MED were present but zero.
Note: MED comparison works for single path selection within an AS (when the route does not include an AS path), though this usage Is uncommon.
By default, only the MEDs of routes that have the same peer autonomous systems (ASs) are compared. You can configure routing table path selection options to obtain different behaviors.
- Prefer strictly internal paths, which include IGP routes and locally generated routes (static, direct, local, and so forth).
- Prefer strictly external BGP (EBGP) paths over external paths learned through internal BGP (IBGP) sessions.
- Prefer the path whose next hop is resolved through the
IGP route with the lowest metric.
Note: A path is considered a BGP equal-cost path (and will be used for forwarding) if a tie-break is performed after the previous step. All paths with the same neighboring AS, learned by a multipath-enabled BGP neighbor, are considered.
BGP multipath does not apply to paths that share the same MED-plus-IGP cost yet differ in IGP cost. Multipath path selection is based on the IGP cost metric, even if two paths have the same MED-plus-IGP cost.
- If both paths are external, prefer the currently active
path to minimize route-flapping. This rule is not used if any one
of the following conditions is true:
- path-selection external-router-id is configured.
- Both peers have the same router ID.
- Either peer is a confederation peer.
- Neither path is the current active path.
- Prefer a primary route over a secondary route. A primary route is one that belongs to the routing table. A secondary route is one that is added to the routing table through an export policy.
- Prefer the path from the peer with the lowest router ID. For any path with an originator ID attribute, substitute the originator ID for the router ID during router ID comparison.
- Prefer the path with the shortest cluster list length. The length is 0 for no list.
- Prefer the path from the peer with the lowest peer IP address.
Routing Table Path Selection
The third step of the algorithm, by default, evaluates the length of the AS path and determines the active path. You can configure an option that enables Junos OS to skip this third step of the algorithm by including the as-path-ignore option.
![]() | Note: The as-path-ignore option is not supported for routing instances. |
To configure routing table path selection behavior, include the path-selection statement:
For a list of hierarchy levels at which you can include this statement, see the statement summary section for this statement.
Routing table path selection can be configured in one of the following ways:
- Emulate the Cisco IOS default behavior (cisco-non-deterministic). This mode evaluates routes in the order that they are received
and does not group them according to their neighboring AS. With cisco-non-deterministic mode, the active path is always first.
All inactive, but eligible, paths follow the active path and are maintained
in the order in which they were received, with the most recent path
first. Ineligible paths remain at the end of the list.
As an example, suppose you have three path advertisements for the 192.168.1.0 /24 route:
- Path 1—learned through EBGP; AS Path of 65010; MED of 200
- Path 2—learned through IBGP; AS Path of 65020; MED of 150; IGP cost of 5
- Path 3—learned through IBGP; AS Path of 65010; MED of 100; IGP cost of 10
These advertisements are received in quick succession, within a second, in the order listed. Path 3 is received most recently, so the routing device compares it against path 2, the next most recent advertisement. The cost to the IBGP peer is better for path 2, so the routing device eliminates path 3 from contention. When comparing paths 1 and 2, the routing device prefers path 1 because it is received from an EBGP peer. This allows the routing device to install path 1 as the active path for the route.
Note: We do not recommend using this configuration option in your network. It is provided solely for interoperability to allow all routing devices in the network to make consistent route selections.
- Always comparing MEDs whether or not the peer ASs of the compared routes are the same (always-compare-med).
- Override the rule that If both paths are external, the currently active path is preferred (external-router-id). Continue with the next step (Step 12) in the path-selection process.
- Adding the IGP cost to the next-hop destination to the
MED value before comparing MED values for path selection (med-plus-igp).
BGP multipath does not apply to paths that share the same MED-plus-IGP cost, yet differ in IGP cost. Multipath path selection is based on the IGP cost metric, even if two paths have the same MED-plus-IGP cost.
Effects of Advertising Multiple Paths to a Destination
BGP advertises only the active path, unless you configure BGP to advertise multiple paths to a destination.
Suppose a routing device has in its routing table four paths to a destination and is configured to advertise up to three paths (add-path send path-count 3). The three paths are chosen based on path selection criteria. That is, the three best paths are chosen in path-selection order. The best path is the active path. This path is removed from consideration and a new best path is chosen. This process is repeated until the specified number of paths is reached.
Example: Ignoring the AS Path Attribute When Selecting the Best Path
If multiple BGP routes to the same destination exist, BGP selects the best path based on the route attributes of the paths. One of the route attributes that affects the best-path decision is the length of the AS paths of each route. Routes with shorter AS paths are preferred over those with longer AS paths. Although not typically practical, some scenarios might require that the AS path length be ignored in the route selection process. This example shows how to configure a routing device to ignore the AS path attribute.
Requirements
No special configuration beyond device initialization is required before you configure this example.
Overview
On externally connected routing devices, the purpose of skipping the AS path comparison might be to force an external BGP (EBGP) versus internal BGP (IBGP) decision to remove traffic from your network as soon as possible. On internally connected routing devices, you might want your IBGP-only routers to default to the local externally connected gateway. The local IBGP-only (internal) routers skip the AS path comparison and move down the decision tree to use the closest interior gateway protocol (IGP) gateway (lowest IGP metric). Doing this might be an effective way to force these routers to use a LAN connection instead of their WAN connection.
![]() | Caution: When you include the as-path-ignore statement on a routing device in your network, you might need to include it on all other BGP-enabled devices in your network to prevent routing loops and convergence issues. This is especially true for IBGP path comparisons. |
In this example, Device R2 is learning about the loopback interface address on Device R4 (4.4.4.4/32) from Device R1 and Device R3. Device R1 is advertising 4.4.4.4/32 with an AS-path of 1 5 4, and Device R3 is advertising 4.4.4.4/32 with an AS-path of 3 4. Device R2 selects the path for 4.4.4.4/32 from Device R3 as the best path because the AS path is shorter than the AS path from Device R1.
This example modifies the BGP configuration on Device R2 so that the AS-path length is not used in the best-path selection.
Device R1 has a lower router ID (1.1.1.1) than Device R3 (1.1.1.1). If all other path selection criteria are equal (or, as in this case, ignored), the route learned from Device R1 is used. Because the AS-path attribute is being ignored, the best path is toward Device R1 because of its lower router ID value.
Figure 1 shows the sample topology.
Figure 1: Topology for Ignoring the AS-Path Lengh

Configuration
CLI Quick Configuration
To quickly configure this example, copy the following commands, paste them into a text file, remove any line breaks, change any details necessary to match your network configuration, and then copy and paste the commands into the CLI at the [edit] hierarchy level.
Device R1
Device R2
Device R3
Device R4
Device R5
Configuring Device R2
Step-by-Step Procedure
The following example requires you to navigate various levels in the configuration hierarchy. For information about navigating the CLI, see Using the CLI Editor in Configuration Mode in the CLI User Guide.
To configure Device R2:
- Configure the interfaces.[edit interfaces]user@R2# set fe-1/2/0 unit 2 family inet address 192.168.10.2/24user@R2# set fe-1/2/1 unit 3 family inet address 192.168.20.2/24user@R2# set lo0 unit 2 family inet address 2.2.2.2/32
- Configure EBGP.[edit protocols bgp group ext]user@R2# set type externaluser@R2# set export send-directuser@R2# set export send-staticuser@R2# set export send-localuser@R2# set neighbor 192.168.10.1 peer-as 1user@R2# set neighbor 192.168.20.1 peer-as 3
- Configure the autonomous system (AS) path attribute to
be ignored in the Junos OS path selection algorithm.[edit protocols bgp]user@R2# set path-selection as-path-ignore
- Configure the routing policy.[edit policy-options]user@R2# set policy-statement send-direct term 1 from protocol directuser@R2# set policy-statement send-direct term 1 then acceptuser@R2# set policy-statement send-local term 1 from protocol localuser@R2# set policy-statement send-local term 1 then acceptuser@R2# set policy-statement send-static term 1 from protocol staticuser@R2# set policy-statement send-static term 1 then accept
- Configure some static routes.[edit routing-options static]user@R2# set route 192.168.50.0/24 next-hop 192.168.10.1user@R2# set route 192.168.40.0/24 next-hop 192.168.10.1user@R2# set route 192.168.30.0/24 next-hop 192.168.20.1
- Configure the autonomous system (AS) number and the router
ID.[edit routing-options]user@R2# set router-id 2.2.2.2user@R2# set autonomous-system 2
Results
From configuration mode, confirm your configuration by entering the show interfaces, show policy-options, show protocols, and show routing-options commands. If the output does not display the intended configuration, repeat the instructions in this example to correct the configuration.
If you are done configuring the device, enter commit from configuration mode. Repeat the configuration on the other devices in the network, changing the interface names and IP addresses, as needed.
Verification
Confirm that the configuration is working properly.
Checking the Neighbor Status
Purpose
Make sure that from Device R2, the active path to get to AS 4 is through AS 1 and AS 5, not through AS 3.
![]() | Note: To verify the functionality of the as-path-ignore statement, you might need to run the restart routing command to force reevaluation of the active path. This is because for BGP, if both paths are external, the Junos OS behavior is to prefer the currently active path. This behavior helps to minimize route-flapping. Use caution when restarting the routing protocol process in a production network. |
Action
From operational mode, enter the restart routing command.
user@R2> restart routing
Routing protocols process started, pid 49396
From operational mode, enter the show route 4.4.4.4 protocol bgp command.
user@R2> show route 4.4.4.4 protocol bgp
inet.0: 12 destinations, 25 routes (12 active, 0 holddown, 4 hidden) + = Active Route, - = Last Active, * = Both 4.4.4.4/32 *[BGP/170] 00:00:12, localpref 100 AS path: 1 5 4 I > to 192.168.10.1 via fe-1/2/0.2 [BGP/170] 00:00:08, localpref 100 AS path: 3 4 I > to 192.168.20.1 via fe-1/2/1.3
Meaning
The asterisk (*) is next to the path learned from R1, meaning that this is the active path. The AS path for the active path is 1 5 4, which is longer than the AS path (3 4) for the nonactive path learned from Router R3.
Related Documentation
- ACX, J, M, MX, SRX Series
- Understanding External BGP Peering Sessions
- BGP Configuration Overview
Published: 2013-07-09
Supported Platforms
Related Documentation
- ACX, J, M, MX, SRX Series
- Understanding External BGP Peering Sessions
- BGP Configuration Overview