X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=PVE%2FNetwork%2FSDN%2FVnets.pm;h=caa6bfc16705414f5ba51ce314c3b5b344ec69c7;hb=1262519c3de7ca0dabbdc9a988e61b76328862fa;hp=6ea3a9a5cf9c86f7959250da0127283d79cfc118;hpb=70b035064290a014759ce62e0093df00cd7d62fe;p=pve-network.git diff --git a/PVE/Network/SDN/Vnets.pm b/PVE/Network/SDN/Vnets.pm index 6ea3a9a..caa6bfc 100644 --- a/PVE/Network/SDN/Vnets.pm +++ b/PVE/Network/SDN/Vnets.pm @@ -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,34 +50,80 @@ sub complete_sdn_vnet { } sub get_vnet { - my ($vnetid) = @_; + my ($vnetid, $running) = @_; + + return if !$vnetid; + + my $cfg = {}; + if($running) { + my $cfg = PVE::Network::SDN::running_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, $ipversion) = @_; +sub get_subnets { + my ($vnetid) = @_; - $ipversion = 4 if !$ipversion; + return if !$vnetid; + + 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_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, $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; - 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}) { - eval { - $ip = PVE::Network::SDN::Subnets::next_free_ip($subnetid, $subnet); - }; - 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; @@ -82,24 +131,33 @@ sub get_next_free_ip { return $ip; } -sub add_ip { - my ($vnet, $cidr, $name) = @_; +sub add_cidr { + my ($vnetid, $cidr, $hostname, $mac, $description, $skipdns) = @_; - my ($ip, $mask) = split(/\//, $cidr); - my ($subnetid, $subnet) = PVE::Network::SDN::Subnets::find_ip_subnet($ip, $vnet->{subnets}); - return if !$subnet->{ipam}; + 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); +} + +sub update_cidr { + my ($vnetid, $cidr, $hostname, $oldhostname, $mac, $description, $skipdns) = @_; - PVE::Network::SDN::Subnets::add_ip($subnetid, $subnet, $ip); + 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_ip { - my ($vnet, $cidr) = @_; +sub del_cidr { + my ($vnetid, $cidr, $hostname, $skipdns) = @_; - my ($ip, $mask) = split(/\//, $cidr); - my ($subnetid, $subnet) = PVE::Network::SDN::Subnets::find_ip_subnet($ip, $vnet->{subnets}); - return if !$subnet->{ipam}; + return if !$vnetid; - PVE::Network::SDN::Subnets::del_ip($subnetid, $subnet, $ip); + 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;