Page 1 of 1

BGP Best Path Selection with MED

Posted: Fri Apr 30, 2021 8:25 pm
by netzwerghh
Hello!

I've a problem with the BGP best path selection algorithm. I'm not getting, why in my case the first route is selected as "best". We are using bgp-local-pref=200 for IXP-Peering-Routes. For prefering a special IXP we tag those routes with different MEDs so that we still prefer shortest AS-path. We would like to prefer a Hamburg IXP over Amsterdam, Munich or Dusseldorf. So here are some routes to Google. But although the route from Munich has a higher MED as all other routes it is installed as "best". Why?
[admin@XXX] > /ip route print detail where dst-address=23.227.34.0/24     
Flags: X - disabled, A - active, D - dynamic, C - connect, S - static, r - rip, b - bgp, o - ospf, m - mme, B - blackhole, U - unreachable, P - prohibit 
 0 ADb  dst-address=23.227.34.0/24 gateway=80.81.202.170 gateway-status=80.81.202.170 reachable via  vlan-de-cix-muc distance=20 scope=40 target-scope=10 
        bgp-as-path="15169,396982" bgp-local-pref=200 bgp-med=230 bgp-origin=egp 
        bgp-communities=64800:40001,64800:41004,64800:42004,64800:49999,65101:3096,65102:3000,65103:276,65104:150 received-from=de-cix.muc.rs1 

 1  Db  dst-address=23.227.34.0/24 gateway=80.81.203.34 gateway-status=80.81.203.34 reachable via  vlan-de-cix-ham distance=20 scope=40 target-scope=10 
        bgp-as-path="15169,396982" bgp-local-pref=200 bgp-med=100 bgp-origin=egp 
        bgp-communities=64800:40001,64800:41001,64800:42002,64800:49999,65101:4081,65102:4000,65103:276,65104:150 received-from=de-cix.ham.rs1 

 2  Db  dst-address=23.227.34.0/24 gateway=80.81.203.34 gateway-status=80.81.203.34 reachable via  vlan-de-cix-ham distance=20 scope=40 target-scope=10 
        bgp-as-path="15169,396982" bgp-local-pref=200 bgp-med=100 bgp-origin=egp 
        bgp-communities=64800:40001,64800:41001,64800:42002,64800:49999,65101:4081,65102:4000,65103:276,65104:150 received-from=de-cix.ham.rs2 

 3  Db  dst-address=23.227.34.0/24 gateway=80.249.208.247 gateway-status=80.249.208.247 reachable via  vlan-ams-ix distance=20 scope=40 target-scope=10 
        bgp-as-path="15169,396982" bgp-local-pref=200 bgp-med=220 bgp-origin=egp bgp-communities=64800:40002,64800:41005,64800:42005,64800:49999 
        received-from=ams-ix.rs1 

 4  Db  dst-address=23.227.34.0/24 gateway=80.249.208.247 gateway-status=80.249.208.247 reachable via  vlan-ams-ix distance=20 scope=40 target-scope=10 
        bgp-as-path="15169,396982" bgp-local-pref=200 bgp-med=220 bgp-origin=egp bgp-communities=64800:40002,64800:41005,64800:42005,64800:49999 
        received-from=ams-ix.rs2 

 5  Db  dst-address=23.227.34.0/24 gateway=80.81.203.34 gateway-status=80.81.203.34 reachable via  vlan-de-cix-ham distance=20 scope=40 target-scope=10 
        bgp-as-path="15169,396982" bgp-local-pref=200 bgp-med=100 bgp-origin=egp bgp-communities=64800:40001,64800:41001,64800:42002,64800:49999 
        received-from=de-cix.ham.google 

 6  Db  dst-address=23.227.34.0/24 gateway=62.115.144.140 gateway-status=62.115.144.140 reachable via  vlan-telia-iptransit distance=20 scope=40 
        target-scope=10 bgp-as-path="1299,15169,396982" bgp-local-pref=100 bgp-origin=egp bgp-communities=1299:20000 received-from=telia 
