From dcfe9c677a003cfbcacdfec2f87b4b7b44d88a2c Mon Sep 17 00:00:00 2001 From: 186526 Date: Wed, 28 Dec 2022 01:14:15 +0800 Subject: [PATCH] Fix: bgp route selection --- lib/10-community-transit.conf | 40 ++++++++++++++++++++++++-------- lib/20-community-downstream.conf | 4 +++- lib/20-community-net186.conf | 22 +++++++++++------- lib/20-community-peer.conf | 5 ++-- protocol/kernel.conf | 2 +- 5 files changed, 50 insertions(+), 23 deletions(-) diff --git a/lib/10-community-transit.conf b/lib/10-community-transit.conf index 723f497..e37a3ae 100644 --- a/lib/10-community-transit.conf +++ b/lib/10-community-transit.conf @@ -1,27 +1,32 @@ function pub_add_communities(int ASN; int PeerType) { # DIRECT PEER if (PeerType=0) then { - bgp_large_community.add((LOCAL_ASN, 110, 0)); - bgp_large_community.add((LOCAL_ASN, 110, 2)); + if bgp_large_community !~ [(LOCAL_ASN, 110, *)] then { + bgp_large_community.add((LOCAL_ASN, 110, 0)); + bgp_large_community.add((LOCAL_ASN, 110, 2)); + } # PEER via IX } else if (PeerType=1) then { - bgp_large_community.add((LOCAL_ASN, 110, 0)); - bgp_large_community.add((LOCAL_ASN, 110, 1)); + if bgp_large_community !~ [(LOCAL_ASN, 110, *)] then { + bgp_large_community.add((LOCAL_ASN, 110, 0)); + bgp_large_community.add((LOCAL_ASN, 110, 1)); + } + # UPSTREAM }else if (PeerType=10) then { - bgp_large_community.add((LOCAL_ASN, 110, 10)); + if bgp_large_community !~ [(LOCAL_ASN, 110, *)] then bgp_large_community.add((LOCAL_ASN, 110, 10)); # DOWNSTREAM } else if (PeerType=20) then { - bgp_large_community.add((LOCAL_ASN, 110, 20)); + if bgp_large_community !~ [(LOCAL_ASN, 110, *)] then bgp_large_community.add((LOCAL_ASN, 110, 20)); } # Finished (LOCAL_ASN, 110, *); - if (LOCAL_ASN, 120, POP) ~ bgp_large_community then bgp_large_community.add((LOCAL_ASN, 120, POP)); - if (LOCAL_ASN, 121, REGION) ~ bgp_large_community then bgp_large_community.add((LOCAL_ASN, 121, REGION)); + if bgp_large_community !~ [(LOCAL_ASN, 120, *)] then bgp_large_community.add((LOCAL_ASN, 120, POP)); + if bgp_large_community !~ [(LOCAL_ASN, 121, *)] then bgp_large_community.add((LOCAL_ASN, 121, REGION)); # Finished (LOCAL_ASN, 120~130, *); return true; @@ -67,18 +72,33 @@ function pub_process_communities(int ASN; int PeerType) { return true; } +function pub_process_path() { + # Delete Self eBGP Confed Path. + bgp_path.delete([4200000000..4225479999]); + # Route is via Europe & Africa & Middle East + if bgp_large_community ~ [(LOCAL_ASN, 122, 100), (LOCAL_ASN, 122, 300..310), (LOCAL_ASN, 122, 600)] then bgp_path.prepend(LOCAL_ASN); + # Route is via America + if bgp_large_community ~ [(LOCAL_ASN, 122, 200..299)] then bgp_path.prepend(LOCAL_ASN); + # Route is via East Asia & Oceania + if bgp_large_community ~ [(LOCAL_ASN, 122, 400..500)] then bgp_path.prepend(LOCAL_ASN); + # Self PoP + if (LOCAL_ASN, 120, POP) ~ bgp_large_community then bgp_path.delete(LOCAL_ASN); +} + function transit_import_filter(int ASN) { if !is_valid() then return false; pub_add_communities(ASN, 10); pub_preprocess_communities(); + bgp_local_pref = 400 - bgp_path.len * 10; return true; } function transit_export_filter(int ASN) { - # Delete Self eBGP Confed Path. - bgp_path.delete([4200000000..4225479999]); + pub_process_path(); if !is_valid() then return false; + if bgp_large_community ~ [(LOCAL_ASN, 110, 0), (LOCAL_ASN, 110, 10)] then return false; if bgp_path.last !~ DOWNSTREAM_ASN && source = RTS_BGP then return false; + pub_add_communities(ASN, 10); if pub_process_communities(ASN, 10) then return true; else return false; } \ No newline at end of file diff --git a/lib/20-community-downstream.conf b/lib/20-community-downstream.conf index 64820c2..ceb80cb 100644 --- a/lib/20-community-downstream.conf +++ b/lib/20-community-downstream.conf @@ -3,12 +3,14 @@ function downstream_import_filter(int ASN) { if bgp_path.last !~ DOWNSTREAM_ASN then return false; pub_add_communities(ASN, 20); pub_preprocess_communities(); + bgp_local_pref = 500 - bgp_path.len * 10; return true; } function downstream_export_filter(int ASN) { + pub_process_path(); if !is_valid() then return false; - bgp_path.delete([4200000000..4225479999]); + pub_add_communities(ASN, 10); if pub_process_communities(ASN, 20) then return true; else return false; } \ No newline at end of file diff --git a/lib/20-community-net186.conf b/lib/20-community-net186.conf index 34febc5..84d4162 100644 --- a/lib/20-community-net186.conf +++ b/lib/20-community-net186.conf @@ -4,12 +4,10 @@ function net186_import_filter(int POPID) { public_bgp_path = delete(bgp_path, [4200000000..4225479999]); - if filter(bgp_large_community, [(LOCAL_ASN, 120, *)]).len != 0 then bgp_large_community.add((LOCAL_ASN, 120, POP)); - if filter(bgp_large_community, [(LOCAL_ASN, 121, *)]).len != 0 then bgp_large_community.add((LOCAL_ASN, 121, REGION)); - if (LOCAL_ASN, 122, REGION) ~ bgp_large_community then bgp_large_community.add((LOCAL_ASN, 122, REGION)); - # ROUTE_DECIDER Start - if (LOCAL_ASN, 110, 2) ~ bgp_large_community then bgp_local_pref = 500; - else if (LOCAL_ASN, 110, 1) ~ bgp_large_community then bgp_local_pref = 400; + if (LOCAL_ASN, 122, REGION) !~ bgp_large_community then bgp_large_community.add((LOCAL_ASN, 122, REGION)); + + if bgp_large_community ~ [(LOCAL_ASN, 110, 0), (LOCAL_ASN, 110, 2), (LOCAL_ASN, 110, 20)] then bgp_local_pref = 500; + else if bgp_large_community ~ [(LOCAL_ASN, 110, 1), (LOCAL_ASN, 110, 10)] then bgp_local_pref = 400; else bgp_local_pref = 300; viaRegion = 0; @@ -20,13 +18,16 @@ function net186_import_filter(int POPID) # Route is via East Asia & Oceania if bgp_large_community ~ [(LOCAL_ASN, 122, 400..500)] then viaRegion = viaRegion + 1; - if viaRegion > 2 then return false; - else if viaRegion = 2 then bgp_local_pref = bgp_local_pref - 50; + if viaRegion >= 3 then bgp_local_pref = bgp_local_pref - 50; # Route is via China mainland but not advertise in China mainland. if (LOCAL_ASN, 122, 430) ~ bgp_large_community && REGION != 430 && (LOCAL_ASN, 120, 430) ~ bgp_large_community then return false; - bgp_local_pref = bgp_local_pref - filter(bgp_large_community, [(LOCAL_ASN, 122, *)]).len * 10; + bgp_local_pref = bgp_local_pref - filter(bgp_large_community, [(LOCAL_ASN, 122, *)]).len * 5; + + bgp_local_pref = bgp_local_pref - public_bgp_path.len * 10; + + return true; }; function net186_export_filter(int POPID) { @@ -46,5 +47,8 @@ function net186_export_filter(int POPID) { return false; } + if bgp_large_community !~ [(LOCAL_ASN, 120, *)] then bgp_large_community.add((LOCAL_ASN, 120, POP)); + if bgp_large_community !~ [(LOCAL_ASN, 121, *)] then bgp_large_community.add((LOCAL_ASN, 121, REGION)); + return true; }; \ No newline at end of file diff --git a/lib/20-community-peer.conf b/lib/20-community-peer.conf index e890f44..180a51d 100644 --- a/lib/20-community-peer.conf +++ b/lib/20-community-peer.conf @@ -12,14 +12,15 @@ function direct_peer_import_filter(int ASN) { if !is_peer_route() then return false; pub_add_communities(ASN, 0); pub_preprocess_communities(); + bgp_local_pref = 500 - bgp_path.len * 10; return true; } function direct_peer_export_filter(int ASN) { - # Delete Self eBGP Confed Path. - bgp_path.delete([4200000000..4225479999]); + pub_process_path(); if !is_valid() then return false; if !is_peer_route() then return false; + pub_add_communities(ASN, 10); if pub_process_communities(ASN, 0) then return true; else return false; } \ No newline at end of file diff --git a/protocol/kernel.conf b/protocol/kernel.conf index f5864ef..6e68f23 100644 --- a/protocol/kernel.conf +++ b/protocol/kernel.conf @@ -13,7 +13,7 @@ protocol kernel { # Blackhole (LOCAL_ASN, 0, 666) if (LOCAL_ASN, 0, 666) ~ bgp_large_community then dest = RTD_BLACKHOLE; # Only Announced (LOCAL_ASN, 0, 665) - if (LOCAL_ASN, 0, 665) ~ bgp_large_community then reject; + if (LOCAL_ASN, 0, 665) ~ bgp_large_community && bgp_path.last !~ [4200000000..4225479999] then reject; krt_prefsrc = ROUTER_IP; accept; };