public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Alexandre Derumier <aderumier@odiso.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH qemu-server 1/7] add ipam module
Date: Thu, 24 Jun 2021 15:34:19 +0200	[thread overview]
Message-ID: <20210624133425.3624704-2-aderumier@odiso.com> (raw)
In-Reply-To: <20210624133425.3624704-1-aderumier@odiso.com>

This is the same code than lxc, it can be move to a common module later

Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
---
 PVE/QemuServer/Ipam.pm  | 158 ++++++++++++++++++++++++++++++++++++++++
 PVE/QemuServer/Makefile |   1 +
 2 files changed, 159 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..37b1595
--- /dev/null
+++ b/PVE/QemuServer/Ipam.pm
@@ -0,0 +1,158 @@
+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 ($oldip, $ip) = @_;
+
+    return 1 if !$oldip && $ip;
+    return 1 if !$ip && $oldip;
+    return 1 if $ip && $oldip && $ip ne $oldip;
+}
+
+my $add_net_ip = sub {
+    my ($version, $net, $oldnet, $hostname, $oldhostname, $description) = @_;
+
+    my $oldbridge = $oldnet->{bridge};
+    my $bridge = $net->{bridge};
+    my $mac = $net->{hwaddr};
+    my $ipfield = $version == 4 ? "ip" : "ip6";
+    my $ip = $net->{$ipfield};
+    my $oldip = $oldnet->{$ipfield};
+    my $subnets = PVE::Network::SDN::Vnets::get_subnets($bridge);
+    return if !keys %{$subnets};
+
+    eval {
+        if (!$ip) {
+            my $next_free_ip = PVE::Network::SDN::Vnets::get_next_free_cidr($bridge, $hostname, $mac, $description, $version);
+            $net->{$ipfield} = $next_free_ip if $next_free_ip;
+        } elsif (is_static_ip($ip)) {
+            if (!ip_has_changed($oldip, $ip)) {
+                #update ip attributes if no ip address change
+                PVE::Network::SDN::Vnets::update_cidr($bridge, $ip, $hostname, $oldhostname, $mac, $description);
+            } else {
+                PVE::Network::SDN::Vnets::add_cidr($bridge, $ip, $hostname, $mac, $description);
+            }
+        }
+    };
+    if ($@) {
+        die $@;
+    }
+};
+
+
+my $del_net_ip = sub {
+    my ($version, $oldnet, $net, $hostname, $description) = @_;
+
+    my $oldbridge = $oldnet->{bridge};
+    my $bridge = $net->{bridge};
+    my $ip = $version == 4 ? $net->{ip} : $net->{ip6};
+    my $oldip = $version == 4 ? $oldnet->{ip} : $oldnet->{ip6};
+
+    return if !$oldip || !is_static_ip($oldip);
+
+    my $subnets = PVE::Network::SDN::Vnets::get_subnets($oldbridge);
+    return if !keys %{$subnets};
+
+    eval {
+        PVE::Network::SDN::Vnets::del_cidr($oldbridge, $oldip, $hostname, $description) if !$bridge || $bridge ne $oldbridge || !$ip || $ip ne $oldip;
+    };
+    warn $@ if $@;
+};
+
+
+my $update_net_gateway = sub {
+    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 destroy_net_ip {
+    my ($conf) = @_;
+
+    return if !$have_sdn;
+
+    foreach my $opt (keys %$conf) {
+        next if $opt !~ m/^net(\d+)$/;
+        my $netid = $1;
+        my $oldnet = PVE::LXC::Config->parse_lxc_network($conf->{$opt});
+        my $hostname = $conf->{hostname};
+        my $description = '';
+        delete_net_ip($hostname, $oldnet, undef, $description);
+    }
+}
+
+sub update_net_ip {
+    my ($net, $oldnet, $hostname, $oldhostname, $description) = @_;
+
+    return if !$have_sdn;
+
+    eval {
+        &$add_net_ip(4, $net, $oldnet, $hostname, $oldhostname, $description);
+    };
+    if ($@) {
+        $net->{ip} = $oldnet->{ip};
+        die "can't change ip4: $@\n";
+    }
+
+    eval {
+        &$add_net_ip(6, $net, $oldnet, $hostname, $oldhostname, $description);
+    };
+    if ($@) {
+        my $err = $@;
+        #if error, delete previously added ipv4
+        eval {
+            PVE::Network::SDN::Vnets::del_cidr($net->{bridge}, $net->{ip}, $hostname, $description) if ip_has_changed($oldnet->{ip}, $net->{ip});
+        };
+        $net->{ip6} = $oldnet->{ip6};
+        $net->{ip} = $oldnet->{ip};
+
+        die "error change ipv6: $err\n";
+    }
+
+    delete_net_ip($oldhostname, $oldnet, $net, $description);
+
+    &$update_net_gateway(4, $net);
+    &$update_net_gateway(6, $net);
+}
+
+
+
+sub delete_net_ip {
+    my ($hostname, $oldnet, $net, $description) = @_;
+
+    return if !$have_sdn;
+
+    &$del_net_ip(4, $oldnet, $net, $hostname, $description);
+    &$del_net_ip(6, $oldnet, $net, $hostname, $description);
+}
+
+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.20.1




  reply	other threads:[~2021-06-24 13:35 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-24 13:34 [pve-devel] [PATCH qemu-server 0/7] RFC: sdn: add ipam support Alexandre Derumier
2021-06-24 13:34 ` Alexandre Derumier [this message]
2021-06-24 13:34 ` [pve-devel] [PATCH qemu-server 2/7] add print_ipconfig Alexandre Derumier
2021-06-24 13:34 ` [pve-devel] [PATCH qemu-server 3/7] add ip options to netdescr Alexandre Derumier
2021-06-24 13:34 ` [pve-devel] [PATCH qemu-server 4/7] add vmconfig_update_net_ip on device hotplug Alexandre Derumier
2021-06-24 13:34 ` [pve-devel] [PATCH qemu-server 5/7] add vmconfig_delete_net_ip on device unplug Alexandre Derumier
2021-06-24 13:34 ` [pve-devel] [PATCH qemu-server 6/7] api2: add update ip support Alexandre Derumier
2021-06-24 13:34 ` [pve-devel] [PATCH qemu-server 7/7] api2: add revert " Alexandre Derumier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210624133425.3624704-2-aderumier@odiso.com \
    --to=aderumier@odiso.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal