From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id AE8D675171 for ; Mon, 12 Jul 2021 00:47:47 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 93175EB6A for ; Mon, 12 Jul 2021 00:47:17 +0200 (CEST) Received: from kvmformation3.odiso.net (globalOdiso.M6Lille.odiso.net [89.248.211.242]) by firstgate.proxmox.com (Proxmox) with ESMTP id 1836EEB09 for ; Mon, 12 Jul 2021 00:47:14 +0200 (CEST) Received: by kvmformation3.odiso.net (Postfix, from userid 0) id AC14EAD7; Mon, 12 Jul 2021 00:47:08 +0200 (CEST) From: Alexandre Derumier To: pve-devel@lists.proxmox.com Date: Mon, 12 Jul 2021 00:46:59 +0200 Message-Id: <20210711224707.63089-2-aderumier@odiso.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210711224707.63089-1-aderumier@odiso.com> References: <20210711224707.63089-1-aderumier@odiso.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 1.056 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% HEADER_FROM_DIFFERENT_DOMAINS 0.001 From and EnvelopeFrom 2nd level mail domains are different KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment KAM_LAZY_DOMAIN_SECURITY 1 Sending domain does not have any anti-forgery methods KHOP_HELO_FCRDNS 0.399 Relay HELO differs from its IP's reverse DNS NO_DNS_FOR_FROM 0.001 Envelope sender has no MX or A DNS records SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_NONE 0.001 SPF: sender does not publish an SPF Record Subject: [pve-devel] [PATCH v2 qemu-server 1/9] add ipam module X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 11 Jul 2021 22:47:47 -0000 Signed-off-by: Alexandre Derumier --- 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