[PATCH pve-network] fix #5364: bgp|evpn: derivated router-id from mac address for ipv6 underlay

Alexandre Derumier alexandre.derumier at groupe-cyllene.com
Fri Apr 12 14:56:08 CEST 2024


for ipv4, we use the iface ipv4 router-id as router-id need to 32bit.

That's doesn't work for pure ipv6 underlay network.

since https://www.rfc-editor.org/rfc/rfc6286, we can use any 32bit id,
it's just need to be unique in the ASN.

Simply use the last 4 bytes of iface mac address as unique id

Signed-off-by: Alexandre Derumier <alexandre.derumier at groupe-cyllene.com>
---
 src/PVE/Network/SDN/Controllers/BgpPlugin.pm  |  3 ++-
 src/PVE/Network/SDN/Controllers/EvpnPlugin.pm |  7 +++++--
 src/PVE/Network/SDN/Controllers/Plugin.pm     | 21 +++++++++++++++++++
 src/test/run_test_zones.pl                    |  8 +++++++
 4 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/src/PVE/Network/SDN/Controllers/BgpPlugin.pm b/src/PVE/Network/SDN/Controllers/BgpPlugin.pm
index e001faa..53963e5 100644
--- a/src/PVE/Network/SDN/Controllers/BgpPlugin.pm
+++ b/src/PVE/Network/SDN/Controllers/BgpPlugin.pm
@@ -74,12 +74,13 @@ sub generate_controller_config {
     my $bgp = $config->{frr}->{router}->{"bgp $asn"} //= {};
 
     my ($ifaceip, $interface) = PVE::Network::SDN::Zones::Plugin::find_local_ip_interface_peers(\@peers, $loopback);
+    my $routerid = PVE::Network::SDN::Controllers::Plugin::get_router_id($ifaceip, $interface);
 
     my $remoteas = $ebgp ? "external" : $asn;
 
     #global options
     my @controller_config = (
-        "bgp router-id $ifaceip",
+        "bgp router-id $routerid",
         "no bgp default ipv4-unicast",
         "coalesce-time 1000"
     );
diff --git a/src/PVE/Network/SDN/Controllers/EvpnPlugin.pm b/src/PVE/Network/SDN/Controllers/EvpnPlugin.pm
index 23170a6..18eea47 100644
--- a/src/PVE/Network/SDN/Controllers/EvpnPlugin.pm
+++ b/src/PVE/Network/SDN/Controllers/EvpnPlugin.pm
@@ -70,12 +70,13 @@ sub generate_controller_config {
     my $bgp = $config->{frr}->{router}->{"bgp $asn"} //= {};
 
     my ($ifaceip, $interface) = PVE::Network::SDN::Zones::Plugin::find_local_ip_interface_peers(\@peers, $loopback);
+    my $routerid = PVE::Network::SDN::Controllers::Plugin::get_router_id($ifaceip, $interface);
 
     my $remoteas = $ebgp ? "external" : $asn;
 
     #global options
     my @controller_config = (
-	"bgp router-id $ifaceip",
+	"bgp router-id $routerid",
 	"no bgp hard-administrative-reset",
 	"no bgp default ipv4-unicast",
 	"coalesce-time 1000",
@@ -153,6 +154,8 @@ sub generate_controller_zone_config {
     return if !$vrf || !$vrfvxlan || !$asn;
 
     my ($ifaceip, $interface) = PVE::Network::SDN::Zones::Plugin::find_local_ip_interface_peers(\@peers, $loopback);
+    my $routerid = PVE::Network::SDN::Controllers::Plugin::get_router_id($ifaceip, $interface);
+
     my $is_gateway = $exitnodes->{$local_node};
 
     # vrf
@@ -188,7 +191,7 @@ sub generate_controller_zone_config {
 
     #main vrf router
     @controller_config = ();
-    push @controller_config, "bgp router-id $ifaceip";
+    push @controller_config, "bgp router-id $routerid";
     push @controller_config, "no bgp hard-administrative-reset";
     push @controller_config, "no bgp graceful-restart notification";
 
diff --git a/src/PVE/Network/SDN/Controllers/Plugin.pm b/src/PVE/Network/SDN/Controllers/Plugin.pm
index c1c2cfd..5579513 100644
--- a/src/PVE/Network/SDN/Controllers/Plugin.pm
+++ b/src/PVE/Network/SDN/Controllers/Plugin.pm
@@ -118,4 +118,25 @@ sub on_update_hook {
     # do nothing by default
 }
 
+#helpers
+
+sub read_iface_mac {
+    my ($iface) = @_;
+    return PVE::Tools::file_read_firstline("/sys/class/net/$iface/master/address");
+}
+
+sub get_router_id {
+    my ($ip, $iface) = @_;
+
+    return $ip if Net::IP::ip_is_ipv4($ip);
+
+    #for ipv6, use 4 last bytes of iface mac address as unique id
+    my $mac = read_iface_mac($iface);
+
+    die "can't autofind a router-id value from ip or mac" if !$mac;
+
+    my @mac_bytes = split(':', $mac);
+    return hex($mac_bytes[2]).".".hex($mac_bytes[3]).".".hex($mac_bytes[4]).".".hex($mac_bytes[5]);
+}
+
 1;
diff --git a/src/test/run_test_zones.pl b/src/test/run_test_zones.pl
index 274a119..e506bea 100755
--- a/src/test/run_test_zones.pl
+++ b/src/test/run_test_zones.pl
@@ -99,6 +99,14 @@ foreach my $test (@tests) {
 	},
     );
 
+    my $pve_sdn_controllers_plugin;
+    $pve_sdn_controllers_plugin = Test::MockModule->new('PVE::Network::SDN::Controllers::Plugin');
+    $pve_sdn_controllers_plugin->mock(
+	read_iface_mac => sub {
+	    return "bc:24:11:1d:69:60";
+	},
+    );
+
     my ($first_plugin) = %{$sdn_config->{controllers}->{ids}} if defined $sdn_config->{controllers};
     if ($first_plugin) {
 	my $controller_plugin = PVE::Network::SDN::Controllers::Plugin->lookup(
-- 
2.39.2




More information about the pve-devel mailing list