BGPベストパス選定におけるMEDの動作と設定方法

音声で聞く

読む前にこの記事のポイントを音声で確認できます。この音声は本記事をもとにAIツール(NotebookLM)で自動生成したもので、発音や言い回しが不自然な箇所や、内容の誤りが含まれる可能性があります。理解の補助としてご利用ください。

BGP は、同じ宛先への経路を複数受け取ると、決まった順番で属性を比べてベストパスを選びます。比較項目の 1 つである MED(Multi-Exit Discriminator)は、通常、同じ隣接 AS から受け取った経路どうしでしか比べず、隣接 AS が違う経路では無視されます。この記事では、Cisco ルータで MED がベストパスの選択にどう効くかを、設定と出力で確認します。

この記事でわかること
  1. MED を付けた経路を 1 つ受け取ったときのベストパスを確認する
  2. 隣接 AS が違う経路どうしでは MED を比べないことを確認する
  3. 経路が 3 つあるときの比べ方(新しい順に 2 つずつ)を確認する
  4. 違う AS どうしでも MED を比べる設定(bgp always-compare-med)を知る

BGP のベストパス選択の優先順位

Cisco ルータの BGP は、次の順番で経路を比べ、差がついた時点でベストパスを決めます。

順番優先する経路
1Weight が最も大きい
2Local Preference が最も大きい
3自分で生成した(network、aggregate-address、再配布)
4AS_PATH が最も短い
5Origin が最も小さい(IGP < EGP < Incomplete)
6MED が最も小さい(通常は、隣接 AS が同じ経路どうしでだけ比べる)
7iBGP より eBGP
8ネクストホップまでの IGP メトリックが最も小さい
9(BGP マルチパスが有効なら、複数の経路をルーティングテーブルに入れるか判断する)
10どちらも eBGP なら、先に受信した(古い)経路
11BGP ルータ ID が最も小さいルータからの経路
12クラスタリストが最も短い経路
13ネイバーのアドレスが最も小さい経路

6 番目の MED は、AS_PATH の先頭の AS(隣接 AS)が同じ経路どうしでだけ比べます。MED の値の付け方は AS ごとに違うためです。隣接 AS が違う経路では MED を飛ばし、次の比較項目に進みます。

構成例で見る MED の動作

MED の比べ方がベストパスの選択にどう影響するかを確認します。次の構成で、R2、R3、R4 から同じ BGP ルート(1.1.1.1/32)を、それぞれ MED を付けて R6 に広告します。R2 と R3 は同じ AS 23、R4 だけ AS 4 です。

AS 4 AS 23 AS 6 R4 1.1.1.1/32 .4 192.168.46.0/24 MED 30 R2 1.1.1.1/32 .2 192.168.26.0/24 MED 10 R3 1.1.1.1/32 .3 192.168.36.0/24 MED 20 R6
図1 検証構成(R6 側のアドレスはすべて .6)

まず、R6 を設定します。3 台のネイバーを設定しておき、相手側の設定を順に入れてピアを張っていきます。

R6

R6(config)# router bgp 6
R6(config-router)# bgp router-id 6.6.6.6
R6(config-router)# neighbor 192.168.26.2 remote-as 23
R6(config-router)#  neighbor 192.168.36.3 remote-as 23
R6(config-router)#  neighbor 192.168.46.4 remote-as 4

R4 とピアを張る

R4 に設定を入れ、R6 とピアを張ります。R4 は 1.1.1.1/32 を広告し、R6 向けのルートマップで MED を 30 にします。set metric で設定する値が MED です。

AS 4 AS 6 R4 1.1.1.1/32 .4 192.168.46.0/24 MED 30 R6
図2 R4 から経路を受け取る

R4

R4(config)# interface Loopback0
R4(config-if)# ip address 1.1.1.1 255.255.255.255
R4(config-if)#!
R4(config-if)# router bgp 4
R4(config-router)#  bgp router-id 4.4.4.4
R4(config-router)#  network 1.1.1.1 mask 255.255.255.255
R4(config-router)#  neighbor 192.168.46.6 remote-as 6
R4(config-router)#  neighbor 192.168.46.6 route-map MED30 out
R4(config-router)#!
R4(config-router)# route-map MED30 permit 10
R4(config-route-map)#  set metric 30

