Skip to content

BGP Route Policies — Filtering và Modification Với CEL

Tại Sao Quan Trọng Trong Production

Trong môi trường hybrid connectivity thực tế, on-prem router thường advertise nhiều hơn những gì VPC cần biết. Có thể on-prem advertise toàn bộ internal routing table bao gồm management networks, test environments, và legacy segments không cần reachable từ VPC. Ngược lại, Cloud Router mặc định advertise tất cả VPC subnets — bao gồm những subnets mà on-prem không nên biết đến vì lý do security.

BGP route policies giải quyết bài toán này: filter và modify BGP routes trước khi chúng được accept vào VPC hoặc advertised ra on-prem. Điều này không chỉ là vấn đề network hygiene — nó còn là security control. Route leaking sai có thể tạo ra reachability không mong muốn.

Điểm đặc biệt trong Cloud Router là route policies sử dụng Common Expression Language (CEL) — cùng ngôn ngữ dùng trong GCP IAM conditions và Kubernetes validating admission policies. Đây là sự khác biệt lớn so với Cisco/Juniper route-maps truyền thống.


Internal Model — Cơ Chế Route Policy

Hai Chiều Application

Route policies được áp dụng theo một trong hai hướng trên một BGP session:

Import policies (inbound):

  • Áp dụng cho routes đang được NHẬN từ BGP peer
  • Quyết định route nào được accept vào Cloud Router, route nào bị drop
  • Có thể modify attributes của route trước khi accept (ví dụ: add communities)
  • Chạy TRƯỚC khi route được đưa vào best-path selection

Export policies (outbound):

  • Áp dụng cho routes đang được ADVERTISE ra BGP peer
  • Quyết định route nào được gửi đi, route nào bị giữ lại
  • Có thể modify attributes của route trước khi advertise (ví dụ: prepend AS path)
  • Chạy SAU khi routes đã được chọn để advertise (từ advertisement mode)

Quan trọng: Theo tài liệu GCP, "A particular BGP route policy can be applied only in one direction, either inbound for learned routes, or outbound for advertised routes, but not both simultaneously." Một policy chỉ được assign cho một chiều.

Pipeline Evaluation

BGP Peer → RECEIVE UPDATE


    Import Policy (nếu có)
    ├── Match conditions (CEL expressions)
    ├── Action: ACCEPT / DROP
    └── Modify attributes (nếu ACCEPT)


    BGP RIB (Routing Information Base)


    Best-Path Selection


    Dynamic Route Control Plane


    VPC Route Table

Với export:

VPC Route Table / Advertisement Mode


    Chọn routes để advertise


    Export Policy (nếu có)
    ├── Match conditions
    ├── Action: ADVERTISE / DROP
    └── Modify attributes (nếu ADVERTISE)


    BGP Peer ← SEND UPDATE

Fail-Open Model

Đây là đặc điểm quan trọng cần hiểu: Cloud Router dùng fail-open model cho route policies.

Theo tài liệu: Route policy evaluation kết thúc khi route được accept hoặc reject. Nếu route đi qua tất cả policy terms mà không bị explicitly accept hoặc drop, route mặc định được PASS (không bị drop).

Điều này có nghĩa: nếu policy của bạn có lỗi hoặc không cover một số routes, các routes đó vẫn đi qua. Đây là behavior an toàn hơn fail-closed (không có gì qua nếu không explicit allow), nhưng có nghĩa là bạn phải explicit DROP những gì bạn không muốn, không phải explicit ALLOW những gì bạn muốn.


Common Expression Language (CEL) Cho Route Policies

Cú Pháp Cơ Bản

BGP route policies trong Cloud Router được viết bằng CEL — một ngôn ngữ biểu thức đơn giản, strongly-typed, không có side effects. CEL cho phép bạn viết điều kiện match cho routes.

Các trường có thể match trong import policy (routes nhận từ peer):

  • destination.ip_prefix: prefix của route (CIDR)
  • path_attributes.as_path.as_numbers: danh sách ASNs trong AS_PATH
  • path_attributes.communities.communities_list: danh sách BGP communities

Các trường có thể match trong export policy (routes advertise ra peer):

  • destination.ip_prefix: prefix được advertise
  • Và các attributes khác tùy product

