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 3EB0064086 for ; Mon, 5 Oct 2020 17:11:37 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 0FC751EA8A for ; Mon, 5 Oct 2020 17:11:32 +0200 (CEST) Received: from kvmformation1.odiso.net (globalOdiso.M6Lille.odiso.net [89.248.211.242]) by firstgate.proxmox.com (Proxmox) with ESMTP id 2ECA31EA82 for ; Mon, 5 Oct 2020 17:11:31 +0200 (CEST) Received: by kvmformation1.odiso.net (Postfix, from userid 0) id 1B7F1E8980; Mon, 5 Oct 2020 17:11:31 +0200 (CEST) From: Alexandre Derumier To: pve-devel@lists.proxmox.com Date: Mon, 5 Oct 2020 17:11:29 +0200 Message-Id: <20201005151129.464353-1-aderumier@odiso.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 1 AWL -0.363 Adjusted score from AWL reputation of From: address HEADER_FROM_DIFFERENT_DOMAINS 0.248 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.398 Relay HELO differs from its IP's reverse DNS NO_DNS_FOR_FROM 0.379 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] POC v3: 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: Mon, 05 Oct 2020 15:11:37 -0000 This is a POC to call ip to retreive ip address from ipam. (it's really just a poc && buggy , it need to be improve for vnet changes, pending config apply/revert,...) Signed-off-by: Alexandre Derumier --- src/PVE/LXC/Config.pm | 107 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 106 insertions(+), 1 deletion(-) diff --git a/src/PVE/LXC/Config.pm b/src/PVE/LXC/Config.pm index 49f599b..10ae3d1 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(); @@ -1151,7 +1159,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; } @@ -1231,6 +1238,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($conf->{hostname}, $net); PVE::Network::veth_delete("veth${vmid}i$netid"); } else { die "skip\n"; # skip non-hotpluggable opts @@ -1257,6 +1266,7 @@ sub vmconfig_hotplug_pending { } elsif ($opt =~ m/^net(\d+)$/) { my $netid = $1; my $net = $class->parse_lxc_network($value); + update_net_ip($conf->{hostname}, $net, undef, "vm:$vmid net:$netid"); $value = $class->print_lxc_network($net); PVE::LXC::update_net($vmid, $conf, $opt, $net, $netid, $rootdir); } elsif ($opt eq 'memory' || $opt eq 'swap') { @@ -1333,6 +1343,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($conf->{hostname}, $net, $oldnet, "vm:$vmid net:$netid"); $conf->{pending}->{$opt} = $class->print_lxc_network($net); } }; @@ -1596,4 +1608,97 @@ sub get_backup_volumes { return $return_volumes; } +sub update_net_ip { + my ($hostname, $net, $oldnet, $description) = @_; + + return if !$have_sdn; + + my $oldbridge = $oldnet->{bridge}; + my $bridge = $net->{bridge}; + + my $subnets = PVE::Network::SDN::Vnets::get_subnets($bridge); + + return if !keys %{$subnets}; + + #delete old ip + if ($oldnet->{ip} && $oldnet->{ip} ne 'dhcp' && $oldnet->{ip} ne 'manual') { + my $deletebridge = $oldbridge ne $bridge ? $oldbridge : $bridge; + eval { + PVE::Network::SDN::Vnets::del_cidr($deletebridge, $oldnet->{ip}, $hostname, $description) if !$net->{ip} || $net->{ip} ne $oldnet->{ip}; + #writeconfig ? + }; + warn $@ if $@; + } + + eval { + if (!$net->{ip}) { + $net->{ip} = PVE::Network::SDN::Vnets::get_next_free_cidr($bridge, $hostname, $description); + #writeconfig ? + } elsif ($net->{ip} ne 'dhcp' && $net->{ip} ne 'manual') { + PVE::Network::SDN::Vnets::add_cidr($bridge, $net->{ip}, $hostname, $description) if $net->{ip} ne $oldnet->{ip}; + #writeconfig ? + } + }; + if ($@) { + #empty ip if error + $net->{ip} = ''; + die $@; + } + + #delete old ip6 + if ($oldnet->{ip6} && $oldnet->{ip6} ne 'dhcp' && $oldnet->{ip6} ne 'manual' && $net->{ip6} ne 'auto') { + my $deletebridge = $oldbridge ne $bridge ? $oldbridge : $bridge; + eval { + PVE::Network::SDN::Vnets::del_cidr($deletebridge, $oldnet->{ip6}, $hostname) if !$net->{ip6} || $net->{ip6} ne $oldnet->{ip6}; + #writeconfig ? + }; + warn $@ if $@; + } + + eval { + if (!$net->{ip6}) { + + $net->{ip6} = PVE::Network::SDN::Vnets::get_next_free_cidr($bridge, $hostname, $description, 6); + #writeconfig ? + } elsif ($net->{ip6} ne 'dhcp' && $net->{ip6} ne 'manual' && $net->{ip6} ne 'auto') { + PVE::Network::SDN::Vnets::add_cidr($bridge, $net->{ip6}, $hostname, $description) if $net->{ip6} ne $oldnet->{ip6}; + #writeconfig ? + } + }; + if ($@) { + #empty ip if error + $net->{ip6} = ''; + die $@; + } + + if ($net->{ip}) { + my ($ip, $mask) = split(/\//, $net->{ip}); + my ($subnetidv4, $subnetv4) = PVE::Network::SDN::Subnets::find_ip_subnet($ip, $mask, $subnets); + $net->{gw} = $subnetv4->{gateway} if $subnetv4->{gateway}; + } + + if ($net->{ip6}) { + my ($ip6, $mask6) = split(/\//, $net->{ip6}); + my ($subnetidv6, $subnetv6) = PVE::Network::SDN::Subnets::find_ip_subnet($ip6, $mask6, $subnets); + $net->{gw6} = $subnetv6->{gateway} if $subnetv6->{gateway}; + } + +} + +sub delete_net_ip { + my ($hostname, $net) = @_; + + return if !$have_sdn; + + my $bridge = $net->{bridge}; + my $subnets = PVE::Network::SDN::Vnets::get_subnets($bridge); + return if !keys %{$subnets}; + + if ($net->{ip6} && ($net->{ip6} eq 'auto' || $net->{ip6} eq 'manual' || $net->{ip6} eq 'dhcp')) { + PVE::Network::SDN::Vnets::del_cidr($hostname, $bridge, $net->{ip6}); + } elsif ($net->{ip} && $net->{ip} ne 'dhcp' && $net->{ip} ne 'manual') { + PVE::Network::SDN::Vnets::del_cidr($hostname, $bridge, $net->{ip}); + } +} + 1; -- 2.20.1