]>
git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/SubnetPlugin.pm
1 package PVE
::Network
::SDN
::SubnetPlugin
;
7 use Net
::Subnet
qw(subnet_matcher);
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
;
15 use base
qw(PVE::SectionConfig);
17 PVE
::Cluster
::cfs_register_file
('sdn/subnets.cfg',
18 sub { __PACKAGE__-
>parse_config(@_); },
19 sub { __PACKAGE__-
>write_config(@_); });
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',
27 PVE
::JSONSchema
::register_format
('pve-sdn-subnet-id', \
&parse_sdn_subnet_id
);
28 sub parse_sdn_subnet_id
{
29 my ($id, $noerr) = @_;
35 my ($zone, $ip, $mask) = split(/-/, $id);
39 if (!(PVE
::JSONSchema
::pve_verify_cidrv4
($cidr, 1) ||
40 PVE
::JSONSchema
::pve_verify_cidrv6
($cidr, 1)))
42 return undef if $noerr;
43 die "value does not look like a valid CIDR network\n";
51 subnet
=> get_standard_option
('pve-sdn-subnet-id',
52 { completion
=> \
&PVE
::Network
::SDN
::Subnets
::complete_sdn_subnet
}),
68 description
=> "associated vnet",
71 type
=> 'string', format
=> 'ip',
72 description
=> "Subnet Gateway: Will be assign on vnet for layer3 zones",
76 description
=> "enable masquerade for this subnet if pve-firewall",
78 # #cloudinit, dhcp options
81 # description => "static routes [network=<network>:gateway=<ip>,network=<network>:gateway=<ip>,... ]",
84 type
=> 'string', format
=> 'dns-name',
85 description
=> "dns domain zone prefix ex: 'adm' -> <hostname>.adm.mydomain.com",
92 vnet
=> { optional
=> 0 },
93 gateway
=> { optional
=> 1 },
94 # routes => { optional => 1 },
95 snat
=> { optional
=> 1 },
96 dnszoneprefix
=> { optional
=> 1 },
101 my ($class, $zone, $subnetid, $subnet, $old_subnet) = @_;
103 my $cidr = $subnet->{cidr
};
104 my $mask = $subnet->{mask
};
106 my $subnet_matcher = subnet_matcher
($cidr);
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
};
115 my $old_gateway = $old_subnet->{gateway
} if $old_subnet;
119 my $vnet = PVE
::Network
::SDN
::Vnets
::get_vnet
($vnetid);
120 raise_param_exc
({ vnet
=> "$vnetid don't exist"}) if !$vnet;
121 raise_param_exc
({ vnet
=> "you can't add a subnet on a vlanaware vnet"}) if $vnet->{vlanaware
};
125 my $pointopoint = 1 if Net
::IP
::ip_is_ipv4
($gateway) && $mask == 32;
127 #for /32 pointopoint, we allow gateway outside the subnet
128 raise_param_exc
({ gateway
=> "$gateway is not in subnet $cidr"}) if $gateway && !$subnet_matcher->($gateway) && !$pointopoint;
132 PVE
::Network
::SDN
::Subnets
::add_subnet
($zone, $subnetid, $subnet);
134 #don't register gateway for pointopoint
135 return if $pointopoint;
137 #delete gateway on removal
138 if (!defined($gateway) && $old_gateway) {
140 PVE
::Network
::SDN
::Subnets
::del_ip
($zone, $subnetid, $old_subnet, $old_gateway);
144 if(!$old_gateway || $gateway && $gateway ne $old_gateway) {
145 my $hostname = "$vnetid-gw";
146 my $description = "gateway";
147 PVE
::Network
::SDN
::Subnets
::add_ip
($zone, $subnetid, $subnet, $gateway, $hostname, $mac, $description, 1);
150 #delete old gateway after update
151 if($gateway && $old_gateway && $gateway ne $old_gateway) {
153 PVE
::Network
::SDN
::Subnets
::del_ip
($zone, $subnetid, $old_subnet, $old_gateway);
161 my ($class, $subnetid, $subnet_cfg, $vnet_cfg) = @_;