From: Daniel Herzig <d.herzig@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH network 2/4] fix #5900: add helper functions
Date: Thu, 5 Dec 2024 17:33:30 +0100 [thread overview]
Message-ID: <20241205163332.130930-3-d.herzig@proxmox.com> (raw)
In-Reply-To: <20241205163332.130930-1-d.herzig@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
next prev parent reply other threads:[~2024-12-05 16:34 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-05 16:33 [pve-devel] [PATCH guest-common, network, container, qemu-server 0/4] fix #5900: enhance SDN auto-dhcp behaviour Daniel Herzig
2024-12-05 16:33 ` [pve-devel] [PATCH guest-common 1/4] fix #5900: add helper function Daniel Herzig
2024-12-12 17:12 ` Stefan Hanreich
2024-12-16 8:38 ` Daniel Herzig
2024-12-05 16:33 ` Daniel Herzig [this message]
2024-12-12 17:06 ` [pve-devel] [PATCH network 2/4] fix #5900: add helper functions Stefan Hanreich
2024-12-16 8:35 ` Daniel Herzig
2024-12-05 16:33 ` [pve-devel] [PATCH container 3/4] fix #5900: do not create container if dhcp range is exhausted Daniel Herzig
2024-12-05 16:33 ` [pve-devel] [PATCH qemu-server 4/4] fix #5900: do not create vm " Daniel Herzig
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=20241205163332.130930-3-d.herzig@proxmox.com \
--to=d.herzig@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