]> git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/SubnetPlugin.pm
controllers: evpn : use frr restart if reload fail
[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 my $mac = undef;
117
118 if($vnetid) {
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};
122 $mac = $vnet->{mac};
123 }
124
125 my $pointopoint = 1 if Net::IP::ip_is_ipv4($gateway) && $mask == 32;
126
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;
129
130
131 if ($ipam) {
132 PVE::Network::SDN::Subnets::add_subnet($zone, $subnetid, $subnet);
133
134 #don't register gateway for pointopoint
135 return if $pointopoint;
136
137 #delete gateway on removal
138 if (!defined($gateway) && $old_gateway) {
139 eval {
140 PVE::Network::SDN::Subnets::del_ip($zone, $subnetid, $old_subnet, $old_gateway);
141 };
142 warn if $@;
143 }
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);
148 }
149
150 #delete old gateway after update
151 if($gateway && $old_gateway && $gateway ne $old_gateway) {
152 eval {
153 PVE::Network::SDN::Subnets::del_ip($zone, $subnetid, $old_subnet, $old_gateway);
154 };
155 warn if $@;
156 }
157 }
158 }
159
160 sub on_delete_hook {
161 my ($class, $subnetid, $subnet_cfg, $vnet_cfg) = @_;
162
163 return;
164 }
165
166 1;