]>
git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/Subnets.pm
1 package PVE
::Network
::SDN
::Subnets
;
6 use Net
::Subnet
qw(subnet_matcher);
7 use PVE
::Cluster
qw(cfs_read_file cfs_write_file cfs_lock_file);
10 use PVE
::Network
::SDN
::Ipams
;
11 use PVE
::Network
::SDN
::Dns
;
12 use PVE
::Network
::SDN
::SubnetPlugin
;
13 PVE
::Network
::SDN
::SubnetPlugin-
>register();
14 PVE
::Network
::SDN
::SubnetPlugin-
>init();
16 sub sdn_subnets_config
{
17 my ($cfg, $id, $noerr) = @_;
19 die "no sdn subnet ID specified\n" if !$id;
21 my $scfg = $cfg->{ids
}->{$id};
22 die "sdn subnet '$id' does not exist\n" if (!$noerr && !$scfg);
28 my $config = cfs_read_file
("sdn/subnets.cfg");
34 cfs_write_file
("sdn/subnets.cfg", $cfg);
40 return keys %{$cfg->{ids
}};
43 sub complete_sdn_subnet
{
44 my ($cmdname, $pname, $cvalue) = @_;
46 my $cfg = PVE
::Network
::SDN
::Subnets
::config
();
48 return $cmdname eq 'add' ?
[] : [ PVE
::Network
::SDN
::Subnets
::sdn_subnets_ids
($cfg) ];
52 my ($subnetid, $running) = @_;
56 my $cfg = PVE
::Network
::SDN
::config
();
57 $cfg = $cfg->{subnets
};
59 $cfg = PVE
::Network
::SDN
::Subnets
::config
();
62 my $subnet = PVE
::Network
::SDN
::Subnets
::sdn_subnets_config
($cfg, $subnetid, 1);
67 my ($ip, $subnets) = @_;
72 foreach my $id (sort keys %{$subnets}) {
73 my $cidr = $id =~ s/-/\//r
;
74 my $subnet_matcher = subnet_matcher
($cidr);
75 next if !$subnet_matcher->($ip);
76 $subnet = $subnets->{$id};
80 die "can't find any subnet for ip $ip" if !$subnet;
82 return ($subnetid, $subnet);
85 my $verify_dns_zone = sub {
86 my ($zone, $dns) = @_;
88 return if !$zone || !$dns;
90 my $dns_cfg = PVE
::Network
::SDN
::Dns
::config
();
91 my $plugin_config = $dns_cfg->{ids
}->{$dns};
92 my $plugin = PVE
::Network
::SDN
::Dns
::Plugin-
>lookup($plugin_config->{type
});
93 $plugin->verify_zone($plugin_config, $zone);
96 my $get_reversedns_zone = sub {
97 my ($subnetid, $dns, $ip) = @_;
99 return if !$subnetid || !$dns || !$ip;
101 my $dns_cfg = PVE
::Network
::SDN
::Dns
::config
();
102 my $plugin_config = $dns_cfg->{ids
}->{$dns};
103 my $plugin = PVE
::Network
::SDN
::Dns
::Plugin-
>lookup($plugin_config->{type
});
104 $plugin->get_reversedns_zone($plugin_config, $subnetid, $ip);
107 my $add_dns_record = sub {
108 my ($zone, $dns, $hostname, $dnszoneprefix, $ip) = @_;
109 return if !$zone || !$dns || !$hostname || !$ip;
111 $hostname .= ".$dnszoneprefix" if $dnszoneprefix;
113 my $dns_cfg = PVE
::Network
::SDN
::Dns
::config
();
114 my $plugin_config = $dns_cfg->{ids
}->{$dns};
115 my $plugin = PVE
::Network
::SDN
::Dns
::Plugin-
>lookup($plugin_config->{type
});
116 $plugin->add_a_record($plugin_config, $zone, $hostname, $ip);
120 my $add_dns_ptr_record = sub {
121 my ($reversezone, $zone, $dns, $hostname, $dnszoneprefix, $ip) = @_;
123 return if !$zone || !$reversezone || !$dns || !$hostname || !$ip;
125 $hostname .= ".$dnszoneprefix" if $dnszoneprefix;
126 $hostname .= ".$zone";
127 my $dns_cfg = PVE
::Network
::SDN
::Dns
::config
();
128 my $plugin_config = $dns_cfg->{ids
}->{$dns};
129 my $plugin = PVE
::Network
::SDN
::Dns
::Plugin-
>lookup($plugin_config->{type
});
130 $plugin->add_ptr_record($plugin_config, $reversezone, $hostname, $ip);
133 my $del_dns_record = sub {
134 my ($zone, $dns, $hostname, $dnszoneprefix, $ip) = @_;
136 return if !$zone || !$dns || !$hostname || !$ip;
138 $hostname .= ".$dnszoneprefix" if $dnszoneprefix;
140 my $dns_cfg = PVE
::Network
::SDN
::Dns
::config
();
141 my $plugin_config = $dns_cfg->{ids
}->{$dns};
142 my $plugin = PVE
::Network
::SDN
::Dns
::Plugin-
>lookup($plugin_config->{type
});
143 $plugin->del_a_record($plugin_config, $zone, $hostname, $ip);
146 my $del_dns_ptr_record = sub {
147 my ($reversezone, $dns, $ip) = @_;
149 return if !$reversezone || !$dns || !$ip;
151 my $dns_cfg = PVE
::Network
::SDN
::Dns
::config
();
152 my $plugin_config = $dns_cfg->{ids
}->{$dns};
153 my $plugin = PVE
::Network
::SDN
::Dns
::Plugin-
>lookup($plugin_config->{type
});
154 $plugin->del_ptr_record($plugin_config, $reversezone, $ip);
158 my ($zone, $subnetid, $subnet, $hostname) = @_;
163 my $ipamid = $subnet->{ipam
};
164 my $dns = $zone->{dns
};
165 my $dnszone = $zone->{dnszone
};
166 my $reversedns = $zone->{reversedns
};
167 my $dnszoneprefix = $subnet->{dnszoneprefix
};
169 #verify dns zones before ipam
170 &$verify_dns_zone($dnszone, $dns);
173 my $ipam_cfg = PVE
::Network
::SDN
::Ipams
::config
();
174 my $plugin_config = $ipam_cfg->{ids
}->{$ipamid};
175 my $plugin = PVE
::Network
::SDN
::Ipams
::Plugin-
>lookup($plugin_config->{type
});
177 $cidr = $plugin->add_next_freeip($plugin_config, $subnetid, $subnet);
178 ($ip, undef) = split(/\//, $cidr);
184 my $reversednszone = &$get_reversedns_zone($subnetid, $reversedns, $ip);
187 &$add_dns_record($dnszone, $dns, $hostname, $dnszoneprefix, $ip);
189 &$add_dns_ptr_record($reversednszone, $dnszone, $reversedns, $hostname, $dnszoneprefix, $ip);
195 PVE
::Network
::SDN
::Subnets
::del_ip
($subnetid, $subnet, $ip, $hostname)
203 my ($zone, $subnetid, $subnet, $ip, $hostname) = @_;
207 my $ipamid = $subnet->{ipam
};
208 my $dns = $zone->{dns
};
209 my $dnszone = $zone->{dnszone
};
210 my $reversedns = $zone->{reversedns
};
211 my $reversednszone = &$get_reversedns_zone($subnetid, $reversedns, $ip);
212 my $dnszoneprefix = $subnet->{dnszoneprefix
};
214 #verify dns zones before ipam
215 &$verify_dns_zone($dnszone, $dns);
216 &$verify_dns_zone($reversednszone, $reversedns);
219 my $ipam_cfg = PVE
::Network
::SDN
::Ipams
::config
();
220 my $plugin_config = $ipam_cfg->{ids
}->{$ipamid};
221 my $plugin = PVE
::Network
::SDN
::Ipams
::Plugin-
>lookup($plugin_config->{type
});
223 $plugin->add_ip($plugin_config, $subnetid, $ip);
230 &$add_dns_record($dnszone, $dns, $hostname, $dnszoneprefix, $ip);
232 &$add_dns_ptr_record($reversednszone, $dnszone, $reversedns, $hostname, $dnszoneprefix, $ip);
238 PVE
::Network
::SDN
::Subnets
::del_ip
($subnetid, $subnet, $ip, $hostname)
245 my ($zone, $subnetid, $subnet, $ip, $hostname) = @_;
249 my $ipamid = $subnet->{ipam
};
250 my $dns = $zone->{dns
};
251 my $dnszone = $zone->{dnszone
};
252 my $reversedns = $zone->{reversedns
};
253 my $reversednszone = &$get_reversedns_zone($subnetid, $reversedns, $ip);
254 my $dnszoneprefix = $subnet->{dnszoneprefix
};
256 &$verify_dns_zone($dnszone, $dns);
257 &$verify_dns_zone($reversednszone, $reversedns);
260 my $ipam_cfg = PVE
::Network
::SDN
::Ipams
::config
();
261 my $plugin_config = $ipam_cfg->{ids
}->{$ipamid};
262 my $plugin = PVE
::Network
::SDN
::Ipams
::Plugin-
>lookup($plugin_config->{type
});
263 $plugin->del_ip($plugin_config, $subnetid, $ip);
267 &$del_dns_record($dnszone, $dns, $hostname, $dnszoneprefix, $ip);
268 &$del_dns_ptr_record($reversednszone, $reversedns, $ip);