public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pve-devel] [PATCH network/container/qemu-server 0/3] sdn: register a new IP at VM start if missing
@ 2023-11-21 14:55 Stefan Lendl
  2023-11-21 14:55 ` [pve-devel] [PATCH pve-network 1/3] sdn: register MAC in IPAM if not found Stefan Lendl
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Stefan Lendl @ 2023-11-21 14:55 UTC (permalink / raw)
  To: pve-devel


If a VM or LXC starts and no IP was found in the IPAM, register a new one.

This is very seful as a fallback if for some reason an IP mapping was deleted
or there is a bug somewhere that does not register an IP.

This acts more like DHCP to allocate an IP on demand.

Special case that is not covered:
  If a subnet with IPv4 exists and VMs are present, adding a new subnet with
  IPv6, this will not register an IPv6. And vice-versa.



pve-network:

Stefan Lendl (1):
  sdn: register MAC in IPAM if not found

 src/PVE/Network/SDN/Vnets.pm | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)


pve-container:

Stefan Lendl (1):
  sdn: pass vmid and hostname to add_dhcp_mapping

 src/PVE/LXC.pm            | 4 ++--
 src/lxc-pve-prestart-hook | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)


qemu-server:

Stefan Lendl (1):
  sdn: pass vmid and hostname to add_dhcp_mapping

 PVE/QemuServer.pm             | 2 +-
 vm-network-scripts/pve-bridge | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)


Summary over all repositories:
  5 files changed, 14 insertions(+), 8 deletions(-)

-- 
murpp v0.4.0





^ permalink raw reply	[flat|nested] 6+ messages in thread

* [pve-devel] [PATCH pve-network 1/3] sdn: register MAC in IPAM if not found
  2023-11-21 14:55 [pve-devel] [PATCH network/container/qemu-server 0/3] sdn: register a new IP at VM start if missing Stefan Lendl
@ 2023-11-21 14:55 ` Stefan Lendl
  2023-11-21 14:55 ` [pve-devel] [PATCH pve-container 2/3] sdn: pass vmid and hostname to add_dhcp_mapping Stefan Lendl
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Stefan Lendl @ 2023-11-21 14:55 UTC (permalink / raw)
  To: pve-devel

if inside add_dhcp_mapping, which is called at VM or LCX start, we do
not find an IP in IPAM, register the MAC.

This is very useful as a fallback if for some reason an IP mapping was
deleted or there is a bug somewhere that does not register an IP.

This acts more like DHCP to allocate an IP on demand.

In order to properly register the IP, the VMID and hostname is required
as a parameter.

Signed-off-by: Stefan Lendl <s.lendl@proxmox.com>
---
 src/PVE/Network/SDN/Vnets.pm | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/PVE/Network/SDN/Vnets.pm b/src/PVE/Network/SDN/Vnets.pm
index 09378ff..0dfdfd7 100644
--- a/src/PVE/Network/SDN/Vnets.pm
+++ b/src/PVE/Network/SDN/Vnets.pm
@@ -186,7 +186,7 @@ sub del_ips_from_mac {
 }
 
 sub add_dhcp_mapping {
-    my ($vnetid, $mac) = @_;
+    my ($vnetid, $mac, $vmid, $name) = @_;
 
     my $vnet = PVE::Network::SDN::Vnets::get_vnet($vnetid);
     return if !$vnet;
@@ -195,7 +195,13 @@ sub add_dhcp_mapping {
 
     return if !$zone->{ipam} || !$zone->{dhcp};
 
-    my ($ip4,$ip6) = PVE::Network::SDN::Vnets::get_ips_from_mac($vnetid, $mac);
+    my ($ip4, $ip6) = PVE::Network::SDN::Vnets::get_ips_from_mac($vnetid, $mac);
+    if ( ! ($ip4 || $ip6) ) {
+	print "No IP found for MAC: $mac for VMID:$vmid\n";
+	add_next_free_cidr($vnetid, $name, $mac, "$vmid", undef, 1);
+	($ip4, $ip6) = PVE::Network::SDN::Vnets::get_ips_from_mac($vnetid, $mac);
+	print "got new IP from IPAM: $ip4 $ip6\n";
+    }
     PVE::Network::SDN::Dhcp::add_mapping($vnetid, $mac, $ip4, $ip6) if $ip4 || $ip6;
 }
 
-- 
2.42.0





^ permalink raw reply	[flat|nested] 6+ messages in thread

* [pve-devel] [PATCH pve-container 2/3] sdn: pass vmid and hostname to add_dhcp_mapping
  2023-11-21 14:55 [pve-devel] [PATCH network/container/qemu-server 0/3] sdn: register a new IP at VM start if missing Stefan Lendl
  2023-11-21 14:55 ` [pve-devel] [PATCH pve-network 1/3] sdn: register MAC in IPAM if not found Stefan Lendl
