]>
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);
9 use PVE
::Cluster
qw(cfs_read_file cfs_write_file cfs_lock_file);
10 use PVE
::Network
::SDN
::Dns
;
11 use PVE
::Network
::SDN
::Ipams
;
13 use PVE
::Network
::SDN
::SubnetPlugin
;
14 PVE
::Network
::SDN
::SubnetPlugin-
>register();
15 PVE
::Network
::SDN
::SubnetPlugin-
>init();
17 sub sdn_subnets_config
{
18 my ($cfg, $id, $noerr) = @_;
20 die "no sdn subnet ID specified\n" if !$id;
22 my $scfg = $cfg->{ids
}->{$id};
23 die "sdn subnet '$id' does not exist\n" if (!$noerr && !$scfg);
26 my ($zone, $network, $mask) = split(/-/, $id);
27 $scfg->{cidr
} = "$network/$mask";
28 $scfg->{zone
} = $zone;
29 $scfg->{network
} = $network;
30 $scfg->{mask
} = $mask;
37 my $config = cfs_read_file
("sdn/subnets.cfg");
43 cfs_write_file
("sdn/subnets.cfg", $cfg);
49 return sort keys %{$cfg->{ids
}};
52 sub complete_sdn_subnet
{
53 my ($cmdname, $pname, $cvalue) = @_;
55 my $cfg = PVE
::Network
::SDN
::Subnets
::config
();
57 return $cmdname eq 'add' ?
[] : [ PVE
::Network
::SDN
::Subnets
::sdn_subnets_ids
($cfg) ];
61 my ($subnetid, $running) = @_;
65 my $cfg = PVE
::Network
::SDN
::config
();
66 $cfg = $cfg->{subnets
};
68 $cfg = PVE
::Network
::SDN
::Subnets
::config
();
71 my $subnet = PVE
::Network
::SDN
::Subnets
::sdn_subnets_config
($cfg, $subnetid, 1);
76 my ($ip, $mask, $subnets) = @_;
81 foreach my $id (sort keys %{$subnets}) {
83 next if $mask ne $subnets->{$id}->{mask
};
84 my $cidr = $subnets->{$id}->{cidr
};
85 my $subnet_matcher = subnet_matcher
($cidr);
86 next if !$subnet_matcher->($ip);
87 $subnet = $subnets->{$id};
91 die "can't find any subnet for ip $ip" if !$subnet;
93 return ($subnetid, $subnet);
96 my $verify_dns_zone = sub {
97 my ($zone, $dns) = @_;
99 return if !$zone || !$dns;
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->verify_zone($plugin_config, $zone);
107 my $get_reversedns_zone = sub {
108 my ($subnetid, $subnet, $dns, $ip) = @_;
110 return if !$subnetid || !$dns || !$ip;
112 my $dns_cfg = PVE
::Network
::SDN
::Dns
::config
();
113 my $plugin_config = $dns_cfg->{ids
}->{$dns};
114 my $plugin = PVE
::Network
::SDN
::Dns
::Plugin-
>lookup($plugin_config->{type
});
115 $plugin->get_reversedns_zone($plugin_config, $subnetid, $subnet, $ip);
118 my $add_dns_record = sub {
119 my ($zone, $dns, $hostname, $ip) = @_;
120 return if !$zone || !$dns || !$hostname || !$ip;
122 my $dns_cfg = PVE
::Network
::SDN
::Dns
::config
();
123 my $plugin_config = $dns_cfg->{ids
}->{$dns};
124 my $plugin = PVE
::Network
::SDN
::Dns
::Plugin-
>lookup($plugin_config->{type
});
125 $plugin->add_a_record($plugin_config, $zone, $hostname, $ip);
129 my $add_dns_ptr_record = sub {
130 my ($reversezone, $zone, $dns, $hostname, $ip) = @_;
132 return if !$zone || !$reversezone || !$dns || !$hostname || !$ip;
134 $hostname .= ".$zone";
135 my $dns_cfg = PVE
::Network
::SDN
::Dns
::config
();
136 my $plugin_config = $dns_cfg->{ids
}->{$dns};
137 my $plugin = PVE
::Network
::SDN
::Dns
::Plugin-
>lookup($plugin_config->{type
});
138 $plugin->add_ptr_record($plugin_config, $reversezone, $hostname, $ip);
141 my $del_dns_record = sub {
142 my ($zone, $dns, $hostname, $ip) = @_;
144 return if !$zone || !$dns || !$hostname || !$ip;
146 my $dns_cfg = PVE
::Network
::SDN
::Dns
::config
();
147 my $plugin_config = $dns_cfg->{ids
}->{$dns};
148 my $plugin = PVE
::Network
::SDN
::Dns
::Plugin-
>lookup($plugin_config->{type
});
149 $plugin->del_a_record($plugin_config, $zone, $hostname, $ip);
152 my $del_dns_ptr_record = sub {
153 my ($reversezone, $dns, $ip) = @_;
155 return if !$reversezone || !$dns || !$ip;
157 my $dns_cfg = PVE
::Network
::SDN
::Dns
::config
();
158 my $plugin_config = $dns_cfg->{ids
}->{$dns};
159 my $plugin = PVE
::Network
::SDN
::Dns
::Plugin-
>lookup($plugin_config->{type
});
160 $plugin->del_ptr_record($plugin_config, $reversezone, $ip);
164 my ($zone, $subnetid, $subnet, $hostname, $description) = @_;
168 $description = '' if !$description;
170 my $ipamid = $zone->{ipam
};
171 my $dns = $zone->{dns
};
172 my $dnszone = $zone->{dnszone
};
173 my $reversedns = $zone->{reversedns
};
174 my $dnszoneprefix = $subnet->{dnszoneprefix
};
176 $hostname .= ".$dnszoneprefix" if $dnszoneprefix;
178 #verify dns zones before ipam
179 &$verify_dns_zone($dnszone, $dns);
182 my $ipam_cfg = PVE
::Network
::SDN
::Ipams
::config
();
183 my $plugin_config = $ipam_cfg->{ids
}->{$ipamid};
184 my $plugin = PVE
::Network
::SDN
::Ipams
::Plugin-
>lookup($plugin_config->{type
});
186 $cidr = $plugin->add_next_freeip($plugin_config, $subnetid, $subnet, $hostname, $description);
187 ($ip, undef) = split(/\//, $cidr);
193 my $reversednszone = &$get_reversedns_zone($subnetid, $subnet, $reversedns, $ip);
196 &$add_dns_record($dnszone, $dns, $hostname, $ip);
198 &$add_dns_ptr_record($reversednszone, $dnszone, $reversedns, $hostname, $ip);
204 PVE
::Network
::SDN
::Subnets
::del_ip
($subnetid, $subnet, $ip, $hostname)
212 my ($zone, $subnetid, $subnet, $ip, $hostname, $description) = @_;
214 return if !$subnet || !$ip;
216 my $ipamid = $zone->{ipam
};
217 my $dns = $zone->{dns
};
218 my $dnszone = $zone->{dnszone
};
219 my $reversedns = $zone->{reversedns
};
220 my $reversednszone = &$get_reversedns_zone($subnetid, $subnet, $reversedns, $ip);
221 my $dnszoneprefix = $subnet->{dnszoneprefix
};
223 $hostname .= ".$dnszoneprefix" if $dnszoneprefix;
225 #verify dns zones before ipam
226 &$verify_dns_zone($dnszone, $dns);
227 &$verify_dns_zone($reversednszone, $reversedns);
230 my $ipam_cfg = PVE
::Network
::SDN
::Ipams
::config
();
231 my $plugin_config = $ipam_cfg->{ids
}->{$ipamid};
232 my $plugin = PVE
::Network
::SDN
::Ipams
::Plugin-
>lookup($plugin_config->{type
});
234 $plugin->add_ip($plugin_config, $subnetid, $subnet, $ip, $hostname, $description);
241 &$add_dns_record($dnszone, $dns, $hostname, $ip);
243 &$add_dns_ptr_record($reversednszone, $dnszone, $reversedns, $hostname, $ip);
249 PVE
::Network
::SDN
::Subnets
::del_ip
($subnetid, $subnet, $ip, $hostname)
256 my ($zone, $subnetid, $subnet, $ip, $hostname) = @_;
260 my $ipamid = $zone->{ipam
};
261 my $dns = $zone->{dns
};
262 my $dnszone = $zone->{dnszone
};
263 my $reversedns = $zone->{reversedns
};
264 my $reversednszone = &$get_reversedns_zone($subnetid, $subnet, $reversedns, $ip);
265 my $dnszoneprefix = $subnet->{dnszoneprefix
};
266 $hostname .= ".$dnszoneprefix" if $dnszoneprefix;
269 &$verify_dns_zone($dnszone, $dns);
270 &$verify_dns_zone($reversednszone, $reversedns);
273 my $ipam_cfg = PVE
::Network
::SDN
::Ipams
::config
();
274 my $plugin_config = $ipam_cfg->{ids
}->{$ipamid};
275 my $plugin = PVE
::Network
::SDN
::Ipams
::Plugin-
>lookup($plugin_config->{type
});
276 $plugin->del_ip($plugin_config, $subnetid, $subnet, $ip);
280 &$del_dns_record($dnszone, $dns, $hostname, $ip);
281 &$del_dns_ptr_record($reversednszone, $reversedns, $ip);