]> git.proxmox.com Git - pve-network.git/blobdiff - PVE/Network/SDN/Vnets.pm
ipams: add mac address
[pve-network.git] / PVE / Network / SDN / Vnets.pm
index c9916b1ca3938912b25b7bd29686612b3ace2230..d68ffab7d592ecbd01f242b58ae6492a9face104 100644 (file)
@@ -3,9 +3,12 @@ package PVE::Network::SDN::Vnets;
 use strict;
 use warnings;
 
-use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file);
 use Net::IP;
+
+use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file);
+use PVE::Network::SDN;
 use PVE::Network::SDN::Subnets;
+use PVE::Network::SDN::Zones;
 
 use PVE::Network::SDN::VnetPlugin;
 PVE::Network::SDN::VnetPlugin->register();
@@ -35,7 +38,7 @@ sub write_config {
 sub sdn_vnets_ids {
     my ($cfg) = @_;
 
-    return keys %{$cfg->{ids}};
+    return sort keys %{$cfg->{ids}};
 }
 
 sub complete_sdn_vnet {
@@ -47,31 +50,56 @@ sub complete_sdn_vnet {
 }
 
 sub get_vnet {
-    my ($vnetid) = @_;
+    my ($vnetid, $running) = @_;
+
+    my $cfg = {};
+    if($running) {
+       my $cfg = PVE::Network::SDN::config();
+       $cfg = $cfg->{vnets};
+    } else {
+       $cfg = PVE::Network::SDN::Vnets::config();
+    }
 
-    my $cfg = PVE::Network::SDN::Vnets::config();
     my $vnet = PVE::Network::SDN::Vnets::sdn_vnets_config($cfg, $vnetid, 1);
+
     return $vnet;
 }
 
-sub get_next_free_ip {
-    my ($vnet, $hostname, $ipversion) = @_;
+sub get_subnets {
+    my ($vnetid) = @_;
 
-    $ipversion = 4 if !$ipversion;
+    my $subnets = undef;
     my $subnets_cfg = PVE::Network::SDN::Subnets::config();
-    my @subnets = PVE::Tools::split_list($vnet->{subnets}) if $vnet->{subnets};
+    foreach my $subnetid (sort keys %{$subnets_cfg->{ids}}) {
+       my $subnet = PVE::Network::SDN::Subnets::sdn_subnets_config($subnets_cfg, $subnetid);
+       next if !$subnet->{vnet} || $subnet->{vnet} ne $vnetid;
+       $subnets->{$subnetid} = $subnet;
+    }
+    return $subnets;
+
+}
+
+sub get_next_free_cidr {
+    my ($vnetid, $hostname, $mac, $description, $ipversion) = @_;
+
+    my $vnet = PVE::Network::SDN::Vnets::get_vnet($vnetid);
+    my $zoneid = $vnet->{zone};
+    my $zone = PVE::Network::SDN::Zones::get_zone($zoneid);
+
+    $ipversion = 4 if !$ipversion;
+    my $subnets = PVE::Network::SDN::Vnets::get_subnets($vnetid, 1);
     my $ip = undef;
-    my $subnet = undef;
     my $subnetcount = 0;
-    foreach my $s (@subnets) {
-       my $subnetid = $s =~ s/\//-/r;
-       my ($network, $mask) = split(/-/, $subnetid);
+
+    foreach my $subnetid (sort keys %{$subnets}) {
+        my $subnet = $subnets->{$subnetid};
+       my $network = $subnet->{network};
+
        next if $ipversion != Net::IP::ip_get_version($network);
        $subnetcount++;
-       $subnet = $subnets_cfg->{ids}->{$subnetid};
-       if ($subnet && $subnet->{ipam}) {
+       if ($zone->{ipam}) {
            eval {
-               $ip = PVE::Network::SDN::Subnets::next_free_ip($subnetid, $subnet, $hostname);
+               $ip = PVE::Network::SDN::Subnets::next_free_ip($zone, $subnetid, $subnet, $hostname, $mac, $description);
            };
            warn $@ if $@;
        }
@@ -82,24 +110,34 @@ sub get_next_free_ip {
     return $ip;
 }
 
-sub add_ip {
-    my ($vnet, $cidr, $hostname) = @_;
+sub add_cidr {
+    my ($vnetid, $cidr, $hostname, $mac, $description) = @_;
+
+    my $subnets = PVE::Network::SDN::Vnets::get_subnets($vnetid, 1);
+    my $vnet = PVE::Network::SDN::Vnets::get_vnet($vnetid);
+    my $zoneid = $vnet->{zone};
+    my $zone = PVE::Network::SDN::Zones::get_zone($zoneid);
 
     my ($ip, $mask) = split(/\//, $cidr);
-    my ($subnetid, $subnet) = PVE::Network::SDN::Subnets::find_ip_subnet($ip, $vnet->{subnets});
-    return if !$subnet->{ipam};
+    die "ip address is not in cidr format" if !$mask;
+    my ($subnetid, $subnet) = PVE::Network::SDN::Subnets::find_ip_subnet($ip, $mask, $subnets);
 
-    PVE::Network::SDN::Subnets::add_ip($subnetid, $subnet, $ip, $hostname);
+    PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $ip, $hostname, $mac, $description);
 }
 
-sub del_ip {
-    my ($vnet, $cidr, $hostname) = @_;
+sub del_cidr {
+    my ($vnetid, $cidr, $hostname) = @_;
+
+    my $subnets = PVE::Network::SDN::Vnets::get_subnets($vnetid, 1);
+    my $vnet = PVE::Network::SDN::Vnets::get_vnet($vnetid);
+    my $zoneid = $vnet->{zone};
+    my $zone = PVE::Network::SDN::Zones::get_zone($zoneid);
 
     my ($ip, $mask) = split(/\//, $cidr);
-    my ($subnetid, $subnet) = PVE::Network::SDN::Subnets::find_ip_subnet($ip, $vnet->{subnets});
-    return if !$subnet->{ipam};
+    die "ip address is not in cidr format" if !$mask;
+    my ($subnetid, $subnet) = PVE::Network::SDN::Subnets::find_ip_subnet($ip, $mask, $subnets);
 
-    PVE::Network::SDN::Subnets::del_ip($subnetid, $subnet, $ip, $hostname);
+    PVE::Network::SDN::Subnets::del_ip($zone, $subnetid, $subnet, $ip, $hostname);
 }
 
 1;