R6 の BGP テーブルを見ると、R4 から 1.1.1.1/32 を MED 30 で受信し、ベストパスにしています。

R6

R6# show ip bgp
BGP table version is 4, local router ID is 6.6.6.6
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal, 
              r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter, 
              x best-external, a additional-path, c RIB-compressed, 
Origin codes: i - IGP, e - EGP, ? - incomplete
RPKI validation codes: V valid, I invalid, N Not found

     Network          Next Hop            Metric LocPrf Weight Path
 *>  1.1.1.1/32       192.168.46.4            30             0 4 i

R6# show ip bgp 1.1.1.1
BGP routing table entry for 1.1.1.1/32, version 4
Paths: (1 available, best #1, table default)
  Advertised to update-groups:
     2         
  Refresh Epoch 1
  4
    192.168.46.4 from 192.168.46.4 (4.4.4.4)
      Origin IGP, metric 30, localpref 100, valid, external, best

R2 とピアを張る

次に、R2 に設定を入れてピアを張ります。R2 は MED を 10 にします。

AS 4 AS 23 AS 6 R4 1.1.1.1/32 .4 192.168.46.0/24 MED 30 R2 1.1.1.1/32 .2 192.168.26.0/24 MED 10 R6
図3 R4 と R2 から経路を受け取る

R2

R2(config)# interface Loopback0 
R2(config-if)#  ip address 1.1.1.1 255.255.255.255
R2(config-if)#!
R2(config-if)# router bgp 23
R2(config-router)#  bgp router-id 2.2.2.2
R2(config-router)#  network 1.1.1.1 mask 255.255.255.255
R2(config-router)#  neighbor 192.168.26.6 remote-as 6
R2(config-router)#  neighbor 192.168.26.6 route-map MED10 out
R2(config-router)#!
R2(config-router)# route-map MED10 permit 10
R2(config-route-map)#  set metric 10
R2(config-route-map)#!

R6 の BGP テーブルを見ると、R4 からの経路がベストパスのままです。

R6

R6# show ip bgp    
BGP table version is 4, local router ID is 6.6.6.6
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal, 
              r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter, 
              x best-external, a additional-path, c RIB-compressed, 
Origin codes: i - IGP, e - EGP, ? - incomplete
RPKI validation codes: V valid, I invalid, N Not found

     Network          Next Hop            Metric LocPrf Weight Path
 *   1.1.1.1/32       192.168.26.2            10             0 23 i
 *>                   192.168.46.4            30             0 4 i


R6# show ip bgp 1.1.1.1  
BGP routing table entry for 1.1.1.1/32, version 4
Paths: (2 available, best #2, table default)
  Advertised to update-groups:
     2         
  Refresh Epoch 1
  23
    192.168.26.2 from 192.168.26.2 (2.2.2.2)
      Origin IGP, metric 10, localpref 100, valid, external
  Refresh Epoch 1
  4
    192.168.46.4 from 192.168.46.4 (4.4.4.4)
      Origin IGP, metric 30, localpref 100, valid, external, best
注意

R2 からの経路(MED 10)は R4 からの経路(MED 30)より MED が小さいですが、隣接 AS が AS 23 と AS 4 で違うため、MED は比べません。

MED を飛ばしたあとも差がつかず、どちらも eBGP の経路なので、10 番目の「先に受信した(古い)経路」で決まります。先に受信していた R4 の経路がベストパスになります。

R3 とピアを張る

最後に、R3 に設定を入れてピアを張ります。R3 は MED を 20 にします。

AS 4 AS 23 AS 6 R4 1.1.1.1/32 .4 192.168.46.0/24 MED 30 R2 1.1.1.1/32 .2 192.168.26.0/24 MED 10 R3 1.1.1.1/32 .3 192.168.36.0/24 MED 20 R6
図4 3 台から経路を受け取る

R3

R3(config)# interface Loopback0
R3(config-if)#  ip address 1.1.1.1 255.255.255.255
R3(config-if)#!
R3(config-if)# router bgp 23
R3(config-router)#  bgp router-id 3.3.3.3
R3(config-router)#  network 1.1.1.1 mask 255.255.255.255
R3(config-router)#  neighbor 192.168.36.6 remote-as 6
R3(config-router)#  neighbor 192.168.36.6 route-map MED20 out
R3(config-router)#!
R3(config-router)# route-map MED20 permit 10
R3(config-route-map)#  set metric 20

R6 の BGP テーブルを確認すると、まだ R4 の経路がベストパスです。

R6

R6# show ip bgp         
BGP table version is 4, local router ID is 6.6.6.6
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal, 
              r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter, 
              x best-external, a additional-path, c RIB-compressed, 
Origin codes: i - IGP, e - EGP, ? - incomplete
RPKI validation codes: V valid, I invalid, N Not found

     Network          Next Hop            Metric LocPrf Weight Path
 *   1.1.1.1/32       192.168.36.3            20             0 23 i
 *                    192.168.26.2            10             0 23 i
 *>                   192.168.46.4            30             0 4 I


R6# show ip bgp 1.1.1.1
BGP routing table entry for 1.1.1.1/32, version 4
Paths: (3 available, best #3, table default)
  Advertised to update-groups:
     2         
  Refresh Epoch 1
  23
    192.168.36.3 from 192.168.36.3 (3.3.3.3)
      Origin IGP, metric 20, localpref 100, valid, external
  Refresh Epoch 1
  23
    192.168.26.2 from 192.168.26.2 (2.2.2.2)
      Origin IGP, metric 10, localpref 100, valid, external
  Refresh Epoch 1
  4
    192.168.46.4 from 192.168.46.4 (4.4.4.4)
      Origin IGP, metric 30, localpref 100, valid, external, best

Cisco ルータは、受け取った経路を新しい順に並べ、新しいものから 2 つずつ比べていきます。show ip bgp 1.1.1.1 の表示も、この新しい順(R3、R2、R4)です。

  1. R3 と R2 を比べます。どちらも隣接 AS が AS 23 なので MED を比べ、MED 10 の R2 が勝ちます。
  2. 勝った R2 と R4 を比べます。隣接 AS が違うので MED は比べません。どちらも eBGP なので、先に受信した R4 が勝ちます。
新しい順(show ip bgp の表示順) R3AS 23 / MED 20 R2AS 23 / MED 10 R4AS 4 / MED 30 R2AS 23 / MED 10 ① 同じ AS 23 なので MED を比較   → MED 10 の R2 R4AS 4 / MED 30 ② R2 と R4 は隣接 AS が違うので MED は比べない   → どちらも eBGP なので、先に受信した R4 ベストパス
図5 3 つの経路を比べる順番

その結果、R4 の経路がベストパスになります。

補足

このように、MED を比べる経路と比べない経路が混ざると、経路を受け取った順番で結果が変わることがあります。bgp deterministic-med を設定すると、同じ隣接 AS の経路をまとめて先に比べてから、各 AS の代表を比べるようになり、受け取った順番に左右されにくくなります。

ここまでが、Cisco IOS の既定の動作です。

違う AS どうしでも MED を比べるには

既定の動作を変えて、隣接 AS が違う経路どうしでも MED を比べるには、bgp always-compare-med を設定します。

R6

R6(config-router)# bgp always-compare-med

この設定をすると、R6 は隣接 AS に関係なく MED を比べ、MED が最も小さい経路を優先します。この構成では、MED 10 の R2 の経路がベストパスになります。動作は次の記事で確認しています。

>> 参考:BGPのベストパス選定:bgp always-compare-medの活用方法

まとめ

受け取った経路MED を比べるかベストパス
R4(AS 4)だけ経路が 1 つなので比較なしR4
R4(AS 4)と R2(AS 23)隣接 AS が違うので比べないR4(先に受信)
R4、R2、R3(AS 23)R2 と R3 だけ比べるR4(R2 との比較で先に受信)

BGP のベストパス選択では、MED は既定で同じ隣接 AS の経路どうしでだけ比べます。違う AS の経路でも MED を比べたいときは、bgp always-compare-med で動作を変えます。

コメント