]> git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/SubnetPlugin.pm
use statements cleanup
[pve-network.git] / PVE / Network / SDN / SubnetPlugin.pm
1 package PVE::Network::SDN::SubnetPlugin;
2
3 use strict;
4 use warnings;
5
6 use Net::IP;
7 use Net::Subnet qw(subnet_matcher);
8
9 use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file);
10 use PVE::Exception qw(raise raise_param_exc);
11 use PVE::JSONSchema qw(get_standard_option);
12 use PVE::Network::SDN::Ipams;
13 use PVE::Network::SDN::Vnets;
14
15 use base qw(PVE::SectionConfig);
16
17 PVE::Cluster::cfs_register_file('sdn/subnets.cfg',
18 sub { __PACKAGE__->parse_config(@_); },
19 sub { __PACKAGE__->write_config(@_); });
20
21 PVE::JSONSchema::register_standard_option('pve-sdn-subnet-id', {
22 description => "The SDN subnet object identifier.",
23 type => 'string', format => 'pve-sdn-subnet-id',
24 type => 'string'
25 });
26
27 PVE::JSONSchema::register_format('pve-sdn-subnet-id', \&parse_sdn_subnet_id);
28 sub parse_sdn_subnet_id {
29 my ($id, $noerr) = @_;
30
31 my $cidr = "";
32 if($id =~ /\//) {
33 $cidr = $id;
34 } else {
35 my ($zone, $ip, $mask) = split(/-/, $id);
36 $cidr = "$ip/$mask";
37 }
38
39 if (!(PVE::JSONSchema::pve_verify_cidrv4($cidr, 1) ||
40 PVE::JSONSchema::pve_verify_cidrv6($cidr, 1)))
41 {
42 return undef if $noerr;
43 die "value does not look like a valid CIDR network\n";
44 }
45 return $id;
46 }
47
48 my $defaultData = {
49
50 propertyList => {
51 subnet => get_standard_option('pve-sdn-subnet-id',
52 { completion => \&PVE::Network::SDN::Subnets::complete_sdn_subnet }),
53 },
54 };
55
56 sub type {
57 return 'subnet';
58 }
59
60 sub private {
61 return $defaultData;
62 }
63
64 sub properties {
65 return {
66 vnet => {
67 type => 'string',
68 description => "associated vnet",
69 },
70 gateway => {
71 type => 'string', format => 'ip',
72 description => "Subnet Gateway: Will be assign on vnet for layer3 zones",
73 },
74 snat => {
75 type => 'boolean',
76 description => "enable masquerade for this subnet if pve-firewall",
77 },
78 # #cloudinit, dhcp options
79 # routes => {
80 # type => 'string',
81 # description => "static routes [network=<network>:gateway=<ip>,network=<network>:gateway=<ip>,... ]",
82 # },
83 dnszoneprefix => {
84 type => 'string', format => 'dns-name',
85 description => "dns domain zone prefix ex: 'adm' -> <hostname>.adm.mydomain.com",
86 },
87 };
88 }
89
90 sub options {
91 return {
92 vnet => { optional => 0 },
93 gateway => { optional => 1 },
94 # routes => { optional => 1 },
95 snat => { optional => 1 },
96 dnszoneprefix => { optional => 1 },
97 };
98 }
99
100 sub on_update_hook {
101 my ($class, $zone, $subnetid, $subnet, $old_subnet) = @_;
102
103 my $cidr = $subnet->{cidr};
104 my $mask = $subnet->{mask};
105
106 my $subnet_matcher = subnet_matcher($cidr);
107
108 my $vnetid = $subnet->{vnet};
109 my $gateway = $subnet->{gateway};
110 my $ipam = $zone->{ipam};
111 my $dns = $zone->{dns};
112 my $dnszone = $zone->{dnszone};
113 my $reversedns = $zone->{reversedns};
114
115 my $old_gateway = $old_subnet->{gateway} if $old_subnet;
116
117 if($vnetid) {
118 my $vnet = PVE::Network::SDN::Vnets::get_vnet($vnetid);
119 raise_param_exc({ vnet => "$vnetid don't exist"}) if !$vnet;
120 raise_param_exc({ vnet => "you can't add a subnet on a vlanaware vnet"}) if $vnet->{vlanaware};
121 }
122
123 my $pointopoint = 1 if Net::IP::ip_is_ipv4($gateway) && $mask == 32;
124
125 #for /32 pointopoint, we allow gateway outside the subnet
126 raise_param_exc({ gateway => "$gateway is not in subnet $cidr"}) if $gateway && !$subnet_matcher->($gateway) && !$pointopoint;
127
128
129 if ($ipam) {
130 my $ipam_cfg = PVE::Network::SDN::Ipams::config();
131 my $plugin_config = $ipam_cfg->{ids}->{$ipam};
132 my $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($plugin_config->{type});
133 $plugin->add_subnet($plugin_config, $subnetid, $subnet);
134
135 #don't register gateway for pointopoint
136 return if $pointopoint;
137
138 #delete gateway on removal
139 if (!defined($gateway) && $old_gateway) {
140 eval {
141 PVE::Network::SDN::Subnets::del_ip($zone, $subnetid, $old_subnet, $old_gateway);
142 };
143 warn if $@;
144 }
145 if(!$old_gateway || $gateway && $gateway ne $old_gateway) {
146 my $hostname = "$vnetid-gw";
147 my $description = "$vnetid gw";
148 PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $gateway, $hostname, $description, 1);
149 }
150
151 #delete old gateway after update
152 if($gateway && $old_gateway && $gateway ne $old_gateway) {
153 eval {
154 PVE::Network::SDN::Subnets::del_ip($zone, $subnetid, $old_subnet, $old_gateway);
155 };
156 warn if $@;
157 }
158 }
159 }
160
161 sub on_delete_hook {
162 my ($class, $subnetid, $subnet_cfg, $vnet_cfg) = @_;
163
164 return;
165 }
166
167 1;