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;
sub sdn_vnets_ids {
my ($cfg) = @_;
- return keys %{$cfg->{ids}};
+ return sort keys %{$cfg->{ids}};
}
sub complete_sdn_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;
}
}
-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};
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 ($zone->{ipam}) {
eval {
- $ip = PVE::Network::SDN::Subnets::next_free_ip($zone, $subnetid, $subnet, $hostname);
+ $ip = PVE::Network::SDN::Subnets::next_free_ip($zone, $subnetid, $subnet, $hostname, $mac, $description);
};
warn $@ if $@;
}
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 $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($zone, $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 $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($zone, $subnetid, $subnet, $ip, $hostname);
}