If I shutdown the BGP-session to the Munich route server one of the Hamburg routes is the best one as expected.
[admin@XXX] /ipv6 firewall address-list> /ip route print detail where dst-address=23.227.34.0/24                                                   
Flags: X - disabled, A - active, D - dynamic, C - connect, S - static, r - rip, b - bgp, o - ospf, m - mme, B - blackhole, U - unreachable, P - prohibit 
 0 ADb  dst-address=23.227.34.0/24 gateway=80.81.203.34 gateway-status=80.81.203.34 reachable via  vlan-de-cix-ham distance=20 scope=40 target-scope=10 
        bgp-as-path="15169,396982" bgp-local-pref=200 bgp-med=100 bgp-origin=egp 
        bgp-communities=64800:40001,64800:41001,64800:42002,64800:49999,65101:4081,65102:4000,65103:276,65104:150 received-from=de-cix.ham.rs1 

 1  Db  dst-address=23.227.34.0/24 gateway=80.81.203.34 gateway-status=80.81.203.34 reachable via  vlan-de-cix-ham distance=20 scope=40 target-scope=10 
        bgp-as-path="15169,396982" bgp-local-pref=200 bgp-med=100 bgp-origin=egp 
        bgp-communities=64800:40001,64800:41001,64800:42002,64800:49999,65101:4081,65102:4000,65103:276,65104:150 received-from=de-cix.ham.rs2 

 2  Db  dst-address=23.227.34.0/24 gateway=80.249.208.247 gateway-status=80.249.208.247 reachable via  vlan-ams-ix distance=20 scope=40 target-scope=10 
        bgp-as-path="15169,396982" bgp-local-pref=200 bgp-med=220 bgp-origin=egp bgp-communities=64800:40002,64800:41005,64800:42005,64800:49999 
        received-from=ams-ix.rs1 

 3  Db  dst-address=23.227.34.0/24 gateway=80.249.208.247 gateway-status=80.249.208.247 reachable via  vlan-ams-ix distance=20 scope=40 target-scope=10 
        bgp-as-path="15169,396982" bgp-local-pref=200 bgp-med=220 bgp-origin=egp bgp-communities=64800:40002,64800:41005,64800:42005,64800:49999 
        received-from=ams-ix.rs2 

 4  Db  dst-address=23.227.34.0/24 gateway=80.81.203.34 gateway-status=80.81.203.34 reachable via  vlan-de-cix-ham distance=20 scope=40 target-scope=10 
        bgp-as-path="15169,396982" bgp-local-pref=200 bgp-med=100 bgp-origin=egp bgp-communities=64800:40001,64800:41001,64800:42002,64800:49999 
        received-from=de-cix.ham.google 

 5  Db  dst-address=23.227.34.0/24 gateway=62.115.144.140 gateway-status=62.115.144.140 reachable via  vlan-telia-iptransit distance=20 scope=40 
        target-scope=10 bgp-as-path="1299,15169,396982" bgp-local-pref=100 bgp-origin=egp bgp-communities=1299:20000 received-from=telia 
This is real world data but no information you wouldn't be able to get from peeringdb.

Re: BGP Best Path Selection with MED

Posted: Thu Feb 16, 2023 2:23 am
by tokarenkos
I also have the problem with selection using MED in RouterOS 6.49.7

Re: BGP Best Path Selection with MED

