From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pve-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9])
	by lore.proxmox.com (Postfix) with ESMTPS id 2EB1B1FF163
	for <inbox@lore.proxmox.com>; Thu,  5 Dec 2024 17:34:05 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 86E961EA4E;
	Thu,  5 Dec 2024 17:34:00 +0100 (CET)
From: Daniel Herzig <d.herzig@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Thu,  5 Dec 2024 17:33:30 +0100
Message-Id: <20241205163332.130930-3-d.herzig@proxmox.com>
X-Mailer: git-send-email 2.39.5
In-Reply-To: <20241205163332.130930-1-d.herzig@proxmox.com>
References: <20241205163332.130930-1-d.herzig@proxmox.com>
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.052 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pve-devel] [PATCH network 2/4] fix #5900: add helper functions
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pve-devel-bounces@lists.proxmox.com
Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com>

This patch adds helper functions to evaluate if a vnet (bridge) associated
with a zone under SDN's auto-dhcp control via dnsmasq can retrieve a
dhcp lease.

Signed-off-by: Daniel Herzig <d.herzig@proxmox.com>
---
 src/PVE/Network/SDN/Dhcp.pm | 83 +++++++++++++++++++++++++++++++++++++
 1 file changed, 83 insertions(+)

diff --git a/src/PVE/Network/SDN/Dhcp.pm b/src/PVE/Network/SDN/Dhcp.pm
index d48de34..4ddd128 100644
--- a/src/PVE/Network/SDN/Dhcp.pm
+++ b/src/PVE/Network/SDN/Dhcp.pm
@@ -128,4 +128,87 @@ sub regenerate_config {
     }
 }
 
+sub defined_dhcp_ip_count_in_zone {
+    my $zone_id = shift;
+    my $vnets_in_zone = PVE::Network::SDN::Zones::get_vnets($zone_id);
+    my $range_count_array;
+    my $res;
+    for my $vnet_id (keys %$vnets_in_zone) {
+	my $subnets_in_vnet = PVE::Network::SDN::Vnets::get_subnets($vnet_id);
+	for my $subnet (keys %$subnets_in_vnet) {
+	    my $dhcp_ranges = PVE::Network::SDN::Subnets::get_dhcp_ranges(${subnets_in_vnet}->{$subnet});
+	    if (scalar @$dhcp_ranges) {
+		for my $dhcp_range (@$dhcp_ranges) {
+		    my $start_ip = ${dhcp_range}->{'start-address'};
+		    my $end_ip = ${dhcp_range}->{'end-address'};
+		    my $subnet_ip_count = new Net::IP("$start_ip - $end_ip")->size();
+		    push (@$range_count_array, $subnet_ip_count);
+		}
+	    }
+	}
+    }
+    if ($range_count_array) {
+	$res = eval join '+', @$range_count_array;
+    }
+    return $res;
+}
+
+sub used_dhcp_ips_in_zone {
+    my $zone_id = shift;
+    my $pve_ipam_db = PVE::Network::SDN::Ipams::PVEPlugin::read_db();
+    my $subnets_in_zone = $pve_ipam_db->{'zones'}->{$zone_id}->{'subnets'};
+    my $res;
+    for my $subnet_in_zone (keys %$subnets_in_zone) {
+	my $ips_in_subnet = ${subnets_in_zone}->{$subnet_in_zone}->{'ips'};
+	if (scalar (keys %$ips_in_subnet)) {
+	    for my $leased_ip (keys %$ips_in_subnet) {
+		$res++ if (!exists ${ips_in_subnet}->{$leased_ip}->{'gateway'});
+	    }
+	}
+    }
+    return $res;
+}
+
+sub available_dhcp_ips_in_zone {
+    my $zone_id = shift;
+    my $available_ip_count = defined_dhcp_ip_count_in_zone($zone_id);
+    my $used_ip_count = used_dhcp_ips_in_zone($zone_id);
+    if (!defined($available_ip_count)) {
+	$available_ip_count = 0;
+    }
+    if (!defined($used_ip_count)) {
+	$used_ip_count = 0;
+    }
+    my $res = $available_ip_count - $used_ip_count;
+    return $res;
+}
+
+sub test_bridge_for_sdn_dnsmasq {
+    my $bridge = shift;
+    my $vnets_cfg = PVE::Network::SDN::Vnets::config();
+    my $vnet_ids = [ PVE::Network::SDN::Vnets::sdn_vnets_ids($vnets_cfg) ];
+    my $zones_cfg = PVE::Network::SDN::Zones::config();
+    my $zone_ids = [ PVE::Network::SDN::Zones::sdn_zones_ids($zones_cfg) ];
+    my $dhcp_dnsmasq_zones;
+    my $vnets_in_dhcp_dnsmasq_zones;
+    for my $zone (@$zone_ids) {
+	push(@$dhcp_dnsmasq_zones, $zone)
+	    if (defined(${zones_cfg}->{'ids'}->{$zone}->{'dhcp'}) &&
+		(${zones_cfg}->{'ids'}->{$zone}->{'dhcp'} eq 'dnsmasq'))
+    }
+    for my $vnet (@$vnet_ids) {
+	my $vnet_zone = ${vnets_cfg}->{'ids'}->{$vnet}->{'zone'};
+	push(@$vnets_in_dhcp_dnsmasq_zones, $vnet)
+	    if ("@$dhcp_dnsmasq_zones" =~ /$vnet_zone/)
+    }
+    if (("@$vnets_in_dhcp_dnsmasq_zones" =~ /$bridge/)) {
+	my $zone_id = ${vnets_cfg}->{'ids'}->{$bridge}->{'zone'};
+	if ((PVE::Network::SDN::Dhcp::available_dhcp_ips_in_zone($zone_id)) lt 1) {
+	    die "No DHCP leases left in zone '$zone_id' for bridge '$bridge', please check your SDN config.\n";
+	}
+    }
+}
+
+
+
 1;
-- 
2.39.5



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel