[pve-devel] [PATCH network 2/4] fix #5900: add helper functions
Daniel Herzig
d.herzig at proxmox.com
Thu Dec 5 17:33:30 CET 2024
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 at 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
More information about the pve-devel
mailing list