[pve-devel] [PATCH v2 qemu-server 1/9] add ipam module

Alexandre Derumier aderumier at odiso.com
Mon Jul 12 00:46:59 CEST 2021


Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 PVE/QemuServer/Ipam.pm  | 141 ++++++++++++++++++++++++++++++++++++++++
 PVE/QemuServer/Makefile |   1 +
 2 files changed, 142 insertions(+)
 create mode 100644 PVE/QemuServer/Ipam.pm

diff --git a/PVE/QemuServer/Ipam.pm b/PVE/QemuServer/Ipam.pm
new file mode 100644
index 0000000..202474c
--- /dev/null
+++ b/PVE/QemuServer/Ipam.pm
@@ -0,0 +1,141 @@
+package PVE::QemuServer::Ipam;
+
+use strict;
+use warnings;
+
+my $have_sdn;
+eval {
+    require PVE::Network::SDN::Zones;
+    $have_sdn = 1;
+};
+
+sub is_static_ip {
+    my ($ip) = @_;
+
+    return 1 if $ip !~ m/(dhcp|manual|auto)$/;
+}
+
+
+sub ip_has_changed {
+    my ($version, $net, $oldnet) = @_;
+
+    my $ipfield = $version == 4 ? "ip" : "ip6";
+    my $ip = $net->{$ipfield};
+    my $oldip = $oldnet->{$ipfield};
+
+    return 1 if !$oldip && $ip;
+    return 1 if !$ip && $oldip;
+    return 1 if $ip && $oldip && $ip ne $oldip;
+    return 1 if !$oldnet->{bridge} || !$net->{bridge} || $oldnet->{bridge} ne $net->{bridge};
+}
+
+sub add_net_ip {
+    my ($version, $net, $hostname, $description, $skipdns) = @_;
+
+    my $bridge = $net->{bridge};
+    my $mac = $net->{hwaddr};
+    my $ipfield = $version == 4 ? "ip" : "ip6";
+    my $ip = $net->{$ipfield};
+    my $subnets = PVE::Network::SDN::Vnets::get_subnets($bridge);
+    return if !keys %{$subnets};
+
+    eval {
+        if (!$ip) {
+	print"add_find_free\n";
+
+            my $next_free_ip = PVE::Network::SDN::Vnets::get_next_free_cidr($bridge, $hostname, $mac, $description, $version, $skipdns);
+            $net->{$ipfield} = $next_free_ip if $next_free_ip;
+        } elsif (is_static_ip($ip)) {
+	print"add_cidr $ip\n";
+	    PVE::Network::SDN::Vnets::add_cidr($bridge, $ip, $hostname, $mac, $description, $skipdns);
+        }
+    };
+    if ($@) {
+        die $@;
+    }
+}
+
+sub update_net_ip {
+    my ($version, $net, $hostname, $oldhostname, $description, $skipdns) = @_;
+
+    my $bridge = $net->{bridge};
+    my $mac = $net->{hwaddr};
+    my $ipfield = $version == 4 ? "ip" : "ip6";
+    my $ip = $net->{$ipfield};
+
+    return if !$ip || !is_static_ip($ip);
+
+    my $subnets = PVE::Network::SDN::Vnets::get_subnets($bridge);
+    return if !keys %{$subnets};
+
+    eval {
+	#update ip attributes if no ip address change
+	print"update_cidr $ip\n";
+	PVE::Network::SDN::Vnets::update_cidr($bridge, $ip, $hostname, $oldhostname, $mac, $description, $skipdns);
+    };
+    if ($@) {
+        die $@;
+    }
+}
+
+
+sub update_net_gateway {
+    my ($version, $net) = @_;
+
+    my $bridge = $net->{bridge};
+    my $netip = $version == 4 ? $net->{ip} : $net->{ip6};
+    my $gwfield = $version == 4 ? "gw" : "gw6";
+
+    return if (!$netip || !is_static_ip($netip));
+
+    my $subnets = PVE::Network::SDN::Vnets::get_subnets($bridge);
+    return if !keys %{$subnets};
+
+    #update gateway
+    my ($ip, $mask) = split(/\//, $netip);
+    my ($subnetid, $subnet) = PVE::Network::SDN::Subnets::find_ip_subnet($ip, $mask, $subnets);
+    my $gw = $subnet->{gateway} if $subnet->{gateway};
+    $net->{$gwfield} = $gw if $gw;
+
+}
+
+sub delete_net_ip {
+    my ($version, $net, $hostname, $skipdns) = @_;
+
+    return if !$have_sdn;
+
+    my $bridge = $net->{bridge};
+    my $ip = $version == 4 ? $net->{ip} : $net->{ip6};
+
+    return if !$ip || !is_static_ip($ip);
+
+    my $subnets = PVE::Network::SDN::Vnets::get_subnets($bridge);
+    return if !keys %{$subnets};
+
+    eval {
+	print"del_cidr $ip\n";
+
+        PVE::Network::SDN::Vnets::del_cidr($bridge, $ip, $hostname, $skipdns);
+    };
+    warn $@ if $@;
+}
+
+sub is_ip_referenced {
+    my ($version, $nets, $net, $skipsnap, $checkcurrent) = @_;
+
+    if ($checkcurrent) {
+        foreach my $opt (keys %{$nets->{current}}) {
+            return 1 if !ip_has_changed($version, $net, $nets->{current}->{$opt});
+        }
+    }
+
+    foreach my $snapname (keys %{$nets->{snapshots}}) {
+        next if $skipsnap && $skipsnap eq $snapname;
+        my $snapnets = $nets->{snapshots}->{$snapname};
+        foreach my $opt (keys %{$snapnets}) {
+            return 1 if !ip_has_changed($version, $net, $snapnets->{$opt});
+        }
+    }
+}
+
+1;
\ No newline at end of file
diff --git a/PVE/QemuServer/Makefile b/PVE/QemuServer/Makefile
index e4ed184..0292383 100644
--- a/PVE/QemuServer/Makefile
+++ b/PVE/QemuServer/Makefile
@@ -11,6 +11,7 @@ SOURCES=PCI.pm		\
 	CPUConfig.pm	\
 	CGroup.pm	\
 	Drive.pm	\
+	Ipam.pm		\
 
 .PHONY: install
 install: ${SOURCES}
-- 
2.30.2





More information about the pve-devel mailing list