1. 程式人生 > 其它 >華為裝置BGP中的路由控制與實驗

華為裝置BGP中的路由控制與實驗

技術標籤:HCIE R&S學習筆記BGP路由控制

一、ip-prefix在BGP中的應用

在BGP中,ip-prefix除了可以配合filter-policy或者route-policy使用以外,還可以被peer命令直接呼叫,這也是BGP不同於其他路由協議的地方。通過peer命令呼叫的時候,僅對這一個對等體生效,因此BGP對路由的控制能做到更加精準。我們看下面這個舉例。
在這裡插入圖片描述
需求描述:
如圖所示,LSW1、LSW2、LSW3三個交換機分別屬於不同的AS,他們之間維持EBGP鄰居關係,在LSW1上通過BGP釋出兩條路由。現在LSW3上由於某種業務需求,要求禁止10.1.2.0/24這條路由,允許其他的路由。

配置方法:
上述需求可以在LSW3的BGP程序中,通過peer命令呼叫ip-prefix進行過濾。

LSW3上的關鍵配置如下:
ip ip-prefix huawei index 10 deny 10.1.2.0 24 //定義ip-prefix,禁止目標路由
ip ip-prefix huawei index 20 permit 0.0.0.0 0 less-equal 32 //允許其他所有路由

bgp 300
router-id 3.3.3.3
peer 192.168.23.1 as-number 200

ipv4-family unicast
undo synchronization
peer 192.168.23.1 enable

peer 192.168.23.1 ip-prefix huawei import //在import方向關聯ip-prefix。

當然,也可以在LSW2的export方向關聯ip-prefix進行路由過濾,效果是一樣的,大家可以自行試一下,這裡不再贅述。

結果驗證:
完成上述配置後,檢視LSW3的BGP路由表,可以看到,只有10.1.1.0/24這條路由,10.1.2.0/24已經被過濾掉。

完成上述配置後,檢視LSW3的BGP路由表,可以看到,只有10.1.1.0/24這條路由,10.1.2.0/24已經被過濾掉。

[LSW3] display bgp routing-table 

BGP Local router ID is 3.3.3.3
Status codes: * - valid, > - best, d - damped,
           h - history,  i - internal, s - suppressed, S - Stale
           Origin : i - IGP, e - EGP, ? - incomplete


Total Number of Routes: 1
  Network            NextHop        MED        LocPrf    PrefVal Path/Ogn

*>   10.1.1.0/24        192.168.23.1                          0      200 100i

二、filter-policy在BGP中的應用

filter-policy這個過濾工具我們前面曾經介紹過,我們知道filter-policy應用在距離向量路由協議和鏈路狀態路由協議的時候,過濾規則是不一樣的,BGP屬於一種距離向量路由協議。
filter-policy在BGP中有兩種應用方式:

  1. 在BGP檢視下對全域性應用,對所有對等體生效。
  2. 通過peer命令應用,僅對這一個對等體生效。

相對於其他路由協議,BGP可以針對某個對等體單獨應用filter-policy,這樣也保證了BGP對路由的控制更加靈活、精確。下面我們來看一個具體的舉例。
在這裡插入圖片描述
需求描述:
如圖2所示,LSW1、LSW2、LSW3三個交換機分別屬於不同的AS,他們之間維持EBGP鄰居關係,在LSW1上通過BGP釋出兩條路由。現在LSW3上由於某種業務需求,要求禁止10.1.2.0/24這條路由,允許其他的路由。

配置方法:
方法一:在BGP檢視下對全域性應用,對所有對等體生效。
LSW3的關鍵配置如下:

acl number 2001 //定義ACL,用於匹配目標路由
rule 5 deny source 10.1.2.0 0 //拒絕10.1.2.0這條路由
rule 10 permit source any //允許所有

bgp 300
router-id 3.3.3.3
peer 192.168.23.1 as-number 200

ipv4-family unicast
undo synchronization
filter-policy 2001 import //對所有對等體使用filter-policy過濾路由
peer 192.168.23.1 enable

方法二:通過peer命令應用,僅對這一個對等體生效。
LSW3的關鍵配置如下:

acl number 2001 //定義ACL,用於匹配目標路由
rule 5 deny source 10.1.2.0 0 //拒絕10.1.2.0這條路由
rule 10 permit source any //允許所有