@ 2023-11-21 14:55 ` Stefan Lendl
  2023-11-21 17:49   ` Stefan Hanreich
  2023-11-21 14:55 ` [pve-devel] [PATCH qemu-server 3/3] " Stefan Lendl
  2023-11-21 19:57 ` [pve-devel] applied-series: [PATCH network/container/qemu-server 0/3] sdn: register a new IP at VM start if missing Thomas Lamprecht
  3 siblings, 1 reply; 6+ messages in thread
From: Stefan Lendl @ 2023-11-21 14:55 UTC (permalink / raw)
  To: pve-devel

If no DHCP mapping was found in IPAM it will request a new IP.
In order to register an IPAM mapping it requires these values.

Signed-off-by: Stefan Lendl <s.lendl@proxmox.com>
---
 src/PVE/LXC.pm            | 4 ++--
 src/lxc-pve-prestart-hook | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index 9361823..7883cfb 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -993,7 +993,7 @@ sub update_net {
 		warn $@ if $@;
 
 		PVE::Network::SDN::Vnets::add_next_free_cidr($newnet->{bridge}, $conf->{hostname}, $newnet->{hwaddr}, $vmid, undef, 1);
-		PVE::Network::SDN::Vnets::add_dhcp_mapping($newnet->{bridge}, $newnet->{hwaddr});
+		PVE::Network::SDN::Vnets::add_dhcp_mapping($newnet->{bridge}, $newnet->{hwaddr}, $vmid, $conf->{hostname});
 	    }
 
 	    delete $conf->{$opt};
@@ -1046,7 +1046,7 @@ sub update_net {
     } else {
 	if ($have_sdn) {
 	    PVE::Network::SDN::Vnets::add_next_free_cidr($newnet->{bridge}, $conf->{hostname}, $newnet->{hwaddr}, $vmid, undef, 1);
-	    PVE::Network::SDN::Vnets::add_dhcp_mapping($newnet->{bridge}, $newnet->{hwaddr});
+	    PVE::Network::SDN::Vnets::add_dhcp_mapping($newnet->{bridge}, $newnet->{hwaddr}, $vmid, $conf->{hostname});
 	}
 
 	hotplug_net($vmid, $conf, $opt, $newnet, $netid);
diff --git a/src/lxc-pve-prestart-hook b/src/lxc-pve-prestart-hook
index ab35774..4f5c7e2 100755
--- a/src/lxc-pve-prestart-hook
+++ b/src/lxc-pve-prestart-hook
@@ -210,7 +210,7 @@ PVE::LXC::Tools::lxc_hook('pre-start', 'lxc', sub {
 	    next if $k !~ /^net(\d+)/;
 	    my $net = PVE::LXC::Config->parse_lxc_network($conf->{$k});
 	    next if $net->{type} ne 'veth';
-	    PVE::Network::SDN::Vnets::add_dhcp_mapping($net->{bridge}, $net->{hwaddr});
+	    PVE::Network::SDN::Vnets::add_dhcp_mapping($net->{bridge}, $net->{hwaddr}, $conf->{vmid}, $conf->{hostname});
 	}
     }
 });
-- 
2.42.0





^ permalink raw reply	[flat|nested] 6+ messages in thread

* [pve-devel] [PATCH qemu-server 3/3] sdn: pass vmid and hostname to add_dhcp_mapping
  2023-11-21 14:55 [pve-devel] [PATCH network/container/qemu-server 0/3] sdn: register a new IP at VM start if missing Stefan Lendl
  2023-11-21 14:55 ` [pve-devel] [PATCH pve-network 1/3] sdn: register MAC in IPAM if not found Stefan Lendl
  2023-11-21 14:55 ` [pve-devel] [PATCH pve-container 2/3] sdn: pass vmid and hostname to add_dhcp_mapping Stefan Lendl
@ 2023-11-21 14:55 ` Stefan Lendl
  2023-11-21 19:57 ` [pve-devel] applied-series: [PATCH network/container/qemu-server 0/3] sdn: register a new IP at VM start if missing Thomas Lamprecht
  3 siblings, 0 replies; 6+ messages in thread
From: Stefan Lendl @ 2023-11-21 14:55 UTC (permalink / raw)
  To: pve-devel

if no DHCP mapping was found in IPAM it will request a new IP which
requires these values.

