]> git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/Zones/Plugin.pm
c252dfc20536fd8a1fd6c0a1a2dc6910ae8aad42
[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 "SDN zone object ID '$id' contains illegal characters\n";
33 }
34 return $id;
35 }
36
37 my $defaultData = {
38
39 propertyList => {
40 type => {
41 description => "Plugin type.",
42 type => 'string', format => 'pve-configid',
43 type => 'string',
44 },
45 nodes => get_standard_option('pve-node-list', { optional => 1 }),
46 zone => get_standard_option('pve-sdn-zone-id',
47 { completion => \&PVE::Network::SDN::Zones::complete_sdn_zone }),
48 },
49 };
50
51 sub private {
52 return $defaultData;
53 }
54
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
83 sub parse_section_header {
84 my ($class, $line) = @_;
85
86 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
87 my ($type, $id) = (lc($1), $2);
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
92 return ($type, $id, $errmsg, $config);
93 }
94 return undef;
95 }
96
97 sub generate_sdn_config {
98 my ($class, $plugin_config, $node, $data, $ctime) = @_;
99
100 die "please implement inside plugin";
101 }
102
103 sub generate_controller_config {
104 my ($class, $plugin_config, $router, $id, $uplinks, $config) = @_;
105
106 die "please implement inside plugin";
107 }
108
109 sub generate_controller_vnet_config {
110 my ($class, $plugin_config, $controller, $transportid, $vnetid, $config) = @_;
111
112 }
113
114 sub write_controller_config {
115 my ($class, $plugin_config, $config) = @_;
116
117 die "please implement inside plugin";
118 }
119
120 sub controller_reload {
121 my ($class) = @_;
122
123 die "please implement inside plugin";
124 }
125
126 sub on_delete_hook {
127 my ($class, $sndid, $scfg) = @_;
128
129 # do nothing by default
130 }
131
132 sub on_update_hook {
133 my ($class, $zoneid, $zone_cfg, $controller_cfg) = @_;
134
135 # do nothing by default
136 }
137
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
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
198 1;