Always-Compare-Med

Scenario to verify BGP always-compare-med parameter configuration.

The Multi-Exit Discriminator (MED) is a BGP attribute used to suggest the preferred entry point into an AS when multiple entry points exist. Lower MED values are preferred. By default, BGP only compares MED values between routes learned from the same AS. This means if you receive routes to the same destination from different ASes (for example AS 200 and AS 300), BGP will not use MED to decide which route is better because the routes originated from different autonomous systems.

The always-compare-med parameter changes this behavior. When enabled, BGP compares MED values across all routes regardless of the originating AS. This is useful in environments where you want MED to influence path selection even when routes come from different upstream providers. In this test, DUT0 receives the same route from two eBGP neighbors in different ASes with different MED values, demonstrating that MED comparison only occurs when always-compare-med is enabled.

Test BGP Always Compare MED

Description

This test demonstrates the effect of always-compare-med on BGP path selection. DUT0 (AS 100) receives route 192.168.10.0/24 from two eBGP neighbors: DUT1 (AS 200) with MED 100 and DUT2 (AS 300) with MED 50. Without always-compare-med, BGP does not compare MEDs because the routes come from different ASes, so selection depends on other criteria like router-id. After enabling always-compare-med, BGP compares MEDs and selects the route via DUT2 because 50 is lower than 100.

Scenario

Note

First we configure the topology without always-compare-med to observe default behavior. We wait a few seconds before configuring DUT2 to ensure DUT1 establishes first with DUT0 and becomes the older path.

Step 1: Set the following configuration in DUT0 :

set interfaces ethernet eth0 address 10.10.0.100/24
set interfaces ethernet eth1 address 20.20.0.100/24
set protocols bgp 100 neighbor peer1 remote-address 10.10.0.200
set protocols bgp 100 neighbor peer1 remote-as 200
set protocols bgp 100 neighbor peer1 route-map import PERMIT
set protocols bgp 100 neighbor peer2 remote-address 20.20.0.30
set protocols bgp 100 neighbor peer2 remote-as 300
set protocols bgp 100 neighbor peer2 route-map import PERMIT
set protocols route-map PERMIT rule 1 action permit
set system login user admin authentication encrypted-password '$6$GSjsCj8gHLv$/VcqU6FLi6CT2Oxn0MJQ2C2tqnRDrYKNF8HIYWJp68nvXvPdFccDsT04.WtigUONbKYrgKg8d6rEs8PjljMkH0'

Step 2: Set the following configuration in DUT1 :

set interfaces dummy dum0 address 192.168.10.1/24
set interfaces ethernet eth0 address 10.10.0.200/24
set protocols bgp 200 neighbor peer remote-address 10.10.0.100
set protocols bgp 200 neighbor peer remote-as 100
set protocols bgp 200 neighbor peer route-map export SET-MED
set protocols bgp 200 redistribute connected route-map SET-MED
set protocols route-map SET-MED rule 1 action permit
set protocols route-map SET-MED rule 1 set metric 100
set system login user admin authentication encrypted-password '$6$GSjsCj8gHLv$/VcqU6FLi6CT2Oxn0MJQ2C2tqnRDrYKNF8HIYWJp68nvXvPdFccDsT04.WtigUONbKYrgKg8d6rEs8PjljMkH0'

Step 3: Set the following configuration in DUT2 :

set interfaces dummy dum0 address 192.168.10.1/24
set interfaces ethernet eth1 address 20.20.0.30/24
set protocols bgp 300 neighbor peer remote-address 20.20.0.100
set protocols bgp 300 neighbor peer remote-as 100
set protocols bgp 300 neighbor peer route-map export SET-MED
set protocols bgp 300 redistribute connected route-map SET-MED
set protocols route-map SET-MED rule 1 action permit
set protocols route-map SET-MED rule 1 set metric 50
set system login user admin authentication encrypted-password '$6$GSjsCj8gHLv$/VcqU6FLi6CT2Oxn0MJQ2C2tqnRDrYKNF8HIYWJp68nvXvPdFccDsT04.WtigUONbKYrgKg8d6rEs8PjljMkH0'

Attention

Verify both eBGP sessions establish between DUT0 and its neighbors.

Step 4: Run command protocols bgp show ip summary at DUT0 and check if output matches the following regular expressions:

10.10.0.200.*Established[\s\S]+20.20.0.30.*Established
Show output
IPv4 Unicast Summary:
BGP router identifier 20.20.0.100, local AS number 100 VRF default vrf-id 0
BGP table version 2
RIB entries 3, using 384 bytes of memory
Peers 2, using 47 KiB of memory

