]>
git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/Vnets.pm
1 package PVE
::Network
::SDN
::Vnets
;
6 use PVE
::Cluster
qw(cfs_read_file cfs_write_file cfs_lock_file);
8 use PVE
::Network
::SDN
::Subnets
;
10 use PVE
::Network
::SDN
::VnetPlugin
;
11 PVE
::Network
::SDN
::VnetPlugin-
>register();
12 PVE
::Network
::SDN
::VnetPlugin-
>init();
14 sub sdn_vnets_config
{
15 my ($cfg, $id, $noerr) = @_;
17 die "no sdn vnet ID specified\n" if !$id;
19 my $scfg = $cfg->{ids
}->{$id};
20 die "sdn vnet '$id' does not exist\n" if (!$noerr && !$scfg);
26 return cfs_read_file
("sdn/vnets.cfg");
32 cfs_write_file
("sdn/vnets.cfg", $cfg);
38 return keys %{$cfg->{ids
}};
41 sub complete_sdn_vnet
{
42 my ($cmdname, $pname, $cvalue) = @_;
44 my $cfg = PVE
::Network
::SDN
::Vnets
::config
();
46 return $cmdname eq 'add' ?
[] : [ PVE
::Network
::SDN
::Vnets
::sdn_vnet_ids
($cfg) ];
52 my $cfg = PVE
::Network
::SDN
::Vnets
::config
();
53 my $vnet = PVE
::Network
::SDN
::Vnets
::sdn_vnets_config
($cfg, $vnetid, 1);
57 sub get_next_free_ip
{
58 my ($vnet, $hostname, $ipversion) = @_;
60 $ipversion = 4 if !$ipversion;
61 my $subnets_cfg = PVE
::Network
::SDN
::Subnets
::config
();
62 my @subnets = PVE
::Tools
::split_list
($vnet->{subnets
}) if $vnet->{subnets
};
66 foreach my $s (@subnets) {
67 my $subnetid = $s =~ s/\//-/r
;
68 my ($network, $mask) = split(/-/, $subnetid);
69 next if $ipversion != Net
::IP
::ip_get_version
($network);
71 $subnet = $subnets_cfg->{ids
}->{$subnetid};
72 if ($subnet && $subnet->{ipam
}) {
74 $ip = PVE
::Network
::SDN
::Subnets
::next_free_ip
($subnetid, $subnet, $hostname);
80 die "can't find any free ip" if !$ip && $subnetcount > 0;
86 my ($vnet, $cidr, $hostname) = @_;
88 my ($ip, $mask) = split(/\//, $cidr);
89 my ($subnetid, $subnet) = PVE
::Network
::SDN
::Subnets
::find_ip_subnet
($ip, $vnet->{subnets
});
90 return if !$subnet->{ipam
};
92 PVE
::Network
::SDN
::Subnets
::add_ip
($subnetid, $subnet, $ip, $hostname);
96 my ($vnet, $cidr, $hostname) = @_;
98 my ($ip, $mask) = split(/\//, $cidr);
99 my ($subnetid, $subnet) = PVE
::Network
::SDN
::Subnets
::find_ip_subnet
($ip, $vnet->{subnets
});
100 return if !$subnet->{ipam
};
102 PVE
::Network
::SDN
::Subnets
::del_ip
($subnetid, $subnet, $ip, $hostname);