]>
git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/SubnetPlugin.pm
1 package PVE
::Network
::SDN
::SubnetPlugin
;
6 use PVE
::Cluster
qw(cfs_read_file cfs_write_file cfs_lock_file);
7 use base
qw(PVE::SectionConfig);
8 use PVE
::JSONSchema
qw(get_standard_option);
9 use PVE
::Exception
qw(raise raise_param_exc);
10 use Net
::Subnet
qw(subnet_matcher);
11 use PVE
::Network
::SDN
::Vnets
;
12 use PVE
::Network
::SDN
::Ipams
;
15 PVE
::Cluster
::cfs_register_file
('sdn/subnets.cfg',
16 sub { __PACKAGE__-
>parse_config(@_); },
17 sub { __PACKAGE__-
>write_config(@_); });
19 PVE
::JSONSchema
::register_standard_option
('pve-sdn-subnet-id', {
20 description
=> "The SDN subnet object identifier.",
21 type
=> 'string', format
=> 'pve-sdn-subnet-id',
25 PVE
::JSONSchema
::register_format
('pve-sdn-subnet-id', \
&parse_sdn_subnet_id
);
26 sub parse_sdn_subnet_id
{
27 my ($id, $noerr) = @_;
33 my ($zone, $ip, $mask) = split(/-/, $id);
37 if (!(PVE
::JSONSchema
::pve_verify_cidrv4
($cidr, 1) ||
38 PVE
::JSONSchema
::pve_verify_cidrv6
($cidr, 1)))
40 return undef if $noerr;
41 die "value does not look like a valid CIDR network\n";
49 subnet
=> get_standard_option
('pve-sdn-subnet-id',
50 { completion
=> \
&PVE
::Network
::SDN
::Subnets
::complete_sdn_subnet
}),
66 description
=> "associated vnet",
69 type
=> 'string', format
=> 'ip',
70 description
=> "Subnet Gateway: Will be assign on vnet for layer3 zones",
74 description
=> "enable masquerade for this subnet if pve-firewall",
76 # #cloudinit, dhcp options
79 # description => "static routes [network=<network>:gateway=<ip>,network=<network>:gateway=<ip>,... ]",
82 type
=> 'string', format
=> 'dns-name',
83 description
=> "dns domain zone prefix ex: 'adm' -> <hostname>.adm.mydomain.com",
90 vnet
=> { optional
=> 0 },
91 gateway
=> { optional
=> 1 },
92 # routes => { optional => 1 },
93 snat
=> { optional
=> 1 },
94 dnszoneprefix
=> { optional
=> 1 },
99 my ($class, $zone, $subnetid, $subnet, $old_subnet) = @_;
101 my $cidr = $subnet->{cidr
};
102 my $mask = $subnet->{mask
};
104 my $subnet_matcher = subnet_matcher
($cidr);
106 my $vnetid = $subnet->{vnet
};
107 my $gateway = $subnet->{gateway
};
108 my $ipam = $zone->{ipam
};
109 my $dns = $zone->{dns
};
110 my $dnszone = $zone->{dnszone
};
111 my $reversedns = $zone->{reversedns
};
113 my $old_gateway = $old_subnet->{gateway
} if $old_subnet;
116 my $vnet = PVE
::Network
::SDN
::Vnets
::get_vnet
($vnetid);
117 raise_param_exc
({ vnet
=> "$vnetid don't exist"}) if !$vnet;
118 raise_param_exc
({ vnet
=> "you can't add a subnet on a vlanaware vnet"}) if $vnet->{vlanaware
};
121 my $pointopoint = 1 if Net
::IP
::ip_is_ipv4
($gateway) && $mask == 32;
123 #for /32 pointopoint, we allow gateway outside the subnet
124 raise_param_exc
({ gateway
=> "$gateway is not in subnet $cidr"}) if $gateway && !$subnet_matcher->($gateway) && !$pointopoint;
128 my $ipam_cfg = PVE
::Network
::SDN
::Ipams
::config
();
129 my $plugin_config = $ipam_cfg->{ids
}->{$ipam};
130 my $plugin = PVE
::Network
::SDN
::Ipams
::Plugin-
>lookup($plugin_config->{type
});
131 $plugin->add_subnet($plugin_config, $subnetid, $subnet);
133 #don't register gateway for pointopoint
134 return if $pointopoint;
136 #delete gateway on removal
137 if (!defined($gateway) && $old_gateway) {
139 PVE
::Network
::SDN
::Subnets
::del_ip
($zone, $subnetid, $old_subnet, $old_gateway);
143 if(!$old_gateway || $gateway && $gateway ne $old_gateway) {
144 PVE
::Network
::SDN
::Subnets
::add_ip
($zone, $subnetid, $subnet, $gateway);
147 #delete old gateway after update
148 if($gateway && $old_gateway && $gateway ne $old_gateway) {
150 PVE
::Network
::SDN
::Subnets
::del_ip
($zone, $subnetid, $old_subnet, $old_gateway);
158 my ($class, $subnetid, $subnet_cfg, $vnet_cfg) = @_;