public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Stefan Lendl <s.lendl@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH pve-network 1/3] sdn: register MAC in IPAM if not found
Date: Tue, 21 Nov 2023 15:55:54 +0100	[thread overview]
Message-ID: <20231121145556.436743-2-s.lendl@proxmox.com> (raw)
In-Reply-To: <20231121145556.436743-1-s.lendl@proxmox.com>

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





  reply	other threads:[~2023-11-21 14:56 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231121145556.436743-2-s.lendl@proxmox.com \
    --to=s.lendl@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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