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 0DB7867981 for ; Fri, 31 Jul 2020 19:23:25 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id F2A921F532 for ; Fri, 31 Jul 2020 19:23:24 +0200 (CEST) Received: from mailpro.odiso.net (mailpro.odiso.net [89.248.211.110]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 3AFCF1F527 for ; Fri, 31 Jul 2020 19:23:23 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by mailpro.odiso.net (Postfix) with ESMTP id 1437E122E5B1; Fri, 31 Jul 2020 19:23:23 +0200 (CEST) Received: from mailpro.odiso.net ([127.0.0.1]) by localhost (mailpro.odiso.net [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id CI-Tc_co9pqx; Fri, 31 Jul 2020 19:23:23 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by mailpro.odiso.net (Postfix) with ESMTP id F08FF122E5B3; Fri, 31 Jul 2020 19:23:22 +0200 (CEST) X-Virus-Scanned: amavisd-new at mailpro.odiso.com Received: from mailpro.odiso.net ([127.0.0.1]) by localhost (mailpro.odiso.net [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 2MqT_rTxvtX6; Fri, 31 Jul 2020 19:23:22 +0200 (CEST) Received: from pve.fritz.box (unknown [213.211.148.86]) by mailpro.odiso.net (Postfix) with ESMTPSA id C1DC9122E5B1; Fri, 31 Jul 2020 19:23:22 +0200 (CEST) From: Alexandre Derumier To: pve-devel@pve.proxmox.com Date: Fri, 31 Jul 2020 19:23:18 +0200 Message-Id: <20200731172318.14211-1-aderumier@odiso.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-SPAM-LEVEL: Spam detection results: 0 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_NONE -0.0001 Sender listed at https://www.dnswl.org/, no trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [config.pm] Subject: [pve-devel] [patch pve-container] POC: add/del/update ip from vnet-subnet-ipam 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: Fri, 31 Jul 2020 17:23:25 -0000 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 --- 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; =20 +my $have_sdn; +eval { + require PVE::Network::SDN::Vnets; + require PVE::Network::SDN::Subnets; + $have_sdn =3D 1; +}; + + use base qw(PVE::AbstractConfig); =20 my $nodename =3D PVE::INotify::nodename(); @@ -1145,7 +1153,6 @@ sub parse_lxc_network { my $dc =3D PVE::Cluster::cfs_read_file('datacenter.cfg'); $res->{hwaddr} =3D PVE::Tools::random_ether_addr($dc->{mac_prefix}); } - return $res; } =20 @@ -1225,6 +1232,8 @@ sub vmconfig_hotplug_pending { $cgroup->change_cpu_shares(undef, $confdesc->{cpuunits}->{default}); } elsif ($opt =3D~ m/^net(\d)$/) { my $netid =3D $1; + my $net =3D $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 =3D~ m/^net(\d+)$/) { my $netid =3D $1; my $net =3D $class->parse_lxc_network($value); + update_net_ip($net); $value =3D $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 =3D~ m/^net(\d+)$/) { my $netid =3D $1; my $net =3D $class->parse_lxc_network($conf->{pending}->{$opt}); + my $oldnet =3D $class->parse_lxc_network($conf->{$opt}); + update_net_ip($net, $oldnet); $conf->{pending}->{$opt} =3D $class->print_lxc_network($net); } }; @@ -1590,4 +1602,54 @@ sub get_backup_volumes { return $return_volumes; } =20 +sub update_net_ip { + my ($net, $oldnet) =3D @_; + + return if !$have_sdn; + + my $bridge =3D $net->{bridge}; + my $vnet =3D PVE::Network::SDN::Vnets::get_vnet($bridge); + return if !$vnet->{subnets}; + + if (!$net->{ip}) { + $net->{ip} =3D 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 $o= ldnet->{ip}; + } + + if (!$net->{ip6}) { + $net->{ip6} =3D 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->{ip= 6} && $oldnet->{ip6} ne 'auto' && $oldnet->{ip6} ne 'manual' && $net->{ip= 6} ne 'dhcp'; + PVE::Network::SDN::Vnets::add_ip($vnet, $net->{ip6}) if $net->{ip6} ne = $oldnet->{ip6}; + } + + my ($ip, undef) =3D split(/\//, $net->{ip}); + my ($ip6, undef) =3D split(/\//, $net->{ip6}); + + my ($subnetidv4, $subnetv4) =3D PVE::Network::SDN::Subnets::find_ip_= subnet($ip, $vnet->{subnets}); + my ($subnetidv6, $subnetv6) =3D PVE::Network::SDN::Subnets::find_ip_= subnet($ip6, $vnet->{subnets}); + + $net->{gw} =3D $subnetv4->{gateway} if !$net->{gw}; + $net->{gw6} =3D $subnetv6->{gateway} if !$net->{gw6}; +} + +sub delete_net_ip { + my ($net) =3D @_; + + return if !$have_sdn; + + my $bridge =3D $net->{bridge}; + my $vnet =3D 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; --=20 2.20.1