BGP 作為互聯網核心路由協議,其策略控制能力最為強大靈活。本文針對 H3C 路由器 (Comware v5/v7) 環境,深度解析 AS_PATH 屬性操作限制、缺省路由發布最佳實踐、network 命令宣告邏輯,並附帶完整實驗配置案例 (AR18 系列),助網管精準掌握路由策略落地細節。
apply as-path 僅能在 EBGP 出方向 (outbound) 向 AS_PATH 列表前端 (prepend) 添加 AS 號;不可在入方向 (inbound) 添加,亦不可刪除/修改中間 AS。0.0.0.0/0,再在 BGP 下 network 0.0.0.0 0.0.0.0。切勿直接用 default-route imported 或 import-route static 將缺省路由引入 BGP,易導致路由環路或不可控洩漏。network 宣告該路由 不生效 (防止路由回授環路)。拓撲:AR18 (AS 200) — Serial0 (1.1.1.10/30) — 骨幹網 — Serial1 (1.1.1.14/30) — AR18
Loopback1: 20.20.10.254/24 | Loopback2: 20.20.20.254/24 | Loopback3: 20.20.30.254/24
圖 1:BGP 路由策略實驗拓撲
sysname AR18
firewall enable
aaa-enable
!
acl 2000 match-order auto
rule 5 permit source 20.20.30.0 0.0.0.255 ! 僅允許 20.20.30.0/24 通過策略
rule 10 deny source any
!
interface Serial0
link-protocol ppp
ip address 1.1.1.10 255.255.255.252
!
interface Serial1
clock DTECLK1
link-protocol ppp
ip address 1.1.1.14 255.255.255.252
!
interface LoopBack1
ip address 20.20.10.254 255.255.255.0
!
interface LoopBack2
ip address 20.20.20.254 255.255.255.0
!
interface LoopBack3
ip address 20.20.30.254 255.255.255.0
!
bgp 200
undo synchronization
import-route direct route-policy lv ! 引入直連路由 (Loopback) 並套用策略 lv
peer 1.1.1.9 as-number 200 ! IBGP 對等體 (同 AS)
peer 1.1.1.13 as-number 200
!
route-policy lv permit node 10
if-match ip address 2000 ! 引用 ACL 2000,僅匹配 20.20.30.0/24
! 此處可添加 apply as-path 100 200 300 (EBGP out 方向 prepend)
route-policy PREPEND_OUT permit node 10
apply as-path 200 200 200 additive ! 在 AS_PATH 前加三個 200
!
bgp 200
peer 1.1.1.9 route-policy PREPEND_OUT export ! 僅對 EBGP 鄰居生效
效果:對端收到路由 AS_PATH 變長,優選其他路徑,實現入站流量工程。
! 方法一:靜態缺省 + network 宣告 (推薦)
ip route-static 0.0.0.0 0.0.0.0 1.1.1.9 preference 60
!
bgp 200
network 0.0.0.0 0.0.0.0 ! 精確匹配 0.0.0.0/0
!
! 方法二:條件發布 (配合 route-policy)
route-policy DEFAULT_OUT permit node 10
if-match ip-prefix DEFAULT_PREFIX
apply community no-export additive ! 附加 Community 防止傳遞至外部 AS
!
ip ip-prefix DEFAULT_PREFIX index 10 permit 0.0.0.0 0
!
bgp 200
peer 1.1.1.9 route-policy DEFAULT_OUT export
原則:network 僅宣告 本地路由表中非 BGP 來源 的路由 (直連、靜態、IGP)。若需宣告 BGP 學習路由,請使用 import-route bgp (需謹慎配合策略過濾)。
display bgp routing-table 20.20.30.0 ! 查看特定路由屬性 (AS_PATH, Next_Hop, MED, Local_Pref)
display bgp peer 1.1.1.9 verbose ! 查看對等體狀態、收發封包計數、策略應用情況
display bgp routing-table as-path-acl 1 ! 查看匹配特定 AS_PATH 的路由
debugging bgp policy ! 除錯策略匹配過程 (慎用)
no-export 或 no-advertise Community 控制傳遞範圍。display bgp routing-table statistics 對比前後版本,監控路由表膨脹。BGP 策略的核心哲學是「精確控制、最小權限、防禦性編程」。掌握 AS_PATH 操作邊界、缺省路由發布模式、network 宣告語義,配合 ACL/ip-prefix/route-policy 組合拳,即可駕馭複雜互聯互通場景。