Signed-off-by: Stefan Lendl <s.lendl@proxmox.com>
---
 PVE/QemuServer.pm             | 2 +-
 vm-network-scripts/pve-bridge | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 3028e70..978aac7 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -5381,7 +5381,7 @@ sub vmconfig_update_net {
     if ($hotplug) {
 	if ($have_sdn) {
 	    PVE::Network::SDN::Vnets::add_next_free_cidr($newnet->{bridge}, $conf->{name}, $newnet->{macaddr}, $vmid, undef, 1);
-	    PVE::Network::SDN::Vnets::add_dhcp_mapping($newnet->{bridge}, $newnet->{macaddr});
+	    PVE::Network::SDN::Vnets::add_dhcp_mapping($newnet->{bridge}, $newnet->{macaddr}, $vmid, $conf->{name});
 	}
 	vm_deviceplug($storecfg, $conf, $vmid, $opt, $newnet, $arch, $machine_type);
     } else {
diff --git a/vm-network-scripts/pve-bridge b/vm-network-scripts/pve-bridge
index e8f8798..85997a0 100755
--- a/vm-network-scripts/pve-bridge
+++ b/vm-network-scripts/pve-bridge
@@ -37,7 +37,7 @@ my $conf = PVE::QemuConfig->load_config($vmid, $migratedfrom);
 my $netconf = $conf->{$netid};
 
 $netconf = $conf->{pending}->{$netid} if !$migratedfrom && defined($conf->{pending}->{$netid}); 
- 
+
 die "unable to get network config '$netid'\n"
     if !defined($netconf);
 
@@ -45,7 +45,7 @@ my $net = PVE::QemuServer::parse_net($netconf);
 die "unable to parse network config '$netid'\n" if !$net;
 
 if ($have_sdn) {
-    PVE::Network::SDN::Vnets::add_dhcp_mapping($net->{bridge}, $net->{macaddr});
+    PVE::Network::SDN::Vnets::add_dhcp_mapping($net->{bridge}, $net->{macaddr}, $vmid, $conf->{name});
     PVE::Network::SDN::Zones::tap_create($iface, $net->{bridge});
     PVE::Network::SDN::Zones::tap_plug($iface, $net->{bridge}, $net->{tag}, $net->{firewall}, $net->{trunks}, $net->{rate});
 } else {
-- 
2.42.0





^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [pve-devel] [PATCH pve-container 2/3] sdn: pass vmid and hostname to add_dhcp_mapping
  2023-11-21 14:55 ` [pve-devel] [PATCH pve-container 2/3] sdn: pass vmid and hostname to add_dhcp_mapping Stefan Lendl
@ 2023-11-21 17:49   ` Stefan Hanreich
  0 siblings, 0 replies; 6+ messages in thread
From: Stefan Hanreich @ 2023-11-21 17:49 UTC (permalink / raw)
  To: Proxmox VE development discussion, Stefan Lendl

see below

On 11/21/23 15:55, Stefan Lendl wrote:
> If no DHCP mapping was found in IPAM it will request a new IP.
> In order to register an IPAM mapping it requires these values.
> 
> Signed-off-by: Stefan Lendl <s.lendl@proxmox.com>
> ---
>  src/PVE/LXC.pm            | 4 ++--
>  src/lxc-pve-prestart-hook | 2 +-
>  2 files changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
> index 9361823..7883cfb 100644
> --- a/src/PVE/LXC.pm
> +++ b/src/PVE/LXC.pm
> @@ -993,7 +993,7 @@ sub update_net {
>  		warn $@ if $@;
>  
>  		PVE::Network::SDN::Vnets::add_next_free_cidr($newnet->{bridge}, $conf->{hostname}, $newnet->{hwaddr}, $vmid, undef, 1);
> -		PVE::Network::SDN::Vnets::add_dhcp_mapping($newnet->{bridge}, $newnet->{hwaddr});
> +		PVE::Network::SDN::Vnets::add_dhcp_mapping($newnet->{bridge}, $newnet->{hwaddr}, $vmid, $conf->{hostname});
>  	    }
>  
>  	    delete $conf->{$opt};
> @@ -1046,7 +1046,7 @@ sub update_net {
>      } else {
>  	if ($have_sdn) {
>  	    PVE::Network::SDN::Vnets::add_next_free_cidr($newnet->{bridge}, $conf->{hostname}, $newnet->{hwaddr}, $vmid, undef, 1);
> -	    PVE::Network::SDN::Vnets::add_dhcp_mapping($newnet->{bridge}, $newnet->{hwaddr});
> +	    PVE::Network::SDN::Vnets::add_dhcp_mapping($newnet->{bridge}, $newnet->{hwaddr}, $vmid, $conf->{hostname});
>  	}
>  
>  	hotplug_net($vmid, $conf, $opt, $newnet, $netid);
> diff --git a/src/lxc-pve-prestart-hook b/src/lxc-pve-prestart-hook
> index ab35774..4f5c7e2 100755
> --- a/src/lxc-pve-prestart-hook
> +++ b/src/lxc-pve-prestart-hook
> @@ -210,7 +210,7 @@ PVE::LXC::Tools::lxc_hook('pre-start', 'lxc', sub {
>  	    next if $k !~ /^net(\d+)/;
>  	    my $net = PVE::LXC::Config->parse_lxc_network($conf->{$k});
>  	    next if $net->{type} ne 'veth';
> -	    PVE::Network::SDN::Vnets::add_dhcp_mapping($net->{bridge}, $net->{hwaddr});
> +	    PVE::Network::SDN::Vnets::add_dhcp_mapping($net->{bridge}, $net->{hwaddr}, $conf->{vmid}, $conf->{hostname});

The $conf->{vmid} variable does not exist, a simple $vmid would work
here. I've sent a patch correcting this issue.

Other than that consider this series

Tested-by: Stefan Hanreich <s.hanreich@proxmox.com>




^ permalink raw reply	[flat|nested] 6+ messages in thread

* [pve-devel] applied-series: [PATCH network/container/qemu-server 0/3] sdn: register a new IP at VM start if missing
  2023-11-21 14:55 [pve-devel] [PATCH network/container/qemu-server 0/3] sdn: register a new IP at VM start if missing Stefan Lendl
                   ` (2 preceding siblings ...)
  2023-11-21 14:55 ` [pve-devel] [PATCH qemu-server 3/3] " Stefan Lendl
@ 2023-11-21 19:57 ` Thomas Lamprecht
  3 siblings, 0 replies; 6+ messages in thread
From: Thomas Lamprecht @ 2023-11-21 19:57 UTC (permalink / raw)
  To: Proxmox VE development discussion, Stefan Lendl

Am 21/11/2023 um 15:55 schrieb Stefan Lendl:
> 
> If a VM or LXC starts and no IP was found in the IPAM, register a new one.
> 
> This is very seful as a fallback if for some reason an IP mapping was deleted
> or there is a bug somewhere that does not register an IP.
> 
> This acts more like DHCP to allocate an IP on demand.
> 
> Special case that is not covered:
>   If a subnet with IPv4 exists and VMs are present, adding a new subnet with
>   IPv6, this will not register an IPv6. And vice-versa.
> 
> 
> 
> pve-network:
> 
> Stefan Lendl (1):
>   sdn: register MAC in IPAM if not found
> 
>  src/PVE/Network/SDN/Vnets.pm | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> 
> pve-container:
> 
> Stefan Lendl (1):
>   sdn: pass vmid and hostname to add_dhcp_mapping
> 
>  src/PVE/LXC.pm            | 4 ++--
>  src/lxc-pve-prestart-hook | 2 +-
>  2 files changed, 3 insertions(+), 3 deletions(-)
> 
> 
> qemu-server:
> 
> Stefan Lendl (1):
>   sdn: pass vmid and hostname to add_dhcp_mapping
> 
>  PVE/QemuServer.pm             | 2 +-
>  vm-network-scripts/pve-bridge | 4 ++--
>  2 files changed, 3 insertions(+), 3 deletions(-)
> 
> 
> Summary over all repositories:
>   5 files changed, 14 insertions(+), 8 deletions(-)
> 


applied series, thanks!




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-11-21 19:57 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-21 14:55 [pve-devel] [PATCH network/container/qemu-server 0/3] sdn: register a new IP at VM start if missing Stefan Lendl
2023-11-21 14:55 ` [pve-devel] [PATCH pve-network 1/3] sdn: register MAC in IPAM if not found Stefan Lendl
2023-11-21 14:55 ` [pve-devel] [PATCH pve-container 2/3] sdn: pass vmid and hostname to add_dhcp_mapping Stefan Lendl
2023-11-21 17:49   ` Stefan Hanreich
2023-11-21 14:55 ` [pve-devel] [PATCH qemu-server 3/3] " Stefan Lendl
2023-11-21 19:57 ` [pve-devel] applied-series: [PATCH network/container/qemu-server 0/3] sdn: register a new IP at VM start if missing Thomas Lamprecht

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