]> git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/Zones/Plugin.pm
limit vnet/zones/controller to 10 characters
[pve-network.git] / PVE / Network / SDN / Zones / Plugin.pm
1 package PVE::Network::SDN::Zones::Plugin;
2
3 use strict;
4 use warnings;
5
6 use PVE::Tools;
7 use PVE::JSONSchema;
8 use PVE::Cluster;
9
10 use Data::Dumper;
11 use PVE::JSONSchema qw(get_standard_option);
12 use base qw(PVE::SectionConfig);
13
14 PVE::Cluster::cfs_register_file('sdn/zones.cfg',
15 sub { __PACKAGE__->parse_config(@_); });
16
17 PVE::Cluster::cfs_register_file('sdn/zones.cfg.new',
18 sub { __PACKAGE__->parse_config(@_); },
19 sub { __PACKAGE__->write_config(@_); });
20
21 PVE::JSONSchema::register_standard_option('pve-sdn-zone-id', {
22 description => "The SDN zone object identifier.",
23 type => 'string', format => 'pve-sdn-zone-id',
24 });
25
26 PVE::JSONSchema::register_format('pve-sdn-zone-id', \&parse_sdn_zone_id);
27 sub parse_sdn_zone_id {
28 my ($id, $noerr) = @_;
29
30 if ($id !~ m/^[a-z][a-z0-9]*[a-z0-9]$/i) {
31 return undef if $noerr;
32 die "zone ID '$id' contains illegal characters\n";
33 }
34 die "zone ID '$id' can't be more length than 10 characters\n" if length($id) > 10;
35 return $id;
36 }
37
38 my $defaultData = {
39
40 propertyList => {
41 type => {
42 description => "Plugin type.",
43 type => 'string', format => 'pve-configid',
44 type => 'string',
45 },
46 nodes => get_standard_option('pve-node-list', { optional => 1 }),
47 zone => get_standard_option('pve-sdn-zone-id',
48 { completion => \&PVE::Network::SDN::Zones::complete_sdn_zone }),
49 },
50 };
51
52 sub private {
53 return $defaultData;
54 }
55
56 sub decode_value {
57 my ($class, $type, $key, $value) = @_;
58
59 if ($key eq 'nodes') {
60 my $res = {};
61
62 foreach my $node (PVE::Tools::split_list($value)) {
63 if (PVE::JSONSchema::pve_verify_node_name($node)) {
64 $res->{$node} = 1;
65 }
66 }
67
68 return $res;
69 }
70
71 return $value;
72 }
73
74 sub encode_value {
75 my ($class, $type, $key, $value) = @_;
76
77 if ($key eq 'nodes') {
78 return join(',', keys(%$value));
79 }
80
81 return $value;
82 }
83
84 sub parse_section_header {
85 my ($class, $line) = @_;
86
87 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
88 my ($type, $id) = (lc($1), $2);
89 my $errmsg = undef; # set if you want to skip whole section
90 eval { PVE::JSONSchema::pve_verify_configid($type); };
91 $errmsg = $@ if $@;
92 my $config = {}; # to return additional attributes
93 return ($type, $id, $errmsg, $config);
94 }
95 return undef;
96 }
97
98 sub generate_sdn_config {
99 my ($class, $plugin_config, $node, $data, $ctime) = @_;
100
101 die "please implement inside plugin";
102 }
103
104 sub generate_controller_config {
105 my ($class, $plugin_config, $router, $id, $uplinks, $config) = @_;
106
107 die "please implement inside plugin";
108 }
109
110 sub generate_controller_vnet_config {
111 my ($class, $plugin_config, $controller, $transportid, $vnetid, $config) = @_;
112
113 }
114
115 sub write_controller_config {
116 my ($class, $plugin_config, $config) = @_;
117
118 die "please implement inside plugin";
119 }
120
121 sub controller_reload {
122 my ($class) = @_;
123
124 die "please implement inside plugin";
125 }
126
127 sub on_delete_hook {
128 my ($class, $sndid, $scfg) = @_;
129
130 # do nothing by default
131 }
132
133 sub on_update_hook {
134 my ($class, $zoneid, $zone_cfg, $controller_cfg) = @_;
135
136 # do nothing by default
137 }
138
139 #helpers
140 sub parse_tag_number_or_range {
141 my ($str, $max, $tag) = @_;
142
143 my @elements = split(/,/, $str);
144 my $count = 0;
145 my $allowed = undef;
146
147 die "extraneous commas in list\n" if $str ne join(',', @elements);
148 foreach my $item (@elements) {
149 if ($item =~ m/^([0-9]+)-([0-9]+)$/) {
150 $count += 2;
151 my ($port1, $port2) = ($1, $2);
152 die "invalid port '$port1'\n" if $port1 > $max;
153 die "invalid port '$port2'\n" if $port2 > $max;
154 die "backwards range '$port1:$port2' not allowed, did you mean '$port2:$port1'?\n" if $port1 > $port2;
155
156 if ($tag && $tag >= $port1 && $tag <= $port2){
157 $allowed = 1;
158 last;
159 }
160
161 } elsif ($item =~ m/^([0-9]+)$/) {
162 $count += 1;
163 my $port = $1;
164 die "invalid port '$port'\n" if $port > $max;
165
166 if ($tag && $tag == $port){
167 $allowed = 1;
168 last;
169 }
170 }
171 }
172 die "tag $tag is not allowed" if $tag && !$allowed;
173
174 return (scalar(@elements) > 1);
175 }
176
177 #to be move to Network.pm helper
178 sub get_first_local_ipv4_from_interface {
179 my ($interface) = @_;
180
181 my $cmd = ['/sbin/ip', 'address', 'show', 'dev', $interface];
182
183 my $IP = "";
184
185 my $code = sub {
186 my $line = shift;
187
188 if ($line =~ m!^\s*inet\s+($PVE::Tools::IPRE)(?:/\d+|\s+peer\s+)!) {
189 $IP = $1;
190 return;
191 }
192 };
193
194 PVE::Tools::run_command($cmd, outfunc => $code);
195
196 return $IP;
197 }
198
199 1;