[pve-devel] [PATCH pve-network 1/3] sdn: register MAC in IPAM if not found

Stefan Lendl s.lendl at proxmox.com
Tue Nov 21 15:55:54 CET 2023


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 at 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






More information about the pve-devel mailing list