一、BGP 基本概念
BGP(Border Gateway Protocol,边界网关协议)是一种外部网关协议(EGP),主要用于在不同自治系统(AS)之间交换路由信息。与 OSPF、RIP 等内部网关协议(IGP)不同,BGP 更注重路由的可达性和策略控制,而非快速收敛或最小跳数。
核心特点:
路径矢量协议:通过携带 AS 路径(AS Path)等属性判断路由优劣,避免环路。
无类别路由协议:支持 CIDR(无类别域间路由),传递路由时携带子网掩码。
TCP 传输:使用 TCP 端口 179 建立邻居关系,保证数据传输的可靠性。
增量更新:仅在路由信息变化时发送更新,减少带宽占用。
二、BGP 工作原理
1. 邻居关系建立
BGP 邻居(Peer)需手动配置,分为两种类型:
IBGP(内部 BGP):同一 AS 内的 BGP 路由器之间的邻居关系,要求逻辑上直连(通常通过 IGP 保证可达)。
EBGP(外部 BGP):不同 AS 之间的 BGP 路由器之间的邻居关系,通常为物理直连。
建立过程:
发送 TCP 连接请求,建立三次握手。
交换 OPEN 消息,协商 BGP 版本、AS 号、保持时间等参数。
发送 KEEPALIVE 消息确认邻居关系,进入 Established 状态。
2. 路由传递与选路
BGP 通过 UPDATE 消息传递路由条目,包含网络层可达信息(NLRI) 和路径属性(PA)。选路时按优先级评估路径属性,核心属性包括:
Weight(权重):Cisco 私有属性,本地有效,值越大越优先。
Local Preference(本地优先级):IBGP 内传递,值越大越优先,默认 100。
Origin(起源):标记路由来源,优先级:IGP(i)> EGP(e)> Incomplete(?)。
AS Path(AS 路径):记录路由经过的 AS 号,长度越短越优先,同时用于防环。
Next Hop(下一跳):指示到达目标网络的下一跳地址。
MED(多出口鉴别器):向 EBGP 邻居传递,值越小越优先,默认不传递。
三、BGP 消息类型
BGP 共定义 4 种消息类型,均通过 TCP 传输:
OPEN:建立邻居关系时交换,包含版本、AS 号、路由器 ID 等关键信息。
KEEPALIVE:维护邻居关系,周期为保持时间的 1/3(默认 60 秒发送一次,保持时间 180 秒)。
UPDATE:传递路由信息,可包含可达路由(NLRI)和不可达路由(撤销路由)。
NOTIFICATION:当检测到错误时发送,如版本不匹配、AS 号错误等,发送后断开邻居关系。
四、BGP 防环机制
BGP 通过多层机制防止路由环路:
EBGP 防环:接收路由时检查 AS Path,若包含本地 AS 号则丢弃。
IBGP 防环:默认不将从 IBGP 邻居学到的路由传递给其他 IBGP 邻居(需通过路由反射器或联邦解决)。
五、常见应用场景
ISP 之间路由交换:互联网骨干网依赖 BGP 连接不同 AS,实现全球路由互通。
企业多出口选路:通过 BGP 与多个 ISP 建立连接,利用本地优先级、MED 等属性控制出 / 入站流量。
路由过滤与策略:通过路由映射(Route-map)、前缀列表(Prefix-list)等工具过滤路由或修改属性,满足业务需求。
六、实战配置要点
基本配置步骤:
启用 BGP 进程,指定本地 AS 号。
配置邻居 IP 及远程 AS 号(IBGP/EBGP)。
宣告本地网络(或通过 redistribution 引入 IGP 路由)。
实验步骤
1.配置IP地址
R1:
[Huawei]sy R1
[R1]int g 0/0/0
[R1-GigabitEthernet0/0/0]ip address 12.1.1.1 24
[R1-GigabitEthernet0/0/0]int l 0
[R1-LoopBack0]ip address 172.16.0.1 32
[R1-LoopBack0]int l 1
[R1-LoopBack1]ip address 192.168.1.1 24
R2:
[Huawei]sy R2
[R2]int g 0/0/0
[R2-GigabitEthernet0/0/0]ip address 12.1.1.2 24
[R2-GigabitEthernet0/0/1]ip address 172.16.1.1 30
[R2-GigabitEthernet0/0/1]int g 0/0/2
[R2-GigabitEthernet0/0/2]ip address 172.16.1.21 30
[R2-GigabitEthernet0/0/0]int l 0
[R2-LoopBack0]ip address 172.16.0.2 32
R3:
[Huawei]sy R3
[R3]int g 0/0/0
[R3-GigabitEthernet0/0/0]ip address 172.16.1.2 30
[R3-GigabitEthernet0/0/0]int g 0/0/1
[R3-GigabitEthernet0/0/1]ip address 172.16.1.5 30
[R3-GigabitEthernet0/0/1]int l 0
[R3-LoopBack0]ip address 172.16.0.3 32
[R3-LoopBack0]int l 1
[R3-LoopBack1]ip address 172.16.3.3 24
R4:
[Huawei]sy R4
[R4]int g 0/0/0
[R4-GigabitEthernet0/0/0]ip address 172.16.1.6 30
[R4-GigabitEthernet0/0/0]int g 0/0/1
[R4-GigabitEthernet0/0/1]ip address 172.16.1.9 30
[R4-GigabitEthernet0/0/1]int l 0
[R4-LoopBack0]ip address 172.16.0.4 32
[R4-LoopBack0]int l 1
[R4-LoopBack1]ip address 172.16.4.4 24
R5:
[Huawei]sy R5
[R5]int g 0/0/0
[R5-GigabitEthernet0/0/0]ip address 172.16.1.22 30
[R5-GigabitEthernet0/0/0]int g 0/0/1
[R5-GigabitEthernet0/0/1]ip address 172.16.1.17 30
[R5-GigabitEthernet0/0/1]int l 0
[R5-LoopBack0]ip address 172.16.0.5 32
[R5-LoopBack0]int l 1
[R5-LoopBack1]ip address 172.16.5.5 24
R6:
[Huawei]sy R6
[R6]int g 0/0/0
[R6-GigabitEthernet0/0/0]ip address 172.16.1.18 30
[R6-GigabitEthernet0/0/0]int g 0/0/1
[R6-GigabitEthernet0/0/1]ip address 172.16.1.13 30
[R6-GigabitEthernet0/0/1]int l 0
[R6-LoopBack0]ip address 172.16.0.632
[R6-LoopBack0]int l 1
[R6-LoopBack1]ip address 172.16.6624
R7:
<Huawei>sy
Enter system view, return user view with Ctrl+Z.
[Huawei]int g 0/0/0
[Huawei-GigabitEthernet0/0/0]ip address 172.16.1.14 30
[Huawei-GigabitEthernet0/0/0]int g0/0/1
[Huawei-GigabitEthernet0/0/1]ip address 172.16.1.10 30
[Huawei-GigabitEthernet0/0/1]int l 0
[Huawei-LoopBack0]ip address 172.16.0.7 32
[Huawei-LoopBack0]int l 1
[Huawei-LoopBack1]ip address 172.16.7.7 24
[Huawei-LoopBack1]int g 0/0/2
[Huawei-GigabitEthernet0/0/2]ip address 34.1.1.7 24
AI写代码
R8:
[Huawei]sy R8
[R8]int g 0/0/0
[R8-GigabitEthernet0/0/0]ip address 34.1.1.8 24
[R8-GigabitEthernet0/0/0]int l 0
[R8-LoopBack0]ip address 172.16.0.8 32
[R8-LoopBack0]int l 1
[R8-LoopBack1]ip address 172.16.8.8 24
2.配置OSPF
R2:
[R2]ospf 1 r 2.2.2.2
[R2-ospf-1]area 0
[R2-ospf-1-area-0.0.0.0]network 172.16.0.0 0.0.255.255
R3:
[R2]ospf 1 r 3.3.3.3
[R2-ospf-1]area 0
[R2-ospf-1-area-0.0.0.0]network 172.16.0.0 0.0.255.255
R4:
[R2]ospf 1 r 4.4.4.4
[R2-ospf-1]area 0
[R2-ospf-1-area-0.0.0.0]network 172.16.0.0 0.0.255.255
R5:
[R2]ospf 1 r 5.5.5.5
[R2-ospf-1]area 0
[R2-ospf-1-area-0.0.0.0]network 172.16.0.0 0.0.255.255
R6:
[R2]ospf 1 r 6.6.6.6
[R2-ospf-1]area 0
[R2-ospf-1-area-0.0.0.0]network 172.16.0.0 0.0.255.255
R7:
[R2]ospf 1 r 7.7.7.7
[R2-ospf-1]area 0
[R2-ospf-1-area-0.0.0.0]network 172.16.0.0 0.0.255.255
3.配置BGP
R1:
[R1]bgp 1
[R1-bgp]peer 12.1.1.2 as-number 2
R2:
[R2]bgp 64512
[R2-bgp]confederation id 2
[R2-bgp]confederation peer-as 64513
[R2-bgp]peer 12.1.1.1 as-number 1
[R2-bgp]peer 172.16.0.3 as-number 64512
[R2-bgp]peer 172.16.0.3 connect-interface l0
[R2-bgp]peer 172.16.0.3 next-hop-local
[R2-bgp]peer 172.16.1.22 as-number 64513
[R2-bgp]peer 172.16.1.22 next-hop-local
R3:
[R3]bgp 64512
[R3-bgp]confederation id 2
[R3-bgp]peer 172.16.0.2 as-number 64512
[R3-bgp]peer 172.16.0.2 connect-interface l0
[R3-bgp]peer 172.16.0.4 as-number 64512
[R3-bgp]peer 172.16.0.4 connect-interface l0
R4:
[R4]bgp 64512
[R4-bgp]confederation id 2
[R4-bgp]peer 172.16.0.3 as-number 64512
[R4-bgp]peer 172.16.0.3 connect-interface l0
[R4-bgp]peer 172.16.0.3 next-hop-invariable
[R4-bgp]peer 172.16.0.3 next-hop-local
R5:
[R5]bgp 64513
[R5-bgp]confederation id 2
[R5-bgp]confederation peer-as 64512
[R5-bgp]peer 172.16.1.21 as-number 64512
[R5-bgp]peer 172.16.0.6 as-number 64513
[R5-bgp]peer 172.16.0.6 connect-interface l0
[R5-bgp]peer 172.16.0.6 next-hop-local
R6:
[R6]bgp 64513
[R6-bgp]confederation id 2
[R6-bgp]peer 172.16.0.5 as-number 64513
[R6-bgp]peer 172.16.0.5 connect-interface l0
[R6-bgp]peer 172.16.0.7 as-number 64513
[R6-bgp]peer 172.16.0.7 connect-interface l0
R7:
[R7]bgp 64513
[R7-bgp]confederation id 2
[R7-bgp]peer 172.16.0.6 as-number 64513
[R7-bgp]peer 172.16.0.6 connect-interface l0
[R7-bgp]peer 172.16.0.6 next-hop-local
[R7-bgp]peer 34.1.1.8 as-number 3
R8:
[R8]bgp 3
[R8-bgp]peer 34.1.1.7 as-number 2
4.宣告网段
R1:
[R1-bgp]network 172.16.0.1 32
R3:
[R3-bgp]peer 172.16.0.2 reflect-client
[R3-bgp]peer 172.16.0.4 reflect-client
R6:
[R6-bgp]peer 172.16.0.5 reflect-client
[R6-bgp]peer 172.16.0.7 reflect-client
R2:
[R2]ip route-static 172.16.0.0 16 NULL 0
[R2]bgp 64512
[R2-bgp]network 172.16.0.0 16
R7:
[R7]ip route-static 172.16.0.0 16 NULL 0
[R7]bgp 64513
[R7-bgp]network 172.16.0.0 16
R8:
[R8-bgp]network 172.16.0.8 32
5.配置隧道
R1:
[R1]int t 0/0/0
[R1-Tunnel0/0/0]ip address 10.1.1.1 24
[R1-Tunnel0/0/0]tunnel-protocol gre
[R1-Tunnel0/0/0]source 172.16.0.1
[R1-Tunnel0/0/0]destination 172.16.0.8
[R1]ip route-static 192.168.2.0 24 10.1.1.2
R8:
[R8]int Tunnel 0/0/0
[R8-Tunnel0/0/0]ip address 10.1.1.2 24
[R8-Tunnel0/0/0]tunnel-protocol gre
[R8-Tunnel0/0/0]source 172.16.0.8
[R8-Tunnel0/0/0]destination 172.16.0.1
[R8]ip route-static 192.168.1.0 24 10.1.1.1
3359

被折叠的 条评论
为什么被折叠?