Ví dụ CEL expression:

cel
// Match routes với prefix trong dải 10.0.0.0/8
destination.ip_prefix.contains('10.0.0.0/8')

// Match routes với specific ASN trong AS_PATH
path_attributes.as_path.as_numbers.exists(asn, asn == 65001)

// Match routes với specific community
path_attributes.communities.communities_list.exists(
  c, c.community_value == '64512:100'
)

Named Sets

Để tránh lặp lại danh sách prefixes hoặc ASNs trong nhiều policies, Cloud Router hỗ trợ named sets — tập hợp prefixes hoặc ASNs được đặt tên và tái sử dụng.

Ví dụ:

bash
# Tạo named prefix list
gcloud compute routers update ROUTER_NAME \
    --add-custom-learned-route-ranges=... \
    --region=REGION

Named sets được định nghĩa ở router level và có thể được tham chiếu từ policies. Giới hạn quan trọng: Named sets là router-specific — không thể share giữa các Cloud Routers khác nhau.

Policy Terms và Priority

Một route policy gồm nhiều terms, mỗi term có:

  • Priority: Số thấp hơn = ưu tiên cao hơn (evaluated first)
  • Match conditions: CEL expressions
  • Action: ACCEPT, DROP, hoặc NEXT_TERM (continue to next term)

Evaluation:

  1. Terms được evaluate theo priority (thấp đến cao)
  2. Khi match đầu tiên tìm thấy, action được thực hiện
  3. Nếu action là ACCEPT hoặc DROP, evaluation dừng
  4. Nếu không có term nào match, fail-open (route pass)
Terms: [priority=100, priority=200, priority=300]

Route → Check priority 100:
  Match? → ACCEPT → Done
  No Match? → Continue

Route → Check priority 200:
  Match? → DROP → Done
  No Match? → Continue

Route → Check priority 300:
  Match? → ...
  No Match? → FAIL-OPEN (route pass)

BGP Communities Trong Cloud Router

Khái Niệm BGP Communities

BGP communities (RFC 1997) là một optional transitive path attribute. Mỗi community là một 32-bit value, thường biểu diễn dạng ASN:value (ví dụ: 65000:100). Communities dùng để "tag" routes và cho phép peers filter hoặc change behavior dựa trên tags.

Cách Cloud Router Xử Lý Communities

Cloud Router treats BGP communities as non-transitive attributes. Điều này có hệ quả quan trọng:

  1. Cloud Router DROP communities từ learned routes: Khi Cloud Router nhận một route có communities từ on-prem, communities đó không được giữ lại khi route được install vào VPC route table. Nếu route đó được re-advertise (qua NCC), communities sẽ KHÔNG có trong advertisement.

  2. Import policies CÓ THỂ match communities: Dù communities bị drop khỏi VPC route table, bạn VẪN có thể dùng import policy để match communities của route received và quyết định accept/drop/modify trước khi drop xảy ra. Đây là cơ chế để "phân loại" routes từ on-prem dựa trên communities họ tag.

  3. Export policies CÓ THỂ add communities: Khi advertise routes ra on-prem, export policy có thể add communities để on-prem router biết cách xử lý route đó.

  4. Không support extended communities: Cloud Router hiện tại không support match hoặc modify extended communities attributes.

Well-Known Communities và Giới Hạn

Well-known communities như NO_EXPORT (65535:65281) và NO_ADVERTISE (65535:65282) cần được chỉ định bằng numeric value trong Cloud Router policies, không phải tên. Lý do: Cloud Router không natively nhận biết well-known community names.

Nếu on-prem router gắn NO_EXPORT community lên route và gửi đến Cloud Router, Cloud Router sẽ nhận route đó bình thường nhưng sẽ drop community. Route sẽ không có NO_EXPORT semantics trong VPC. Đây là behavior đặc thù cần biết khi thiết kế routing policies phối hợp với on-prem.


Ứng Dụng Thực Tế

Chặn Routes Không Cần Thiết Từ On-Prem

Scenario: On-prem router advertise 500 routes nhưng VPC chỉ cần 50 routes từ production segment.

