]> 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 d45ef2a8435a748a90118c681bffe95f95e2345a..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 {
@@ -65,10 +68,10 @@ sub get_vnet {
 sub get_subnets {
     my ($vnetid) = @_;
 
-    my $subnets = {};
+    my $subnets = undef;
     my $subnets_cfg = PVE::Network::SDN::Subnets::config();
     foreach my $subnetid (sort keys %{$subnets_cfg->{ids}}) {
-       my $subnet = $subnets_cfg->{ids}->{$subnetid};
+       my $subnet = PVE::Network::SDN::Subnets::sdn_subnets_config($subnets_cfg, $subnetid);
        next if !$subnet->{vnet} || $subnet->{vnet} ne $vnetid;
        $subnets->{$subnetid} = $subnet;
     }
@@ -76,8 +79,12 @@ sub get_subnets {
 
 }
 
-sub get_next_free_ip {
-    my ($vnetid, $hostname, $ipversion) = @_;
+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);
@@ -86,13 +93,13 @@ sub get_next_free_ip {
 
     foreach my $subnetid (sort keys %{$subnets}) {
         my $subnet = $subnets->{$subnetid};
-       my ($network, $mask) = split(/-/, $subnetid);
+       my $network = $subnet->{network};
 
        next if $ipversion != Net::IP::ip_get_version($network);
        $subnetcount++;
-       if ($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 $@;
        }
@@ -103,26 +110,34 @@ sub get_next_free_ip {
     return $ip;
 }
 
-sub add_ip {
-    my ($vnetid, $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, $subnets);
+    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 {
+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, $subnets);
+    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;