Posted: Thu Feb 23, 2023 10:07 am
by kerfuffle
The issue persists in 7.7 (Be grateful in 6.x you have some troubleshooting capabilities for BGP - can't even see the AS_PATH in /ip/route/print let alone MED)... on Mikrotik, sadly MED isn't all that useful under iBGP in many real-world scenarios, where you'd actually want to use MED to steer traffic - It doesn't appear that it's going to get any attention anytime soon.

Re: BGP Best Path Selection with MED

Posted: Tue Sep 26, 2023 12:25 pm
by netzwerghh
Hey Mikrotik,

this issue is still persisting and is really annoying. Because sometimes it works as expected and sometimes it's doing crap. Or can someone tell me why the active route is preferred over one of the last two routes. Which have same path length, same local-pref, same origin but different MED? Or is Mikrotik preferring path with lower ASN on same path length? Which doesn't make sense. Or at least AFTER evaluating MED.

This is on RouterOS 7.11.2
[admin@XXXX] > /routing/route/print detail where dst-address=2a07:c480::/29
Flags: X - disabled, F - filtered, U - unreachable, A - active; 
c - connect, s - static, r - rip, b - bgp, o - ospf, i - is-is, d - dhcp, v - vpn, m - modem, a - ldp-add>
H - hw-offloaded; + - ecmp, B - blackhole 
  b   afi=ip6 contribution=candidate dst-address=2a07:c480::/29 routing-table=main 
       gateway=2001:7f8:9e::1b1b:0:1 immediate-gw=2001:7f8:9e::1b1b:0:1%vlan-de-cix-dus distance=20 
       scope=40 target-scope=10 belongs-to="bgp-IP6-2001:7f8:9e::de3a:fd:1" 
       bgp.peer-cache-id=*28000A7 .as-path="6939,49009,50017" 
       .communities=64800:42003,65103:276,65102:9000,64800:41003,64800:40001,64800:49999,65101:9093,
             65104:150 
       .large-communities=6695:1000:2,6695:1001:1 .local-pref=300 .med=210 .atomic-aggregate=no 
       .origin=igp 
       debug.fwp-ptr=0xD353960 

  b   afi=ip6 contribution=candidate dst-address=2a07:c480::/29 routing-table=main 
       gateway=2001:7f8:9e::1b1b:0:1 immediate-gw=2001:7f8:9e::1b1b:0:1%vlan-de-cix-dus distance=20 
       scope=40 target-scope=10 belongs-to="bgp-IP6-2001:7f8:9e::de3a:fc:1" 
       bgp.peer-cache-id=*28000A6 .as-path="6939,49009,50017" 
       .communities=64800:42003,65103:276,65102:9000,64800:41003,64800:40001,64800:49999,65101:9093,
             65104:150 
       .large-communities=6695:1000:2,6695:1001:1 .local-pref=300 .med=210 .atomic-aggregate=no 
       .origin=igp 
       debug.fwp-ptr=0xD353960 

  b   afi=ip6 contribution=candidate dst-address=2a07:c480::/29 routing-table=main 
       gateway=2001:7f8::31bb:0:1 immediate-gw=2001:7f8::31bb:0:1%vlan-de-cix-fra distance=20 scope=40 
       target-scope=10 belongs-to="bgp-IP6-2001:7f8::31bb:0:1" 
       bgp.peer-cache-id=*2800083 .as-path="12731,50017" 
       .communities=64800:42001,64800:41002,64800:40001,64800:49999,12731:103,12731:104 .local-pref=300 
       .med=225 .atomic-aggregate=no .origin=igp 
       debug.fwp-ptr=0xD345C00 

  b   afi=ip6 contribution=candidate dst-address=2a07:c480::/29 routing-table=main 
       gateway=2001:7f8::31bb:0:1 immediate-gw=2001:7f8::31bb:0:1%vlan-de-cix-fra distance=20 scope=40 
       target-scope=10 belongs-to="bgp-IP6-2001:7f8::1a27:5051:c19d" 
       bgp.peer-cache-id=*2800077 .as-path="12731,50017" 
       .communities=64800:42001,65103:276,64800:41002,64800:40001,65101:1090,64800:49999,12731:103,
             12731:104,65104:150,65102:1000 
       .large-communities=6695:1000:2,6695:1001:1 .local-pref=300 .med=225 .atomic-aggregate=no 
       .origin=igp 
       debug.fwp-ptr=0xD345C00 

 Ab   afi=ip6 contribution=active dst-address=2a07:c480::/29 routing-table=main 
       gateway=2001:7f8::31bb:0:1 immediate-gw=2001:7f8::31bb:0:1%vlan-de-cix-fra distance=20 scope=40 
       target-scope=10 belongs-to="bgp-IP6-2001:7f8::1a27:5051:c09d" 
       bgp.peer-cache-id=*2800076 .as-path="12731,50017" 
       .communities=64800:42001,65103:276,64800:41002,64800:40001,65101:1090,64800:49999,12731:103,
             12731:104,65104:150,65102:1000 
       .large-communities=6695:1000:2,6695:1001:1 .local-pref=300 .med=225 .atomic-aggregate=no 
       .origin=igp 
       debug.fwp-ptr=0xD345C00 

  b   afi=ip6 contribution=candidate dst-address=2a07:c480::/29 routing-table=main 
       gateway=2001:7f8:3d::31bb:0:1 immediate-gw=2001:7f8:3d::31bb:0:1%vlan-de-cix-ham distance=20 
       scope=40 target-scope=10 belongs-to="bgp-IP6-2001:7f8:3d::31bb:0:1" 
       bgp.peer-cache-id=*280006F .as-path="12731,12731,12731,50017" 
       .communities=64800:42002,64800:41001,64800:40001,64800:49999 .local-pref=300 .med=100 
       .atomic-aggregate=no .origin=igp 
       debug.fwp-ptr=0x203277E0 

  b   afi=ip6 contribution=candidate dst-address=2a07:c480::/29 routing-table=main 
       gateway=2001:7f8:3d::1b1b:0:1 immediate-gw=2001:7f8:3d::1b1b:0:1%vlan-de-cix-ham distance=20 
       scope=40 target-scope=10 belongs-to="bgp-IP6-2001:7f8:3d::1b1b:0:1" 
       bgp.peer-cache-id=*280005F .as-path="6939,49009,50017" .local-pref=100 .atomic-aggregate=no 
       .origin=igp 
       debug.fwp-ptr=0x203264E0 

  b   afi=ip6 contribution=candidate dst-address=2a07:c480::/29 routing-table=main 
       gateway=2001:7f8:3d::c5c5:0:1 immediate-gw=2001:7f8:3d::c5c5:0:1%vlan-de-cix-ham distance=20 
       scope=40 target-scope=10 belongs-to="bgp-IP6-2001:7f8:3d::a8f4:0:2" 
       bgp.peer-cache-id=*280005D .as-path="50629,50017" 
       .communities=50629:10001,64800:42002,65103:276,65101:4122,64800:41001,64800:40001,64800:49999,
             50629:10102,65104:150,65102:4000,50629:220,50629:10205,50629:1020 
       .large-communities=6695:1000:2,6695:1001:1 .local-pref=300 .med=100 .atomic-aggregate=no 
       .origin=igp 
       debug.fwp-ptr=0x20326900 

  b   afi=ip6 contribution=candidate dst-address=2a07:c480::/29 routing-table=main 
       gateway=2001:7f8:3d::c5c5:0:1 immediate-gw=2001:7f8:3d::c5c5:0:1%vlan-de-cix-ham distance=20 
       scope=40 target-scope=10 belongs-to="bgp-IP6-2001:7f8:3d::a8f4:0:1" 
       bgp.peer-cache-id=*280005C .as-path="50629,50017" 
       .communities=50629:10001,64800:42002,65103:276,65101:4122,64800:41001,64800:40001,64800:49999,
             50629:10102,65104:150,65102:4000,50629:220,50629:10205,50629:1020 
       .large-communities=6695:1000:2,6695:1001:1 .local-pref=300 .med=100 .atomic-aggregate=no 
       .origin=igp 
       debug.fwp-ptr=0x20326900

Re: BGP Best Path Selection with MED

Posted: Tue Sep 26, 2023 1:22 pm
by netzwerghh
I've just found an old support request of mine (SUP-84730) regarding this topic and saw that Maris answered this.

From https://help.mikrotik.com/docs/display/ ... hSelection
Prefer the path with the lowest multi-exit discriminator (MED).
The router compares the MED attribute only for paths that have the same neighboring (leftmost) AS. Paths without explicit MED value are treated with MED of 0
It seems that default behaviour is to only compare MED if the leftmost ASN is the same. At least in Cisco you can change this with the option
bgp always-compare-med
. Would be great if Mikrotik could implement the same. Because atm this leads to routing/announcment loops with our Bird route reflectors.

Re: BGP Best Path Selection with MED

Posted: Tue Sep 26, 2023 1:24 pm
by mrz
As far as I can see by looking at attributes routes do not meet the criteria to be selected as best:
https://help.mikrotik.com/docs/display/ ... hSelection

Re: BGP Best Path Selection with MED

Posted: Tue Sep 26, 2023 1:29 pm
by netzwerghh
As far as I can see by looking at attributes routes do not meet the criteria to be selected as best:
https://help.mikrotik.com/docs/display/ ... hSelection
Hi mrz,

yes. I've overread the part in MED comparison that only pathes with the same leftmost ASN are compared for MED. So could you implement an option like Ciscos bgp always-compare-med? At the moment this leads to announcement loops in our network. That we have to manually fix.

Re: BGP Best Path Selection with MED

Posted: Tue Sep 26, 2023 1:35 pm
by mrz
will consider adding this option

Re: BGP Best Path Selection with MED

Posted: Tue Sep 26, 2023 1:52 pm
by netzwerghh
Thank you!