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