The BGP MED (multiple exit discriminator) is an optional non-transitive attribute. That means this value is always carried along with BGP update and can be dropped if the BGP process doesn’t understand it.

Let me first write about my understanding on both commands

bgp deterministic-med command compares MED variable when choosing routes advertised by different peers inside the same autonomous system.

bgp always-compare-med command compares MED variable for paths from neighbors in different autonomous systems.

When same prefix is received from different neighbors with same AS path then MED value is compared to choose the best route among them. This is the default behavior of BGP path selection algorithm process in Cisco IOS.

Wait, did I just say the default behavior is to check MED values as per the BGP path selection algorithm?

Yes, I said that. Then what is the need of ‘bgp deterministic-med’ command.

To explain this let’s look at below topology.

BGP-MED

R5 (BGP AS 65000) is advertising 55.55.55.55/32 to R2 & R3 in AS 65020 and R4 in 65040.

R2, R3 & R4 are advertising the prefix to R1 with MED 200, 300 & 400 respectively.

Let’s look at R1 for the same prefix.

R1#show ip bgp 55.55.55.55
BGP routing table entry for 55.55.55.55/32, version 8
Paths: (3 available, best #3, table default)
 Advertised to update-groups:
 4
 Refresh Epoch 1
 65020 65000
 10.10.12.2 from 10.10.12.2 (2.2.2.2)
 Origin IGP, metric 200, localpref 100, valid, external
 Refresh Epoch 1
 65040 65000
 10.10.14.4 from 10.10.14.4 (4.4.4.4)
 Origin IGP, metric 400, localpref 100, valid, external
 Refresh Epoch 1
 65020 65000
 10.10.13.3 from 10.10.13.3 (3.3.3.3)
 Origin IGP, metric 300, localpref 100, valid, external, best
R1#

The BGP routes are ordered in newest (at top) to oldest (at bottom).

The routes are compared in the order they are present. In our case the first entry (#1) will be compared with second entry (#2) and the winner will be compared with the third entry (#3).

The prefix via R2 is compared with that of R4. As both have different AS, the MED will be ignored. Here the route from R4 will win as it is older than that of R2.

Then R4 will be compared with R3. Again both are from different AS, so the oldest is chosen. Hence prefix from R3 is chosen as the best even if it R2 has the best MED among them.

As we can see the BGP process could not choose the best route as per the MED even between R2 & R3 because they were not placed consecutively.

Is there a way to place them together for a better way of handling BGP path selection algorithm?

If you said ‘bgp deterministic-med’ then that is the correct answer. This ensures that the prefix from on AS is grouped for the MED comparison.

Let’s enable ‘bgp deterministic-med’ command in R1 and then check the output.

router bgp 65010
 bgp router-id 1.1.1.1
 bgp log-neighbor-changes
 bgp deterministic-med
 neighbor 10.10.12.2 remote-as 65020
 neighbor 10.10.13.3 remote-as 65020
 neighbor 10.10.14.4 remote-as 65040

 

R1#show ip bgp 55.55.55.55
BGP routing table entry for 55.55.55.55/32, version 11
Paths: (3 available, best #3, table default)
 Advertised to update-groups:
 4
 Refresh Epoch 1
 65020 65000
 10.10.12.2 from 10.10.12.2 (2.2.2.2)
 Origin IGP, metric 200, localpref 100, valid, external
 Refresh Epoch 1
 65020 65000
 10.10.13.3 from 10.10.13.3 (3.3.3.3)
 Origin IGP, metric 300, localpref 100, valid, external
 Refresh Epoch 1
 65040 65000
 10.10.14.4 from 10.10.14.4 (4.4.4.4)
 Origin IGP, metric 400, localpref 100, valid, external, best
R1#

It looks better now. R2 & R3 are placed in sequential order for better comparison.

R2 & R3 will be compared first. R2 wins as it has better MED (200) than R3 (300).

Then R2 is compared with R4. But they are again from different AS hence MED cannot be compared. We will again come back to the older route among them. Hence R4 wins.

 

Now the question is, do we have a way to compare MED even if the routes have different AS path?

Yes. The answer is bgp always-compare-med command.

It will compare the MED value irrespective of the AS path.

Let’s configure that in R1 and clear bgp session to check.

router bgp 65010
 bgp router-id 1.1.1.1
 bgp always-compare-med
 bgp log-neighbor-changes
 bgp deterministic-med
 neighbor 10.10.12.2 remote-as 65020
 neighbor 10.10.13.3 remote-as 65020
 neighbor 10.10.14.4 remote-as 65040

 

R1#show ip bgp 55.55.55.55
BGP routing table entry for 55.55.55.55/32, version 2
Paths: (3 available, best #1, table default)
 Advertised to update-groups:
 5
 Refresh Epoch 1
 65020 65000
 10.10.12.2 from 10.10.12.2 (2.2.2.2)
 Origin IGP, metric 200, localpref 100, valid, external, best
 Refresh Epoch 1
 65020 65000
 10.10.13.3 from 10.10.13.3 (3.3.3.3)
 Origin IGP, metric 300, localpref 100, valid, external
 Refresh Epoch 1
 65040 65000
 10.10.14.4 from 10.10.14.4 (4.4.4.4)
 Origin IGP, metric 400, localpref 100, valid, external
R1#

It looks much better now. The MED value is checked across all 3 neighbors and the best route is chosen according to lowest MED value 200 that of R2.

 

Now I have removed both the commands and reset the BGP peering on R1.

 

R1(config)#router bgp 65010
R1(config-router)#no bgp deterministic-med
R1(config-router)#no bgp always-compare-med
R1(config-router)#end

R1#show run | sec r b
router bgp 65010
bgp router-id 1.1.1.1
bgp log-neighbor-changes
neighbor 10.10.12.2 remote-as 65020
neighbor 10.10.13.3 remote-as 65020
neighbor 10.10.14.4 remote-as 65040
R1#

R1#show ip bgp 55.55.55.55
BGP routing table entry for 55.55.55.55/32, version 2
Paths: (3 available, best #2, table default)
Advertised to update-groups:
6
Refresh Epoch 1
65040 65000
10.10.14.4 from 10.10.14.4 (4.4.4.4)
Origin IGP, metric 400, localpref 100, valid, external
Refresh Epoch 1
65020 65000
10.10.12.2 from 10.10.12.2 (2.2.2.2)
Origin IGP, metric 200, localpref 100, valid, external, best
Refresh Epoch 1
65020 65000
10.10.13.3 from 10.10.13.3 (3.3.3.3)
Origin IGP, metric 300, localpref 100, valid, external
R1#

 

But why R2 is selected again, we have neither enabled deterministic-med nor always-compare-med.

Remember the BGP path selection algorithm rule which says to prefer the route that comes from the BGP router with the lowest router ID.

As I cleared the BGP process on R1, the prefix from all three routers R2, R3 & R4 are learned at once. So we came down to the rule of lowest router ID which is R2 with 2.2.2.2.

 

 

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s