[pve-devel] [PATCH v2 pve-network 09/13] generate network config in a hash

Alexandre Derumier aderumier at odiso.com
Thu Aug 29 12:32:50 CEST 2019


more clean and avoid duplicate interface

Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 PVE/Network/SDN.pm             | 26 ++++++++--
 PVE/Network/SDN/FrrPlugin.pm   | 33 ++++++------
 PVE/Network/SDN/VlanPlugin.pm  | 32 ++++++------
 PVE/Network/SDN/VxlanPlugin.pm | 95 +++++++++++++++++-----------------
 test/generateconfig.pl         |  7 +--
 5 files changed, 108 insertions(+), 85 deletions(-)

diff --git a/PVE/Network/SDN.pm b/PVE/Network/SDN.pm
index 9d61e08..9d8006e 100644
--- a/PVE/Network/SDN.pm
+++ b/PVE/Network/SDN.pm
@@ -128,7 +128,7 @@ sub generate_etc_network_config {
     }
 
     #generate configuration
-    my $rawconfig = "";
+    my $config = {};
     foreach my $id (keys %{$vnet_cfg->{ids}}) {
 	my $vnet = $vnet_cfg->{ids}->{$id};
 	my $zone = $vnet->{transportzone};
@@ -146,10 +146,30 @@ sub generate_etc_network_config {
 	}
 
 	my $plugin = PVE::Network::SDN::Plugin->lookup($plugin_config->{type});
-	$rawconfig .= $plugin->generate_sdn_config($plugin_config, $zone, $id, $vnet, $uplinks);
+	$plugin->generate_sdn_config($plugin_config, $zone, $id, $vnet, $uplinks, $config);
     }
 
