BGP是一種多用於運營商之間的網絡協議類型,他與IGP不一樣的地方(主要指IGP中的ospf協議)在於它是一種距離矢量型的協議,因此路由器之間傳遞的爲路由表,而運行商選擇BGP協議的緣由在於IGP協議沒法容納公網上數量巨大的路由條目 網絡
BGP做爲一種應用層協議,工做在7層,相似於ospf協議,運行BGP協議的路由器之間須要創建對等體關係。在ospf中,只有直連路由器之間才能創建鄰居關係(由於TTL=1),而在BGP協議中,創建對等體關係的路由器不是必須直連(IBGP中TTL=255;雖然EBPG中TTL=1,但能夠修改),兩個路由可達的路由器之間就能夠創建對等體關係,以下圖所示,RB與RE之間儘管沒有直連,但仍能夠創建對等體關係,在IBGP中路由器之間須要創建全網狀對等體關係,因此IBGP的對等體數量爲n*(n-1)/2
上圖中RB與RE要創建對等體,首先就須要相互之間路由可達,這就須要藉助IGP(ospf等)協議來實現,從這個角度來看,能夠說BGP是創建在IGP之上的協議 ide
BGP按照工做的區域,能夠分爲IBGP與EBGP兩種,在同一個as內運行BGP協議的路由器之間是IBGP協議,不一樣as之間的路由器採用EBGP協議,as號有65535個,相似於私有ip地址64512~65535這1024個as爲私有as號,BGP的as號,至關於BGP的進程號,不一樣於ospf,每一個路由器上只能啓動1個BGP進程,即每一個路由器只能屬於1個as,BGP按照工做的區域又能夠分爲: oop
在同一as內創建BGP對等體的路由器相互之間是IBGP,因爲BGP是一種距離矢量行協議(DV),因此防環成爲必需要考慮的事情,在IBGP中,採用「水平分割法」來進行防環,即:在同一個as內,路由器B從對等體路由器A處學到的路由,不會再發給本as內其餘的IBGP對等體,IBGP一般使用環回口創建對等體關係 學習
EBPG做用於不一樣as之間,經過AS_PATH屬性防環,當一個路由器發現傳遞過來的屬性中包含本身的AS_PATH編號,則會拒絕接收。從EBGP學習到的路由會自動同步給其餘IBGP和EBGP對等體,且被同步的路由器不會更改下一跳地址,對於IBGP對等體,須要手動修改下一跳地址 this
本次實驗的拓撲以下圖所示,其中,as2345中跑ospf協議,BGP創建在ospf基礎之上,ospf在宣告時,R2的g0/0/0接口與R5的g2/0/0接口不宣告進ospf(由於要作EBGP,對方不會相應ospf的hello報文),R2與R5的lo1接口也不宣告進ospf(留做IBGP起源地址)R2與R5創建IBGP對等體關係
這樣整個拓撲結構梳理爲如下幾點: 3d
爲實現以上條件,R1上的配置以下: code
[R1]bgp 100 [R1-bgp]router-id 1.1.1.1 [R1-bgp]peer 12.0.0.2 as-number 2345 [R1-bgp]peer 12.0.0.2 connect-interface g0/0/0
R2上的配置以下: router
[R2]ospf 1 router-id 2.2.2.2 [R2-ospf-1]area 0 [R2-ospf-1-area-0.0.0.0]network 2.2.2.2 0.0.0.0 [R2-ospf-1-area-0.0.0.0]network 23.0.0.2 0.0.0.0 [R2-ospf-1-area-0.0.0.0]network 24.0.0.2 0.0.0.0 [R2]bgp 2345 [R2-bgp]router-id 2.2.2.2 [R2-bgp]peer 12.0.0.1 as-number 100 [R2-bgp]peer 12.0.0.1 connect-interface g0/0/0 [R2]bgp 2345 [R2-bgp]peer 5.5.5.5 as-number 2345 [R2-bgp]peer 5.5.5.5 connect-interface lo0
R3上的配置以下: blog
[R3]ospf 1 router-id 3.3.3.3 [R3-ospf-1]area 0 [R3-ospf-1-area-0.0.0.0]network 23.0.0.3 0.0.0.0 [R3-ospf-1-area-0.0.0.0]network 35.0.0.3 0.0.0.0 [R3-ospf-1-area-0.0.0.0]network 3.3.3.3 0.0.0.0
R4上的配置以下: 接口
[R4]ospf 1 router-id 4.4.4.4 [R4-ospf-1]area 0 [R4-ospf-1-area-0.0.0.0]network 24.0.0.4 0.0.0.0 [R4-ospf-1-area-0.0.0.0]network 45.0.0.4 0.0.0.0 [R4-ospf-1-area-0.0.0.0]network 4.4.4.4 0.0.0.0
R5上的配置以下:
[R5]ospf 1 router-id 5.5.5.5 [R5-ospf-1]area 0 [R5-ospf-1-area-0.0.0.0]network 2.2.2.2 0.0.0.0 [R5-ospf-1-area-0.0.0.0]network 23.0.0.2 0.0.0.0 [R5-ospf-1-area-0.0.0.0]network 24.0.0.2 0.0.0.0 [R5]bgp 2345 [R5-bgp]router-id 5.5.5.5 [R5-bgp]peer 56.0.0.6 as-number 600 [R5-bgp]peer 56.0.0.6 connect-interface g2/0/0 [R5]bgp 2345 [R5-bgp]peer 2.2.2.2 as-number 2345 [R5-bgp]peer 2.2.2.2 connect-interface lo0
R6上的配置以下:
[R6]bgp 600 [R6-bgp]router-id 6.6.6.6 [R6-bgp]peer 56.0.0.5 as-number 2345 [R6-bgp]peer 56.0.0.5 connect-interface g0/0/0
以R2爲例,此時能看到R2與R1和R5都已創建了BGP對等體關係
<R2>dis bgp peer BGP local router ID : 2.2.2.2 Local AS number : 2345 Total number of peers : 2 Peers in established state : 2 Peer V AS MsgRcvd MsgSent OutQ Up/Down State Pre fRcv 5.5.5.5 4 2345 47 49 0 00:45:40 Established 0 12.0.0.1 4 100 63 63 0 01:01:53 Established 0 <R2>
此時R2與R5之間已經創建了對等體,在R2與R5的路由器上面將lo1接口的地址宣告進IBGP,即起源地址。起源的路由必須是本路由器上存在的路由條目,且掩碼位數必須與路由條目中保持一致。起源的地址只要存在於本路由器的路由表中便可,能夠是非直連,一半狀況是在邊界路由器上起源本as內所有路由地址
[R2]bgp 2345 [R2-bgp]network 22.22.22.22 32 [R5]bgp 2345 [R5-bgp]network 55.55.55.55 32
以R2爲例,此時在R2的BGP路由表中已經能看到通往55.55.55.55/32的路由下一跳是5.5.5.5
<R2>dis bgp routing-table 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 Path/Ogn *> 22.22.22.22/32 0.0.0.0 0 0 i *>i 55.55.55.55/32 5.5.5.5 0 100 0 i
同時,R2路由器會將將最優的BGP路由條目加載到本身的路由表中,R5上也是相似結果,此處再也不贅述
<R2>display ip routing-table protocol bgp Route Flags: R - relay, D - download to fib ------------------------------------------------------------------------------ Public routing table : BGP Destinations : 1 Routes : 1 BGP routing table status : <Active> Destinations : 1 Routes : 1 Destination/Mask Proto Pre Cost Flags NextHop Interface 55.55.55.55/32 IBGP 255 0 RD 5.5.5.5 GigabitEthernet 2/0/0 IBGP 255 0 RD 5.5.5.5 GigabitEthernet 0/0/1 BGP routing table status : <Inactive> Destinations : 0 Routes : 0
作完上述操做後,從R5的路由表中也能看到去往22.22.22.22/32網段的地址
<R5>display ip routing-table protocol bgp Route Flags: R - relay, D - download to fib ------------------------------------------------------------------------------ Public routing table : BGP Destinations : 1 Routes : 1 BGP routing table status : <Active> Destinations : 1 Routes : 1 Destination/Mask Proto Pre Cost Flags NextHop Interface 22.22.22.22/32 IBGP 255 0 RD 2.2.2.2 GigabitEthernet 0/0/1 IBGP 255 0 RD 2.2.2.2 GigabitEthernet 0/0/0 BGP routing table status : <Inactive> Destinations : 0 Routes : 0
但此時,你會發如今R2上沒法ping通R5,反過來同樣
<R2>ping -a 22.22.22.22 55.55.55.55 PING 55.55.55.55: 56 data bytes, press CTRL_C to break Request time out Request time out Request time out Request time out Request time out
其緣由就是儘管R2和R5之間創建了IBGP對等體,但數據包沒法直接從R2「飛」到R5
這一點在路由表中可以清楚地看到,在R2路由器表中能看到目的地址是55.55.55.55/32的路由,下一跳爲5.5.5.5,而去往5.5.5.5/32段,下一跳是R3或者R4
<R2>dis ip routing-table Route Flags: R - relay, D - download to fib ------------------------------------------------------------------------------ Routing Tables: Public Destinations : 21 Routes : 22 Destination/Mask Proto Pre Cost Flags NextHop Interface 55.55.55.55/32 IBGP 255 0 RD 5.5.5.5 GigabitEthernet 2/0/0 5.5.5.5/32 OSPF 10 2 D 24.0.0.4 GigabitEthernet 2/0/0 OSPF 10 2 D 23.0.0.3 GigabitEthernet 0/0/1
而此時R3或者R4路由器上根本沒有22.22.22.22/32的源地址,因此數據包在到達R3或者R4路由器上時,直接被丟棄
<R3>dis ip routing-table Route Flags: R - relay, D - download to fib ------------------------------------------------------------------------------ Routing Tables: Public Destinations : 16 Routes : 17 Destination/Mask Proto Pre Cost Flags NextHop Interface 2.2.2.2/32 OSPF 10 1 D 23.0.0.2 GigabitEthernet 0/0/1 3.3.3.3/32 Direct 0 0 D 127.0.0.1 LoopBack0 4.4.4.4/32 OSPF 10 2 D 35.0.0.5 GigabitEthernet 0/0/0 OSPF 10 2 D 23.0.0.2 GigabitEthernet 0/0/1 5.5.5.5/32 OSPF 10 1 D 35.0.0.5 GigabitEthernet 0/0/0 23.0.0.0/24 Direct 0 0 D 23.0.0.3 GigabitEthernet 0/0/1 23.0.0.3/32 Direct 0 0 D 127.0.0.1 GigabitEthernet 0/0/1 23.0.0.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet 0/0/1 24.0.0.0/24 OSPF 10 2 D 23.0.0.2 GigabitEthernet 0/0/1 35.0.0.0/24 Direct 0 0 D 35.0.0.3 GigabitEthernet 0/0/0 35.0.0.3/32 Direct 0 0 D 127.0.0.1 GigabitEthernet 0/0/0 35.0.0.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet 0/0/0 45.0.0.0/24 OSPF 10 2 D 35.0.0.5 GigabitEthernet 0/0/0 127.0.0.0/8 Direct 0 0 D 127.0.0.1 InLoopBack0 127.0.0.1/32 Direct 0 0 D 127.0.0.1 InLoopBack0 127.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0 255.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
上述問題的緣由在於,R3與R4在自身沒有22.22.22.22/32和55.55.55.55/32路由的狀況下,將IBGP學到的路由發送給了其餘對等體。爲了解決這個問題,早期要求只有路由器經過IGP學到了相同的BGP路由後,才能將BGP路由發送給其餘對等體。即路由器經過BGP學習到的全部路由都要保證經過IGP也學習到,不然它不向其餘對等體發送經過BGP學習到的路由條目。但前文說過,BGP的路由數量太大,IGP沒法承載,因此如今全部路由器上將這個限制默認關閉
[R3-bgp]dis this ipv4-family unicast undo synchronization
另一種解決方法是R三、R4路由也創建對等體關係,又因爲IBGP的水平分割機制,因此就能解釋爲何IBGP對等體須要全網狀創建,根據n*(n-1)/2的原則,本例中共需創建6個IBGP對等體,除去已創建對等體的R2--R5,還須要創建的對等體爲:
R2--R3,R2--R4,R5--R3,R5--R4,R3--R4
[R2]bgp 2345 [R2-bgp]peer 3.3.3.3 as-number 2345 [R2-bgp]peer 3.3.3.3 connect-interface lo0 [R2-bgp]peer 4.4.4.4 as-number 2345 [R2-bgp]peer 4.4.4.4 connect-interface lo0 [R3]bgp 2345 [R3-bgp]peer 2.2.2.2 as-number 2345 [R3-bgp]peer 2.2.2.2 connect-interface lo0 [R3-bgp]peer 5.5.5.5 as-number 2345 [R3-bgp]peer 5.5.5.5 connect-interface lo0 [R3-bgp]peer 4.4.4.4 as-number 2345 [R3-bgp]peer 4.4.4.4 connect-interface lo0 [R4]bgp 2345 [R4-bgp]peer 2.2.2.2 as-number 2345 [R4-bgp]peer 2.2.2.2 connect-interface lo0 [R4-bgp]peer 3.3.3.3 as-number 2345 [R4-bgp]peer 3.3.3.3 connect-interface lo0 [R4-bgp]peer 5.5.5.5 as-number 2345 [R4-bgp]peer 5.5.5.5 connect-interface lo0 [R5]bgp 2345 [R5-bgp]peer 3.3.3.3 as-number 2345 [R5-bgp]peer 3.3.3.3 connect-interface lo0 [R5-bgp]peer 4.4.4.4 as-number 2345 [R5-bgp]peer 4.4.4.4 connect-interface lo0
全網狀的IBGP對等體創建完成後,R3和R4路由上就能學習到22.22.22.22/32和55.55.55.55/32路由並放入本身的路由表中
<R3>display ip routing-table protocol bgp Route Flags: R - relay, D - download to fib ------------------------------------------------------------------------------ Public routing table : BGP Destinations : 2 Routes : 2 BGP routing table status : <Active> Destinations : 2 Routes : 2 Destination/Mask Proto Pre Cost Flags NextHop Interface 22.22.22.22/32 IBGP 255 0 RD 2.2.2.2 GigabitEthernet 0/0/1 55.55.55.55/32 IBGP 255 0 RD 5.5.5.5 GigabitEthernet 0/0/0 BGP routing table status : <Inactive> Destinations : 0 Routes : 0
此時再去R2上帶源ping 55.55.55.55,通訊正常
<R2>ping -a 22.22.22.22 55.55.55.55 PING 55.55.55.55: 56 data bytes, press CTRL_C to break Reply from 55.55.55.55: bytes=56 Sequence=1 ttl=254 time=70 ms Reply from 55.55.55.55: bytes=56 Sequence=2 ttl=254 time=40 ms Reply from 55.55.55.55: bytes=56 Sequence=3 ttl=254 time=30 ms Reply from 55.55.55.55: bytes=56 Sequence=4 ttl=254 time=30 ms Reply from 55.55.55.55: bytes=56 Sequence=5 ttl=254 time=30 ms
將R1與R6的lo0接口地址起源
[R1]bgp 100 [R1-bgp]network 1.1.1.1 32 [R6]bgp 600 [R6-bgp]network 6.6.6.6 32
此時,在R3和R4上看到去往R1與R6環回口的下一跳是12.0.0.1和56.0.0.6
<R3>display bgp routing-table BGP Local router ID is 35.0.0.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: 4 Network NextHop MED LocPrf PrefVal Path/Ogn i 1.1.1.1/32 12.0.0.1 0 100 0 100i i 6.6.6.6/32 56.0.0.6 0 100 0 600i *>i 22.22.22.22/32 2.2.2.2 0 100 0 i *>i 55.55.55.55/32 5.5.5.5 0 100 0 i
因爲R3和R4的路由表中並無這兩個網段的路由信息,因此次BGP路由不是最優,不會加載進路由表,R1沒法ping通R6。這也印證了EBGP傳往IBGP的時候下一跳是自身,不會改變,須要手動在修改R2和R5的下一跳地址
[R2]bgp 2345 [R2-bgp]peer 3.3.3.3 next-hop-local [R2-bgp]peer 4.4.4.4 next-hop-local [R2-bgp]peer 5.5.5.5 next-hop-local [R5]bgp 2345 [R5-bgp]peer 2.2.2.2 next-hop-local [R5-bgp]peer 3.3.3.3 next-hop-local [R5-bgp]peer 4.4.4.4 next-hop-local
修改後再到R3上查看BGP路由表,發現慾望1.1.1.1/32與6.6.6.6/32網段下一跳已經改成as內的邊界路由器,BGP路由表爲最優,已加載進路由表中
[R3]dis bgp routing-table BGP Local router ID is 35.0.0.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: 4 Network NextHop MED LocPrf PrefVal Path/Ogn *>i 1.1.1.1/32 2.2.2.2 0 100 0 100i *>i 6.6.6.6/32 5.5.5.5 0 100 0 600i *>i 22.22.22.22/32 2.2.2.2 0 100 0 i *>i 55.55.55.55/32 5.5.5.5 0 100 0 i
此時R1就能夠ping通R6了
<R1>ping -a 1.1.1.1 6.6.6.6 PING 6.6.6.6: 56 data bytes, press CTRL_C to break Reply from 6.6.6.6: bytes=56 Sequence=1 ttl=252 time=70 ms Reply from 6.6.6.6: bytes=56 Sequence=2 ttl=252 time=60 ms Reply from 6.6.6.6: bytes=56 Sequence=3 ttl=252 time=50 ms Reply from 6.6.6.6: bytes=56 Sequence=4 ttl=252 time=50 ms Reply from 6.6.6.6: bytes=56 Sequence=5 ttl=252 time=40 ms