[pve-devel] [patch pve-container] POC: add/del/update ip from vnet-subnet-ipam

Alexandre Derumier aderumier at odiso.com
Fri Jul 31 19:23:18 CEST 2020


This is a POC to call ip to retreive ip address from ipam.

(it's really just a poc && buggt , it need to be improve for vnet changes, pending config apply/revert,...)

Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 src/PVE/LXC/Config.pm | 64 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 63 insertions(+), 1 deletion(-)

diff --git a/src/PVE/LXC/Config.pm b/src/PVE/LXC/Config.pm
index 5bf12d5..1065778 100644
--- a/src/PVE/LXC/Config.pm
+++ b/src/PVE/LXC/Config.pm
@@ -11,6 +11,14 @@ use PVE::INotify;
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::Tools;
 
+my $have_sdn;
+eval {
+    require PVE::Network::SDN::Vnets;
+    require PVE::Network::SDN::Subnets;
+    $have_sdn = 1;
+};
+
+
 use base qw(PVE::AbstractConfig);
 
 my $nodename = PVE::INotify::nodename();
@@ -1145,7 +1153,6 @@ sub parse_lxc_network {
 	my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg');
 	$res->{hwaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix});
     }
-
     return $res;
 }
 
@@ -1225,6 +1232,8 @@ sub vmconfig_hotplug_pending {
 		$cgroup->change_cpu_shares(undef, $confdesc->{cpuunits}->{default});
 	    } elsif ($opt =~ m/^net(\d)$/) {
 		my $netid = $1;
+		my $net = $class->parse_lxc_network($conf->{$opt});
+		delete_net_ip($net);
 		PVE::Network::veth_delete("veth${vmid}i$netid");
 	    } else {
 		die "skip\n"; # skip non-hotpluggable opts
@@ -1251,6 +1260,7 @@ sub vmconfig_hotplug_pending {
 	    } elsif ($opt =~ m/^net(\d+)$/) {
 		my $netid = $1;
 		my $net = $class->parse_lxc_network($value);
+		update_net_ip($net);
 		$value = $class->print_lxc_network($net);
 		PVE::LXC::update_net($vmid, $conf, $opt, $net, $netid, $rootdir);
 	    } elsif ($opt eq 'memory' || $opt eq 'swap') {
@@ -1327,6 +1337,8 @@ sub vmconfig_apply_pending {
 	    } elsif ($opt =~ m/^net(\d+)$/) {
 		my $netid = $1;
 		my $net = $class->parse_lxc_network($conf->{pending}->{$opt});
+		my $oldnet = $class->parse_lxc_network($conf->{$opt});
+		update_net_ip($net, $oldnet);
 		$conf->{pending}->{$opt} = $class->print_lxc_network($net);
 	    }
 	};
@@ -1590,4 +1602,54 @@ sub get_backup_volumes {
     return $return_volumes;
 }
 
+sub update_net_ip {
+    my ($net, $oldnet) = @_;
+
+    return if !$have_sdn;
+
+    my $bridge = $net->{bridge};
+    my $vnet = PVE::Network::SDN::Vnets::get_vnet($bridge);
+    return if !$vnet->{subnets};
+
+    if (!$net->{ip}) {
+	$net->{ip} = PVE::Network::SDN::Vnets::get_next_free_ip($vnet);
+    } elsif ($net->{ip} ne 'dhcp' && $net->{ip} ne 'manual') {
+	PVE::Network::SDN::Vnets::del_ip($vnet, $oldnet->{ip}) if $oldnet->{ip} && $oldnet->{ip} ne 'dhcp' && $oldnet->{ip} ne 'manual';
+	PVE::Network::SDN::Vnets::add_ip($vnet, $net->{ip}) if $net->{ip} ne $oldnet->{ip};
+    }
+
+    if (!$net->{ip6}) {
+	$net->{ip6} = PVE::Network::SDN::Vnets::get_next_free_ip($vnet, 6);
+    } elsif ($net->{ip6} ne 'auto' && $net->{ip6} ne 'manual' && $net->{ip6} ne 'dhcp') {
+	PVE::Network::SDN::Vnets::del_ip($vnet, $oldnet->{ip6}) if $oldnet->{ip6} && $oldnet->{ip6} ne 'auto' && $oldnet->{ip6} ne 'manual' && $net->{ip6} ne 'dhcp';
+	PVE::Network::SDN::Vnets::add_ip($vnet, $net->{ip6}) if $net->{ip6} ne $oldnet->{ip6};
+    }
+
+    my ($ip, undef) = split(/\//, $net->{ip});
+    my ($ip6, undef) = split(/\//, $net->{ip6});
+
+    my ($subnetidv4, $subnetv4) = PVE::Network::SDN::Subnets::find_ip_subnet($ip, $vnet->{subnets});
+    my ($subnetidv6, $subnetv6) = PVE::Network::SDN::Subnets::find_ip_subnet($ip6, $vnet->{subnets});
+
+    $net->{gw} = $subnetv4->{gateway} if !$net->{gw};
+    $net->{gw6} = $subnetv6->{gateway} if !$net->{gw6};
+}
+
+sub delete_net_ip {
+    my ($net) = @_;
+
+    return if !$have_sdn;
+
+    my $bridge = $net->{bridge};
+    my $vnet = PVE::Network::SDN::Vnets::get_vnet($bridge);
+
+    return if !$vnet->{subnets};
+
+    if ($net->{ip6} && ($net->{ip6} eq 'auto' || $net->{ip6} eq 'manual' || $net->{ip6} eq 'dhcp')) {
+	PVE::Network::SDN::Vnets::del_ip($vnet, $net->{ip6});
+    } elsif ($net->{ip} && $net->{ip} ne 'dhcp' && $net->{ip} ne 'manual') {
+	PVE::Network::SDN::Vnets::del_ip($vnet, $net->{ip});
+    }
+}
+
 1;
-- 
2.20.1





More information about the pve-devel mailing list