Neighbor        LocalAddr       V         AS   MsgRcvd   MsgSent   TblVer  InQ OutQ  Up/Down        State   PfxRcd   PfxSnt Desc
10.10.0.200     10.10.0.100     4        200         6         5        2    0    0 00:00:07  Established        2 (Policy) N/A
20.20.0.30      20.20.0.100     4        300         2         2        0    0    0 00:00:00  Established        0 (Policy) N/A

Total number of neighbors 2

Attention

Verify DUT0 receives route 192.168.10.0/24 from both neighbors with their respective MEDs.

Step 5: Run command protocols bgp show ip at DUT0 and check if output matches the following regular expressions:

192.168.10.0/24\s+(10.10.0.200\s+100[\s\S]+20.20.0.30\s+50|20.20.0.30\s+50[\s\S]+10.10.0.200\s+100)
Show output
BGP table version is 3, local router ID is 20.20.0.100, vrf id 0
Default local pref 100, local AS 100
local address -
Status codes:  s suppressed, d damped, h history, u unsorted, * valid, > best, = multipath,
               i internal, r RIB-failure, S Stale, R Removed
Nexthop codes: @NNN nexthop's vrf id, < announce-nh-self
Origin codes:  i - IGP, e - EGP, ? - incomplete
RPKI validation codes: V valid, I invalid, N Not found

     Network          Next Hop            Metric LocPrf Weight Path
 *u  10.10.0.0/24     10.10.0.200            100             0 200 ?
 *u  20.20.0.0/24     20.20.0.30              50             0 300 ?
 *u  192.168.10.0/24  20.20.0.30              50             0 300 ?
 *u                   10.10.0.200            100             0 200 ?

Displayed 3 routes and 4 total paths

Note

Without always-compare-med, BGP does not compare MEDs from different ASes. The best path is selected based on other criteria. We verify DUT1 route is selected as best (Older Path).

Step 6: Run command protocols bgp show ip 192.168.10.0/24 at DUT0 and check if output matches the following regular expressions:

200[\s\S]+10.10.0.200 from 10.10.0.200[\s\S]+best \(Older Path\)
Show output
BGP routing table entry for 192.168.10.0/24, version 2
Paths: (2 available, best #2, table default)
  Not advertised to any peer
  300
    20.20.0.30 from 20.20.0.30 (192.168.10.1)
      Origin incomplete, metric 50, valid, external
      Last update: Thu Mar  5 14:59:11 2026
  200
    10.10.0.200 from 10.10.0.200 (192.168.10.1)
      Origin incomplete, metric 100, valid, external, best (Older Path)
      Last update: Thu Mar  5 14:59:04 2026

Note

Now we enable always-compare-med so BGP compares MEDs regardless of the originating AS.

Step 7: Modify the following configuration lines in DUT0 :

set protocols bgp 100 parameters always-compare-med

Attention

Verify eBGP sessions remain established after configuration change.

Step 8: Run command protocols bgp show ip summary at DUT0 and check if output matches the following regular expressions:

10.10.0.200.*Established[\s\S]+20.20.0.30.*Established
Show output
IPv4 Unicast Summary:
BGP router identifier 20.20.0.100, local AS number 100 VRF default vrf-id 0
BGP table version 4
RIB entries 5, using 640 bytes of memory
Peers 2, using 47 KiB of memory

Neighbor        LocalAddr       V         AS   MsgRcvd   MsgSent   TblVer  InQ OutQ  Up/Down        State   PfxRcd   PfxSnt Desc
10.10.0.200     10.10.0.100     4        200         6         5        4    0    0 00:00:10  Established        2 (Policy) N/A
20.20.0.30      20.20.0.100     4        300         5         3        4    0    0 00:00:03  Established        2 (Policy) N/A

Total number of neighbors 2

Attention

Verify DUT0 now selects the route via DUT2 (MED 50) over DUT1 (MED 100).

Step 9: Run command protocols bgp show ip 192.168.10.0/24 at DUT0 and check if output matches the following regular expressions:

20.20.0.30[\s\S]+metric 50[\s\S]+best \(MED\)
Show output
BGP routing table entry for 192.168.10.0/24, version 4
Paths: (2 available, best #1, table default)
  Not advertised to any peer
  300
    20.20.0.30 from 20.20.0.30 (192.168.10.1)
      Origin incomplete, metric 50, valid, external, best (MED)
      Last update: Thu Mar  5 14:59:10 2026
  200
    10.10.0.200 from 10.10.0.200 (192.168.10.1)
      Origin incomplete, metric 100, valid, external
      Last update: Thu Mar  5 14:59:03 2026