]> git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/VnetPlugin.pm
Fix vnet gateway for routed setup + /32 pointopoint subnet
[pve-network.git] / PVE / Network / SDN / VnetPlugin.pm
1 package PVE::Network::SDN::VnetPlugin;
2
3 use strict;
4 use warnings;
5
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
11 PVE::Cluster::cfs_register_file('sdn/vnets.cfg',
12 sub { __PACKAGE__->parse_config(@_); },
13 sub { __PACKAGE__->write_config(@_); });
14
15 PVE::JSONSchema::register_standard_option('pve-sdn-vnet-id', {
16 description => "The SDN vnet object identifier.",
17 type => 'string', format => 'pve-sdn-vnet-id',
18 });
19
20 PVE::JSONSchema::register_format('pve-sdn-vnet-id', \&parse_sdn_vnet_id);
21 sub parse_sdn_vnet_id {
22 my ($id, $noerr) = @_;
23
24 if ($id !~ m/^[a-z][a-z0-9]*[a-z0-9]$/i) {
25 return undef if $noerr;
26 die "vnet ID '$id' contains illegal characters\n";
27 }
28 die "vnet ID '$id' can't be more length than 8 characters\n" if length($id) > 8;
29 return $id;
30 }
31
32 my $defaultData = {
33
34 propertyList => {
35 vnet => get_standard_option('pve-sdn-vnet-id',
36 { completion => \&PVE::Network::SDN::Vnets::complete_sdn_vnet }),
37 },
38 };
39
40 sub type {
41 return 'vnet';
42 }
43
44 sub private {
45 return $defaultData;
46 }
47
48 sub properties {
49 return {
50 zone => {
51 type => 'string',
52 description => "zone id",
53 },
54 type => {
55 description => "Type",
56 optional => 1,
57 },
58 tag => {
59 type => 'integer',
60 description => "vlan or vxlan id",
61 },
62 vlanaware => {
63 type => 'boolean',
64 description => 'Allow vm VLANs to pass through this vnet.',
65 },
66 alias => {
67 type => 'string',
68 description => "alias name of the vnet",
69 optional => 1,
70 },
71 mac => {
72 type => 'string',
73 description => "Anycast router mac address",
74 optional => 1, format => 'mac-addr'
75 }
76 };
77 }
78
79 sub options {
80 return {
81 zone => { optional => 0},
82 tag => { optional => 1},
83 alias => { optional => 1 },
84 mac => { optional => 1 },
85 vlanaware => { optional => 1 },
86 };
87 }
88
89 sub on_delete_hook {
90 my ($class, $vnetid, $vnet_cfg) = @_;
91
92 #verify if subnets are associated
93 my $subnets = PVE::Network::SDN::Vnets::get_subnets($vnetid);
94 my @subnetlist = ();
95 foreach my $subnetid (sort keys %{$subnets}) {
96 push @subnetlist, $subnetid;
97 }
98 raise_param_exc({ vnet => "Vnet is attached to following subnets:". join(',', @subnetlist)}) if @subnetlist > 0;
99 }
100
101 sub on_update_hook {
102 my ($class, $vnetid, $vnet_cfg, $subnet_cfg) = @_;
103 # verify that tag is not already defined in another vnet
104 if (defined($vnet_cfg->{ids}->{$vnetid}->{tag})) {
105 my $tag = $vnet_cfg->{ids}->{$vnetid}->{tag};
106 foreach my $id (keys %{$vnet_cfg->{ids}}) {
107 next if $id eq $vnetid;
108 my $vnet = $vnet_cfg->{ids}->{$id};
109 if ($vnet->{type} eq 'vnet' && defined($vnet->{tag})) {
110 raise_param_exc({ tag => "tag $tag already exist in vnet $id"}) if $tag eq $vnet->{tag};
111 }
112 }
113 }
114 }
115
116 1;