]> git.proxmox.com Git - pve-network.git/blobdiff - PVE/Network/SDN/Vnets.pm
Fix vnet gateway for routed setup + /32 pointopoint subnet
[pve-network.git] / PVE / Network / SDN / Vnets.pm
index 073ab80301a4103fc17c50449afa142cb2458b25..7cec4180d9c7483e80cc4d9983ab18748e0c2f39 100644 (file)
@@ -4,6 +4,8 @@ use strict;
 use warnings;
 
 use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file);
+use Net::IP;
+use PVE::Network::SDN::Subnets;
 
 use PVE::Network::SDN::VnetPlugin;
 PVE::Network::SDN::VnetPlugin->register();
@@ -52,4 +54,67 @@ sub get_vnet {
     return $vnet;
 }
 
+sub get_subnets {
+    my ($vnetid) = @_;
+
+    my $subnets = {};
+    my $subnets_cfg = PVE::Network::SDN::Subnets::config();
+    foreach my $subnetid (sort keys %{$subnets_cfg->{ids}}) {
+       my $subnet = $subnets_cfg->{ids}->{$subnetid};
+       next if !$subnet->{vnet} || $subnet->{vnet} ne $vnetid;
+       $subnets->{$subnetid} = $subnet;
+    }
+    return $subnets;
+
+}
+
+sub get_next_free_ip {
+    my ($vnetid, $hostname, $ipversion) = @_;
+
+    $ipversion = 4 if !$ipversion;
+    my $subnets = PVE::Network::SDN::Vnets::get_subnets($vnetid);
+    my $ip = undef;
+    my $subnetcount = 0;
+
+    foreach my $subnetid (sort keys %{$subnets}) {
+        my $subnet = $subnets->{$subnetid};
+       my ($network, $mask) = split(/-/, $subnetid);
+
+       next if $ipversion != Net::IP::ip_get_version($network);
+       $subnetcount++;
+       if ($subnet->{ipam}) {
+           eval {
+               $ip = PVE::Network::SDN::Subnets::next_free_ip($subnetid, $subnet, $hostname);
+           };
+           warn $@ if $@;
+       }
+       last if $ip;
+    }
+    die "can't find any free ip" if !$ip && $subnetcount > 0;
+
+    return $ip;
+}
+
+sub add_ip {
+    my ($vnetid, $cidr, $hostname) = @_;
+
+    my $subnets = PVE::Network::SDN::Vnets::get_subnets($vnetid);
+
+    my ($ip, $mask) = split(/\//, $cidr);
+    my ($subnetid, $subnet) = PVE::Network::SDN::Subnets::find_ip_subnet($ip, $subnets);
+
+    PVE::Network::SDN::Subnets::add_ip($subnetid, $subnet, $ip, $hostname);
+}
+
+sub del_ip {
+    my ($vnetid, $cidr, $hostname) = @_;
+
+    my $subnets = PVE::Network::SDN::Vnets::get_subnets($vnetid);
+
+    my ($ip, $mask) = split(/\//, $cidr);
+    my ($subnetid, $subnet) = PVE::Network::SDN::Subnets::find_ip_subnet($ip, $subnets);
+
+    PVE::Network::SDN::Subnets::del_ip($subnetid, $subnet, $ip, $hostname);
+}
+
 1;