]> git.proxmox.com Git - pve-network.git/blobdiff - PVE/Network/SDN/Vnets.pm
controllers: bgp: add bgp-multipath-as-path-relax option
[pve-network.git] / PVE / Network / SDN / Vnets.pm
index 8cf5ac419c82e328f751376c2a5575c09648b442..caa6bfc16705414f5ba51ce314c3b5b344ec69c7 100644 (file)
@@ -6,6 +6,7 @@ use warnings;
 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;
 
@@ -51,9 +52,11 @@ sub complete_sdn_vnet {
 sub get_vnet {
     my ($vnetid, $running) = @_;
 
+    return if !$vnetid;
+
     my $cfg = {};
     if($running) {
-       my $cfg = PVE::Network::SDN::config();
+       my $cfg = PVE::Network::SDN::running_config();
        $cfg = $cfg->{vnets};
     } else {
        $cfg = PVE::Network::SDN::Vnets::config();
@@ -67,6 +70,8 @@ sub get_vnet {
 sub get_subnets {
     my ($vnetid) = @_;
 
+    return if !$vnetid;
+
     my $subnets = undef;
     my $subnets_cfg = PVE::Network::SDN::Subnets::config();
     foreach my $subnetid (sort keys %{$subnets_cfg->{ids}}) {
@@ -78,13 +83,31 @@ sub get_subnets {
 
 }
 
+sub get_subnet_from_vnet_cidr {
+    my ($vnetid, $cidr) = @_;
+
+    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);
+    die "ip address is not in cidr format" if !$mask;
+
+    my ($subnetid, $subnet) = PVE::Network::SDN::Subnets::find_ip_subnet($ip, $mask, $subnets);
+
+    return ($zone, $subnetid, $subnet, $ip);
+}
+
 sub get_next_free_cidr {
-    my ($vnetid, $hostname, $description, $ipversion) = @_;
+    my ($vnetid, $hostname, $mac, $description, $ipversion, $skipdns) = @_;
 
     my $vnet = PVE::Network::SDN::Vnets::get_vnet($vnetid);
     my $zoneid = $vnet->{zone};
     my $zone = PVE::Network::SDN::Zones::get_zone($zoneid);
 
+    return if !$zone->{ipam};
+
     $ipversion = 4 if !$ipversion;
     my $subnets = PVE::Network::SDN::Vnets::get_subnets($vnetid, 1);
     my $ip = undef;
@@ -96,12 +119,11 @@ sub get_next_free_cidr {
 
        next if $ipversion != Net::IP::ip_get_version($network);
        $subnetcount++;
-       if ($zone->{ipam}) {
-           eval {
-               $ip = PVE::Network::SDN::Subnets::next_free_ip($zone, $subnetid, $subnet, $hostname, $description);
-           };
-           warn $@ if $@;
-       }
+
+       eval {
+           $ip = PVE::Network::SDN::Subnets::next_free_ip($zone, $subnetid, $subnet, $hostname, $mac, $description, $skipdns);
+       };
+       warn $@ if $@;
        last if $ip;
     }
     die "can't find any free ip" if !$ip && $subnetcount > 0;
@@ -110,33 +132,32 @@ sub get_next_free_cidr {
 }
 
 sub add_cidr {
-    my ($vnetid, $cidr, $hostname, $description) = @_;
+    my ($vnetid, $cidr, $hostname, $mac, $description, $skipdns) = @_;
 
-    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);
+    return if !$vnetid;
+    
+    my ($zone, $subnetid, $subnet, $ip) = PVE::Network::SDN::Vnets::get_subnet_from_vnet_cidr($vnetid, $cidr);
+    PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $ip, $hostname, $mac, $description, undef, $skipdns);
+}
 
-    my ($ip, $mask) = split(/\//, $cidr);
-    die "ip address is not in cidr format" if !$mask;
-    my ($subnetid, $subnet) = PVE::Network::SDN::Subnets::find_ip_subnet($ip, $mask, $subnets);
+sub update_cidr {
+    my ($vnetid, $cidr, $hostname, $oldhostname, $mac, $description, $skipdns) = @_;
 
-    PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $ip, $hostname, $description);
+    return if !$vnetid;
+
+    my ($zone, $subnetid, $subnet, $ip) = PVE::Network::SDN::Vnets::get_subnet_from_vnet_cidr($vnetid, $cidr);
+    PVE::Network::SDN::Subnets::update_ip($zone, $subnetid, $subnet, $ip, $hostname, $oldhostname, $mac, $description, $skipdns);
 }
 
 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 ($vnetid, $cidr, $hostname, $skipdns) = @_;
 
-    my ($ip, $mask) = split(/\//, $cidr);
-    die "ip address is not in cidr format" if !$mask;
-    my ($subnetid, $subnet) = PVE::Network::SDN::Subnets::find_ip_subnet($ip, $mask, $subnets);
+    return if !$vnetid;
 
-    PVE::Network::SDN::Subnets::del_ip($zone, $subnetid, $subnet, $ip, $hostname);
+    my ($zone, $subnetid, $subnet, $ip) = PVE::Network::SDN::Vnets::get_subnet_from_vnet_cidr($vnetid, $cidr);
+    PVE::Network::SDN::Subnets::del_ip($zone, $subnetid, $subnet, $ip, $hostname, $skipdns);
 }
 
+
+
 1;