bgp 300
router-id 3.3.3.3
peer 192.168.23.1 as-number 200

ipv4-family unicast
undo synchronization
peer 192.168.23.1 enable
peer 192.168.23.1 filter-policy 2001 import //對單個對等體使用filter-policy

三、 route-policy在BGP中的應用

route-policy在前面我們也詳細介紹過了,在BGP中,route-policy同樣是一個非常重要的工具,可以用來設定路由的屬性,對路由進行過濾等。

我們再通過一個舉例來看一下在BGP中呼叫route-policy的方法。
在這裡插入圖片描述
需求描述:
如圖所示,LSW1和LSW2兩個交換機分別屬於不同的AS,他們之間維持EBGP鄰居關係,在LSW1上通過BGP釋出兩條路由。現在由於某種業務需求,需要對LSW1釋出的兩條路由分別設定不通的團體屬性值,10.1.1.0/24這條路由設定團體屬性值為100:1,10.1.2.0/24這條路由設定團體屬性值為100:2。

配置方法:

方法一:通過network命令呼叫route-policy。
這種配置方法是在通過network命令釋出路由的時候直接呼叫route-policy,設定相應的團體屬性值以後再發布給對等體。
LSW1的關鍵配置如下:

ip ip-prefix 1 index 10 permit 10.1.1.0 24 //定義字首列表,匹配目標路由
ip ip-prefix 2 index 10 permit 10.1.2.0 24

route-policy huawei permit node 10 //為不同的路由設定不同的團體屬性值
if-match ip-prefix 1
apply community 100:1

route-policy huawei permit node 20
if-match ip-prefix 2
apply community 100:2

route-policy huawei permit node 30 //允許剩餘所有路由

bgp 100
router-id 1.1.1.1
peer 192.168.12.2 as-number 200

ipv4-family unicast
undo synchronization
network 10.1.1.0 255.255.255.0 route-policy huawei
network 10.1.2.0 255.255.255.0 route-policy huawei
peer 192.168.12.2 enable
peer 192.168.12.2 advertise-community

方法二:通過peer命令呼叫route-policy。
這種配置方法,在通過network命令釋出路由的時候不使用route-policy,但是通過peer命令針對對等體使用route-policy,這樣路由在釋出之前先通過route-policy的過濾,通過並設定相應的團體屬性值以後再發布給對等體。
LSW1的關鍵配置如下:

ip ip-prefix 1 index 10 permit 10.1.1.0 24 //定義字首列表,匹配目標路由
ip ip-prefix 2 index 10 permit 10.1.2.0 24

route-policy huawei permit node 10 //為不同的路由設定不同的團體屬性值
if-match ip-prefix 1
apply community 100:1

route-policy huawei permit node 20
if-match ip-prefix 2
apply community 100:2

route-policy huawei permit node 30 //允許剩餘所有路由

bgp 100
router-id 1.1.1.1
peer 192.168.12.2 as-number 200

ipv4-family unicast
undo synchronization
network 10.1.1.0 255.255.255.0
network 10.1.2.0 255.255.255.0
peer 192.168.12.2 enable
peer 192.168.12.2 route-policy huawei export
peer 192.168.12.2 advertise-community

結果驗證:
上述兩種配置方法雖然不一樣,但是對於這個場景,都可以實現10.1.1.0/24這條路由設定團體屬性值為100:1,10.1.2.0/24這條路由設定團體屬性值為100:2。完成配置後,在LSW2上檢視一下BGP路由表,可以看到已實現上述需求。

[LSW2] display bgp routing-table community  

 BGP Local router ID is 2.2.2.2
 Status codes: * - valid, > - best, d - damped,
           h - history,  i - internal, s - suppressed, S - Stale
           Origin : i - IGP, e - EGP, ? - incomplete


 Total Number of Routes: 2
  Network            NextHop        MED        LocPrf    PrefVal Community

 *>   10.1.1.0/24        192.168.12.1    0                     0      <100:1>
 *>   10.1.2.0/24        192.168.12.1    0                     0      <100:2>

相對於其他路由協議,BGP中應用route-policy的命令會更多一點,更具體一點。常見的呼叫route-policy的命令有下面這些:

  1. network
  2. peer
  3. import-route
  4. dampening

資料整理來源《交換機在江湖》