BGP communities 屬性
BGP 的community 屬性屬于可選可傳遞屬性,不支援該屬性的路由器會原封不動的将community 值傳遞給下遊BGP鄰居(前提是配置了傳遞community 屬性)。
BGP 的community 屬性可以簡化網絡中路由器對相同路由條目的比對工作,如果在一個大型網絡中所有路由器上都需要比對相同的路由條目,如果用ACL或者字首清單進行比對,則工作量比較大,是以可以在一台路由器上為相應的路由條目打上community 屬性值,在其他路由器上可以通過比對community 屬性值的方式間接地來比對路由條目,進而簡化了路由條目的比對工作。
Community 屬性的類别:
可以分為私有community 屬性值和衆所周知的community 屬性值。
私有的community 屬性值通常用4位元組的數字來表示(可以為一個32位的數,也可以是兩個16位的數字,如100:1)
在CISCO 的路由器中,預設是以32位的數字顯示,如果要轉換成兩個16位的數字,則需用通過指令進行轉換((config)#ip bgp-community new-format),會同時轉換BGP表中的顯示格式和配置檔案中的顯示格式。
ip bgp-community new-format 指令僅對本路由器有效,對鄰居路由器無效。
衆所周知的community 屬性值主要有:
no-advertise : 不通告給任何BGP鄰居(最嚴格)。
no-export : 不通告給任何EBGP鄰居,如果存在BGP聯邦,則會在聯邦内的EBGP鄰居之間傳遞。
local-AS :僅在AS 内傳遞,如果存在BGP聯邦,
internet :表示所有路由,可以通過比對Internet 屬性來比對所有的路由條目
當路由器接受到一條帶有衆所周知community 屬性值的路由條目時,必須要做出相應的動作(根據community 屬性值不同而不同)
也可以分為标準的community 屬性值和擴充的community 屬性值。
擴充的community 屬性值一般在MPLS ××× 網絡中才會涉及到。
注:
一條路由字首可以攜帶多個community 值,可以同時攜帶私有community值和衆所周知的community值。
在任何一台BGP 路由器上可以覆寫、追加、删除community 值。
BGP 的community 屬性值不會自動傳遞給鄰居,必須為特定的鄰居配置傳遞。
(config-router)#neighbor 192.168.12.2 send-community ##為特定的鄰居發送路由時傳遞community 屬性。
配置:
l 為路由條目添加community 屬性值
1、 使用ACL或者字首清單比對需要添加community 屬性值的路由條目,盡量使用字首清單比對。
(config)#ip prefix-list 1 permit 1.1.1.1/32
2、 定義一個route-map ,比對到相應的路由條目,并且追加或者覆寫community 屬性值。
(config)#route-map 1 permit 10
(config-route-map)# match ip address prefix-list 1 ##比對需要添加community 屬性的路由條目
(config-route-map)# set community 126:1 ##為比對到的路由條目打上一個私有的community 屬性值
(config-route-map)# set community no-advertise ##為比對到的路由字首打上一個衆所周知的community屬性值
(config-route-map)# set community 126:1 no-advertise ##為比對到的路由字首打上一個私有的和一個衆所周知的community值。
3、在BGP中定義在發送或者接收路由時添加communti 屬性值。
(config-router)#neighbor 192.168.12.2 route-map 1 out ##為BGP鄰居配置政策,在發送BGP路由時應用route-map 1 的政策,也就是為相應的路由字首打上community 值。
(config-router)# neighbor 192.168.12.2 send-community ##為鄰居發送BGP路由時,攜帶community 屬性值
4、檢視帶有community 屬性值得BGP 路由
R2#show ip bgp community ##檢視帶有community 屬性值的BGP條目
BGP table version is 8, local router ID is 192.168.23.2
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete
Network Next Hop Metric LocPrf Weight Path
*>i1.1.1.1/32 192.168.12.1 0 100 0 i ##說明目前的路由字首攜帶了community 值
5、檢視BGP路由條目中的communtiy 值
R2#show ip bgp 1.1.1.1
BGP routing table entry for 1.1.1.1/32, version 8
Paths: (1 available, best #1, table Default-IP-Routing-Table)
Advertised to update-groups:
2
Local
192.168.12.1 from 192.168.12.1 (192.168.26.2)
Origin IGP, metric 0, localpref 100, valid, internal, best
Community: 126:1 ##路由條目中攜帶的community 值。
Paths: (1 available, best #1, table Default-IP-Routing-Table, not advertised to any peer)
Flag: 0x880
Not advertised to any peer
100
192.168.12.1 from 192.168.12.1 (1.1.1.3)
Origin IGP, metric 0, localpref 100, valid, external, best
Community: 100:1 1000:1 no-advertise ##目前路由字首中攜帶了3個community 屬性值
l 在其他BGP路由器上追加community 值
1、 通過ACL、字首清單、或者commun-list 清單比對到要追加community 值的路由條目
(config)#ip prefix-list 1 permit 1.1.1.1/32 ##通過字首比對路由條目
(config)#ip community-list 1 permit 126:1 ##通過community-list 比對路由條目,因為1.1.1.1/32的路由條目中已經攜帶了126:1的community 值,是以可以通過比對communit 值來間接的比對路由條目。
2、 定義route-map 并追加community 值。
(config)#route-map 1 permit 10 ##建立一個route-map
(config-route-map)#match ip address prefix-list 1 ##通過ip 字首比對路由條目
(config-route-map)#match community 1 ##也可以通過community-list 來比對路由條目。
(config-route-map)# set community 100:1 additive ##追加上一個community 值,其中關鍵字additive 就是追加的意思,如果沒有該關鍵字,将會覆寫掉路由條目中的community 值。
注:在route-map 中可以使用字首清單或者community-list 來比對路由條目,如果同時配置了字首清單和community-list,則兩個清單中都滿足的才算比對,也就是兩個條件是and 關系。
3、 将定義好的route-map 關聯到相應的鄰居。
略……………………
l 在BGP路由器上删除community 值
1、 通過IP字首清單、ACL或者community-list 值比對相應的路由條目。
(config)#ip community-list 1 permit 100:1 ##定義一個community-list,來比對那些BGP路由條目中攜帶community 值為100:1的路由條目。
2、 定義一個community-list 值,用來定義需要删除的community 值。
(config)#ip community-list 2 permit 100:1
3、 定義route-map
(config-route-map)#match community 1
(config-route-map)#set comm-list 2 delete #定義要删除的community值
4、 為相應的鄰居關聯route-map
l 通過community 值來比對相應的路由,并完成相應的政策。
Community 屬性值最大的用途就是簡化路由條目的比對工作。
1、使用community-list 來比對相應的路由條目。
(config)#ip community-list 1 permit 100:1 ##比對community 屬性值中包含100:1的路由
4、 定義route-map
(config)#route-map 1 ##建立一個route-map
(config-route-map)#match community 1 ##通過定義好的community-list 1 比對到相應的路由條目。
(config-route-map)#set weight 2000 ##将比對到的路由條目的weight 更改為2000
5、 為相應的鄰居關聯route-map
(config-router)#neighbor 192.168.34.3 route-map 1 in ##從鄰居192.168.34.3接收路由時應用route-map
6、 檢視結果
R4#show ip bgp 1.1.1.1
BGP routing table entry for 1.1.1.1/32, version 25
300 126
192.168.34.3 from 192.168.34.3 (192.168.37.3)
Origin IGP, localpref 100, weight 2000, valid, external, best
Community: 126:1
l 通過community-list 比對路由條目時的注意事項:
(config)#ip community-list 1 permit 100:1 not-adv
比對community 屬性值中至少同時包含100:1 和not-adv 的路由,也就是兩個條件是且(and)的關系。
(config)#ip community-list 1 perimt 100:1
(config)#ip community-list 1 permit not-adv
比對community 屬性值中包含100:1或者包含not-adv 的路由,兩個條件是或(or)的關系。
當一條路由條目中攜帶了多個community 屬性值,預設情況下隻要比對到其中的一個community 值就能比對到相應的路由,但這樣的比對結果不夠精确,為了精确比對到相應的路由,在route-map 中指定community-list 時,可以添加上exact-match 關鍵字,也就是嚴格比對。
為了更清楚的說明以上内容,舉個簡單的例子,路由器上收到了兩條BGP路由分别為:1.1.1.1/32 和1.1.1.2/32,其中1.1.1.1/32攜帶了一個community 值,為100:1,而1.1.1.2/32攜帶了兩個community 值,分别為:100:1 ,200:2。現在的要求是:在路由器上隻允許攜帶了100:1 的路由進入BGP表。
1.1.1.1/32 community:100:1
1.1.1.2/32 community:100:1 200:2
IP community-list 1 permit 100:1
Route-map 1
Match community 1
(config-router)#neighbor 192.168.1.1 route-map 1 in
通過以上配置後,1.1.1.1/32和1.1.1.2/32 都能進入BGP表,原因是在比對的時候沒有嚴格比對。
案例一
1.1.1.2/32 community:100:1 100:2
案例二
Match community 1 exact-match
通過以上配置,隻有1.1.1.1/32的路由可以進去BGP表,因為進行了嚴格比對。
案例三
IP community-list 1 permit 100:1 100:2
通過以上配置,隻會有1.1.1.2/32的路由可以進入BGP表,因為同一條community-list 中的多個值是and關系,需要同時比對,才算比對。
案例四
ip community-list 1 permit 100:1
ip community-list 1 permit 100:2
通過以上配置,1.1.1.1/32和1.1.1.2/32的路由都可以進入BGP表
案例五
ip community-list standard comm permit 100:1
ip community-list standard comm permit 100:2
Match community comm exact-match
通過以上配置,1.1.1.1/32可以進入BGP表,因為1.1.1.2/32的路由不能嚴格比對。
案例六
ip community-list 2 permit 100:2
route-map 1 permit 10
match community 1 2
案例七
ip community-list standard comm2 permit 100:2
match community comm comm2 exact-match
通過那個配置,隻有1.1.1.1/32的路由可以進入BGP表