bash
# Import policy: chỉ nhận routes từ 10.100.0.0/16
# Drop tất cả còn lại
gcloud compute routers update-bgp-peer ROUTER_NAME \
    --peer-name=PEER_NAME \
    --import-custom-routes

Với CEL:

Term 1 (priority=100):
  Match: destination.ip_prefix.contains('10.100.0.0/16')
  Action: ACCEPT

Term 2 (priority=200):
  Match: true (catch-all)
  Action: DROP

Lý do quan trọng: Giảm số routes trong VPC route table (giúp tránh prefix limit), tránh routes không mong muốn xuất hiện trong VPC, cải thiện security posture.

AS Path Prepending Trong Export Policy

Scenario: Bạn có hai Cloud Routers (hai regions) kết nối đến cùng on-prem network. Bạn muốn traffic từ on-prem chủ yếu đi vào us-central1 (primary), chỉ fail over sang us-east1 (backup) khi cần.

Export policy từ us-east1 Cloud Router:

Term 1 (priority=100):
  Match: destination.ip_prefix (all VPC subnets)
  Action: ACCEPT
  Modify: Prepend AS path (add own ASN 2-3 lần)

On-prem thấy routes từ us-central1 với AS path length 1, routes từ us-east1 với AS path length 3-4 → ưu tiên us-central1.

Lưu ý: Để AS path prepending hoạt động nhất quán, cần dùng standard best-path selection mode (không phải legacy mode).

Tagging Routes Với Communities Để On-Prem Filter

Scenario: VPC có nhiều loại subnets — production, development, management. On-prem muốn phân biệt và apply policies khác nhau.

Export policy:

Term 1 (priority=100):
  Match: destination.ip_prefix.contains('10.1.0.0/16') // production
  Action: ACCEPT
  Add community: '65000:100' // production tag

Term 2 (priority=200):
  Match: destination.ip_prefix.contains('10.2.0.0/16') // dev
  Action: ACCEPT
  Add community: '65000:200' // dev tag

On-prem router có thể dùng community 65000:100 để apply stricter security policies cho production routes.


Giới Hạn Và Constraints

5000 Prefix Limit — Trước Policy Evaluation

Prefix limit (5000 unique prefixes theo mặc định) được áp dụng trước khi import policies evaluate. Điều này có nghĩa:

  1. On-prem advertise 6000 routes
  2. Cloud Router nhận 6000 routes qua BGP
  3. Prefix limit check: chỉ nhận 5000, 1000 bị discard silently
  4. Import policy evaluate: chỉ trên 5000 routes đã được nhận

Bạn không thể dùng import policy để "filter trước" và vượt qua prefix limit. Nếu cần nhận hơn 5000 routes, phải request quota increase.

Hệ quả: Nếu on-prem có 6000 routes và bạn chỉ muốn 50, vẫn cần on-prem filter trước, không thể chỉ dùng import policy phía GCP.

Custom Learned Routes Không Bị Ảnh Hưởng Bởi Policies

Import policies không áp dụng cho custom learned routes — chỉ áp dụng cho BGP-received routes. Custom learned routes đi vào dynamic route control plane theo đường riêng.

Named Sets Là Router-Specific

Không thể share named sets giữa các Cloud Routers. Nếu bạn có 10 Cloud Routers cùng cần cùng một prefix list, bạn phải định nghĩa named set 10 lần — một cho mỗi router. Đây là giới hạn về scale trong môi trường large-scale hybrid connectivity.


Anti-Pattern: Dùng Route Policies Thay Cho On-Prem Filtering

Biểu hiện: Team nghĩ rằng có thể để on-prem advertise toàn bộ routing table (hàng nghìn routes) và dùng import policy phía GCP để filter.

Vì sao sai:

  1. Prefix limit áp dụng trước policies — routes vượt quota sẽ bị drop ngẫu nhiên, không phải theo policy
  2. BGP UPDATE processing overhead tăng (dù routes bị drop sau đó, chúng vẫn phải được parse)
  3. Network bandwidth bị tốn cho routes sẽ bị drop anyway

Cách đúng: Filter tại source (on-prem router). Cloud Router import policy là "defense in depth", không phải primary filter.


References