-    return $rawconfig;
+    my $network_config = $config->{network};
+    my $raw_network_config = "";
+    foreach my $iface (keys %$network_config) {
+	$raw_network_config .= "\n";
+	$raw_network_config .= "auto $iface\n";
+	$raw_network_config .= "iface $iface\n";
+	foreach my $option (@{$network_config->{$iface}}) {
+	    $raw_network_config .= "\t$option\n";
+	}
+    }
+
+    my $frr_config = $config->{frr};
+    my $raw_frr_config = "";
+    foreach my $asn (keys %$frr_config) {
+	$raw_frr_config .= "router bgp $asn";
+	foreach my $option (@{$frr_config->{$asn}}) {
+	    $raw_frr_config .= " $option\n";
+	}
+    }
+
+    return wantarray ? ($raw_network_config, $raw_frr_config) : $raw_network_config;
 }
 
 sub write_etc_network_config {
diff --git a/PVE/Network/SDN/FrrPlugin.pm b/PVE/Network/SDN/FrrPlugin.pm
index 4db9ba6..80ca417 100644
--- a/PVE/Network/SDN/FrrPlugin.pm
+++ b/PVE/Network/SDN/FrrPlugin.pm
@@ -34,8 +34,8 @@ sub options {
 }
 
 # Plugin implementation
-sub generate_frr_config {
-    my ($class, $plugin_config, $zoneid, $vnetid, $vnet, $uplinks) = @_;
+sub generate_sdn_config {
+    my ($class, $plugin_config, $zoneid, $vnetid, $vnet, $uplinks, $config) = @_;
 
     my $asn = $plugin_config->{'asn'};
     my @peers = split(',', $plugin_config->{'peers'}) if $plugin_config->{'peers'};
@@ -52,28 +52,29 @@ sub generate_frr_config {
 	$ifaceip = get_first_local_ipv4_from_interface($iface);
     }
 
-    my $config = "\n";
-    $config .= "router bgp $asn\n";
-    $config .= "bgp router-id $ifaceip\n";
-    $config .= "no bgp default ipv4-unicast\n";
-    $config .= "coalesce-time 1000\n";
+    my @router_config = ();
+
+    push @router_config, "router bgp $asn";
+    push @router_config, "bgp router-id $ifaceip";
+    push @router_config, "coalesce-time 1000";
 
     foreach my $address (@peers) {
 	next if $address eq $ifaceip;
-	$config .= "neighbor $address remote-as $asn\n";
+	push @router_config, "neighbor $address remote-as $asn";
     } 
-    $config .= "!\n";
-    $config .= "address-family l2vpn evpn\n";
+    push @router_config, "!";
+    push @router_config, "address-family l2vpn evpn";
     foreach my $address (@peers) {
 	next if $address eq $ifaceip;
-	$config .= " neighbor $address activate\n";
+	push @router_config, " neighbor $address activate";
     }
-    $config .= " advertise-all-vni\n";
-    $config .= "exit-address-family\n";
-    $config .= "!\n";
-    $config .= "line vty\n";
-    $config .= "!\n";
+    push @router_config, " advertise-all-vni";
+    push @router_config, "exit-address-family";
+    push @router_config, "!";
+    push @router_config, "line vty";
+    push @router_config, "!";
 
+    push(@{$config->{frr}->{$asn}}, @router_config);
 
     return $config;
 }
diff --git a/PVE/Network/SDN/VlanPlugin.pm b/PVE/Network/SDN/VlanPlugin.pm
index 88a1274..d2b7475 100644
--- a/PVE/Network/SDN/VlanPlugin.pm
+++ b/PVE/Network/SDN/VlanPlugin.pm
@@ -57,7 +57,7 @@ sub options {
 
 # Plugin implementation
 sub generate_sdn_config {
-    my ($class, $plugin_config, $zoneid, $vnetid, $vnet, $uplinks) = @_;
+    my ($class, $plugin_config, $zoneid, $vnetid, $vnet, $uplinks, $config) = @_;
 
     my $tag = $vnet->{tag};
     my $mtu = $vnet->{mtu};
@@ -72,20 +72,22 @@ sub generate_sdn_config {
     my $iface = $uplinks->{$uplink}->{name};
     $iface = "uplink${uplink}" if !$iface;
     $iface .= ".$tag";
-    my $config = "\n";
-    $config .= "auto $iface\n";
-    $config .= "iface $iface inet manual\n";
-    $config .= "        vlan-protocol $vlanprotocol\n" if $vlanprotocol;
-    $config .= "        mtu $mtu\n" if $mtu;
-    $config .= "\n";
-    $config .= "auto $vnetid\n";
-    $config .= "iface $vnetid inet manual\n";
-    $config .= "        bridge_ports $iface\n";
-    $config .= "        bridge_stp off\n";
-    $config .= "        bridge_fd 0\n";
-    $config .= "        bridge-vlan-aware yes \n" if $vlanaware;
-    $config .= "        mtu $mtu\n" if $mtu;
-    $config .= "        alias $alias\n" if $alias;
+
+    #tagged interface
+    my @iface_config = ();
+    push @iface_config, "vlan-protocol $vlanprotocol" if $vlanprotocol;
+    push @iface_config, "mtu $mtu" if $mtu;
+    push(@{$config->{network}->{$iface}}, @iface_config) if !$config->{network}->{$iface};
+
+    #vnet bridge
+    @iface_config = ();
+    push @iface_config, "bridge_ports $iface";
+    push @iface_config, "bridge_stp off";
+    push @iface_config, "bridge_fd 0";
+    push @iface_config, "bridge-vlan-aware yes" if $vlanaware;
+    push @iface_config, "mtu $mtu" if $mtu;
+    push @iface_config, "alias $alias" if $alias;
+    push(@{$config->{network}->{$vnetid}}, @iface_config) if !$config->{network}->{$vnetid};
 
     return $config;
 }
diff --git a/PVE/Network/SDN/VxlanPlugin.pm b/PVE/Network/SDN/VxlanPlugin.pm
index f090ce8..6e247a7 100644
--- a/PVE/Network/SDN/VxlanPlugin.pm
+++ b/PVE/Network/SDN/VxlanPlugin.pm
@@ -59,7 +59,7 @@ sub options {
 
 # Plugin implementation
 sub generate_sdn_config {
-    my ($class, $plugin_config, $zoneid, $vnetid, $vnet, $uplinks) = @_;
+    my ($class, $plugin_config, $zoneid, $vnetid, $vnet, $uplinks, $config) = @_;
 
     my $tag = $vnet->{tag};
     my $alias = $vnet->{alias};
@@ -87,68 +87,67 @@ sub generate_sdn_config {
     $mtu = $uplinks->{$uplink}->{mtu} - 50 if $uplinks->{$uplink}->{mtu};
     $mtu = $vnet->{mtu} if $vnet->{mtu};
 
-    my $config = "\n";
-    $config .= "auto vxlan$vnetid\n";
-    $config .= "iface vxlan$vnetid\n";
-    $config .= "       vxlan-id $tag\n";
+    #vxlan interface
+    my @iface_config = ();
+    push @iface_config, "vxlan-id $tag";
 
     if($multicastaddress) {
-	$config .= "       vxlan-svcnodeip $multicastaddress\n";
-	$config .= "       vxlan-physdev $iface\n";
+	push @iface_config, "vxlan-svcnodeip $multicastaddress";
+	push @iface_config, "vxlan-physdev $iface";
     } elsif (@unicastaddress) {
 
 	foreach my $address (@unicastaddress) {
 	    next if $address eq $ifaceip;
-	    $config .= "       vxlan_remoteip $address\n";
+	    push @iface_config, "vxlan_remoteip $address";
 	}
     } else {
-	$config .= "       vxlan-local-tunnelip $ifaceip\n" if $ifaceip;
-	$config .= "       bridge-learning off\n";
-	$config .= "       bridge-arp-nd-suppress on\n";
+	push @iface_config, "vxlan-local-tunnelip $ifaceip" if $ifaceip;
+	push @iface_config, "bridge-learning off";
+	push @iface_config, "bridge-arp-nd-suppress on";
     }
 
-    $config .= "       mtu $mtu\n" if $mtu;
-    $config .= "\n";
-    $config .= "auto $vnetid\n";
-    $config .= "iface $vnetid\n";
-    $config .= "       address $ipv4\n" if $ipv4;
-    $config .= "       address $ipv6\n" if $ipv6;
-    $config .= "       hwaddress $mac\n" if $mac;
-    $config .= "       bridge_ports vxlan$vnetid\n";
-    $config .= "       bridge_stp off\n";
-    $config .= "       bridge_fd 0\n";
-    $config .= "       mtu $mtu\n" if $mtu;
-    $config .= "       alias $alias\n" if $alias;
-    $config .= "       vrf $vrf\n" if $vrf;
+    push @iface_config, "mtu $mtu" if $mtu;
+    push(@{$config->{network}->{"vxlan$vnetid"}}, @iface_config) if !$config->{network}->{"vxlan$vnetid"};
+
+    #vnet bridge
+    @iface_config = ();
+    push @iface_config, "address $ipv4" if $ipv4;
+    push @iface_config, "address $ipv6" if $ipv6;
+    push @iface_config, "hwaddress $mac" if $mac;
+    push @iface_config, "bridge_ports vxlan$vnetid";
+    push @iface_config, "bridge_stp off";
+    push @iface_config, "bridge_fd 0";
+    push @iface_config, "mtu $mtu" if $mtu;
+    push @iface_config, "alias $alias" if $alias;
+    push @iface_config, "vrf $vrf" if $vrf;
+    push(@{$config->{network}->{$vnetid}}, @iface_config) if !$config->{network}->{$vnetid};
 
     if ($vrf) {
-	$config .= "\n";
-	$config .= "auto $vrf\n";
-	$config .= "iface $vrf\n";
-	$config .= "       vrf-table auto\n";
+	#vrf intreface
+	@iface_config = ();
+	push @iface_config, "vrf-table auto";
+	push(@{$config->{network}->{$vrf}}, @iface_config) if !$config->{network}->{$vrf};
 
 	if ($vrfvxlan) {
-
-	    my $vxlanvrf = "vxlan$vrf";
+	    #l3vni vxlan interface
+	    my $iface_vxlan = "vxlan$vrf";
+	    @iface_config = ();
+	    push @iface_config, "vxlan-id $vrfvxlan";
+	    push @iface_config, "vxlan-local-tunnelip $ifaceip" if $ifaceip;
+	    push @iface_config, "bridge-learning off";
+	    push @iface_config, "bridge-arp-nd-suppress on";
+	    push @iface_config, "mtu $mtu" if $mtu;
+	    push(@{$config->{network}->{$iface_vxlan}}, @iface_config) if !$config->{network}->{$iface_vxlan};
+
+	    #l3vni bridge
 	    my $brvrf = "br$vrf";
-
-	    $config .= "\n";
-	    $config .= "auto $vxlanvrf\n";
-	    $config .= "iface $vxlanvrf\n";
-	    $config .= "	vxlan-id $vrfvxlan\n";
-	    $config .= "	vxlan-local-tunnelip $ifaceip\n" if $ifaceip;
-	    $config .= "	bridge-learning off\n";
-	    $config .= "	bridge-arp-nd-suppress on\n";
-	    $config .= "	mtu $mtu\n" if $mtu;
-
-	    $config .= "\n";
-	    $config .= "auto $brvrf\n";
-	    $config .= "iface $brvrf\n";
-	    $config .= "	bridge-ports $vxlanvrf\n";
-	    $config .= "	bridge_stp off\n";
-	    $config .= "	bridge_fd 0\n";
-	    $config .= "	mtu $mtu\n" if $mtu;
-	    $config .= "	vrf $vrf\n";
+	    @iface_config = ();
+	    push @iface_config, "bridge-ports $iface_vxlan";
+	    push @iface_config, "bridge_stp off";
+	    push @iface_config, "bridge_fd 0";
+	    push @iface_config, "mtu $mtu" if $mtu;
+	    push @iface_config, "vrf $vrf";
+	    push(@{$config->{network}->{$brvrf}}, @iface_config) if !$config->{network}->{$brvrf};
 	}
     }
 
diff --git a/test/generateconfig.pl b/test/generateconfig.pl
index 1be9afd..6d8b80a 100644
--- a/test/generateconfig.pl
+++ b/test/generateconfig.pl
@@ -6,6 +6,7 @@ use PVE::Cluster qw(cfs_read_file);
 use PVE::Network::SDN;
 
 
-my $rawconfig = PVE::Network::SDN::generate_etc_network_config();
-PVE::Network::SDN::write_etc_network_config($rawconfig);
-print $rawconfig;
+my ($network_config, $frr_config) = PVE::Network::SDN::generate_etc_network_config();
+PVE::Network::SDN::write_etc_network_config($network_config);
+print $network_config;
+print $frr_config;
-- 
2.20.1



More information about the pve-devel mailing list